15.04.2018 Views

programming-for-dummies

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

600<br />

Looping Statements<br />

Looping Statements<br />

A looping statement repeats one or more commands <strong>for</strong> a fixed number of<br />

times or until a certain Boolean condition becomes True. To create a loop<br />

that repeats <strong>for</strong> a fixed number of times, use the FOR-NEXT loop, which<br />

looks like this:<br />

FOR variable = Start TO End<br />

Command<br />

NEXT<br />

If you wanted the FOR-NEXT loop to run five times, you’d set the Start value<br />

to 1 and the End value to 5, such as<br />

FOR variable = 1 TO 5<br />

Command<br />

NEXT<br />

Normally the FOR-NEXT loop counts by one, but you can use the STEP keyword<br />

to make the FOR-NEXT loop count by any value, such as by three, as<br />

shown in this example:<br />

FOR variable = 1 TO 36 STEP 3<br />

Command<br />

NEXT<br />

Rather than count up, the FOR-NEXT loop can also count down. In Visual<br />

Basic, you can count down by using a negative number after the STEP keywords,<br />

such as<br />

FOR variable = 100 TO 1 STEP -1<br />

Command<br />

NEXT<br />

In REALbasic, you can count down by replacing the TO keyword with the<br />

DOWNTO keyword, such as<br />

FOR variable = 100 DOWNTO 1<br />

Command<br />

NEXT<br />

If you don’t know how many times you need to repeat commands, use a DO<br />

loop. The two variations of a DO loop are DO-UNTIL and DO-WHILE (available<br />

only in Visual Basic).

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

Saved successfully!

Ooh no, something went wrong!