18.08.2013 Views

Dalla A alla Z passando per C - Robotica

Dalla A alla Z passando per C - Robotica

Dalla A alla Z passando per C - Robotica

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

}<br />

printf("numero di iterazioni: ");<br />

scanf("%d", &max);<br />

for(i = max, somma = 0; i > 0; i = i - 1)<br />

somma = somma + 1.0 / (i * i * i);<br />

printf("%f\n", somma);<br />

return 0;<br />

NOTA Gli identificatori i e j sono spesso usati <strong>per</strong> le variabili che fungono da contatori.<br />

A rischio di essere ovvio, si noti che può essere usata una variabile<br />

qualsiasi.<br />

Utilizzare la virgola invece del punto-e-virgola <strong>per</strong> separare le espressioni in un costrutto<br />

for costituisce un errore di sintassi.<br />

5.5 Il costrutto if<br />

Il costrutto if serve <strong>per</strong> realizzare l’istruzione di salto condizionale specificata dal diagramma di<br />

flusso di Figura 5.5. Assume la forma<br />

if ( espr ) istr ;<br />

#include <br />

int main()<br />

{<br />

int a, b, max;<br />

}<br />

scanf("%d %d", &a, &b);<br />

max = a;<br />

if (b > a)<br />

max = b;<br />

printf("il massimo è: %d", max);<br />

return 0;<br />

Il costrutto if ammette l’istruzione opzionale else, <strong>per</strong> cui il costrutto if-else serve <strong>per</strong><br />

realizzare l’istruzione condizionale definita dal diagramma di flusso di Figura 5.6. Assume la<br />

forma<br />

if ( espr ) istr1 else istr2<br />

In Figura 5.7 il costrutto if è utilizzato <strong>per</strong> determinare il massimo tra due numeri interi.<br />

47

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!