TScript Keywords Loops (for, while, do..while)

The for keyword

Probably the most useful of all the loops keyword is the "for" loop, as it has a section for the creation of variables required for the execution of the loop, as well as a conditional section and a tail incremental section. The basic syntax for the "for" loop is: -

for(declarations; condition; incremental){
   ... break;
   ... continue;
   ...
}

The declaration section allows for variables to be created and initialise in the loop. The conditional section controls the operation of the loop, it will execute the code within the braces each time the condition hold true. The incremental section will be executed at the end of each loop. If the braces are missing, then only one line of code is executed. The "break" statement will end the loop where it is called and the "continue" statement will make the execution jump straight to the incremental section.

The while keyword

The most basic of all the loop statements is the "while" loop. It only contains the conditional statement. The basic syntax for the "while" loop is: -

while(condition){
   ... break;
   ... continue;
   ...
}

Like the "for" loop, the conditional section controls the operation of the loop, it will execute the code within the braces each time the condition hold true. If the braces are missing, then only one line of code is executed. The "break" statement will end the loop where it is called and the "continue" statement will make the execution jump back up to the top.

The do..while keyword

The "do..while" loop is similar to the while statements, but is execute once before the condition is checked. The basic syntax for the "do..while" loop is: -

do{
   ... break;
   ... continue;
   ...
} while(condition);

The conditional section controls the operation of the loop and is checked at the end of each illiteration, it will return to the start of the "do" loop each time the condition hold true. The braces must be present. The "break" statement will end the loop where it is called and the "continue" statement will make the execution jump to the conditional statement before moving back up to the top if the condition remains true.