15.04.2018 Views

programming-for-dummies

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

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

202<br />

Looping a Fixed Number of Times with the FOR-NEXT Loop<br />

FOR I = 1 TO 5<br />

PRINT “John Smith”<br />

I = 3<br />

NEXT I<br />

This FOR-NEXT loop runs five times. The first time the FOR-NEXT loop runs,<br />

the value of the I variable is set to 1. But within the FOR-NEXT loop, the<br />

value of the I variable is then set to 3. So each time the FOR-NEXT loop runs<br />

again, it checks to see if the I variable’s value is between 1 and 5.<br />

Because the FOR-NEXT loop always resets the value of the I variable to 3,<br />

the I variable never falls outside the range of 1 to 5, so this FOR-NEXT loop<br />

runs indefinitely.<br />

The curly bracket language family creates a FOR-NEXT loop that looks<br />

slightly different than the way other languages do. For example, this is how<br />

BASIC creates a FOR-NEXT loop:<br />

FOR I = 1 TO 15<br />

PRINT “John Smith”<br />

NEXT I<br />

This is the equivalent FOR-NEXT loop in C:<br />

<strong>for</strong> (i = 1, i

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

Saved successfully!

Ooh no, something went wrong!