Loops

Zoltan Padrah edited this page Aug 22, 2012 · 1 revision
Clone this wiki locally
  • Goto
  • label: { statement 1 statement 2 statement 3 .................. statement n } goto label
  • For
  • The for loop can execute a block of code for a fixed or given number of times. Its syntax is as follows. for initialization to test conditions { block of code } The above loop will execute until the test condition fails.
  • While
  • The while loop is used to execute a block of code as long as some condition is true. If the condition is false from the start the block of code is not executed at all. The while loop tests the condition before it’s executed so sometimes the loop may never be executed if initially the condition is not met. Its syntax is as follows. while condition { block of statements executed if condition is true }
  • Repeat
  • The do loop also executes a block of code as long as a condition is satisfied. The difference between a ”repeat ...until” loop and a ”while ” loop is that the while loop tests its condition before execution of the contents of the loop begins; the ”repeat” loop tests its condition after it’s been executed at least once. As noted above, if the test condition is false as the while loop is entered the block of code is never executed. Since the condition is tested at the bottom of a repeat loop, its block of code is always executed at least once.Its syntax is as follows. repeat { block of statements to be executed until the condition is true } until condition