Loops

From ROBOTC API Guide
< Tutorials
Revision as of 19:54, 30 July 2012 by Pgp90 (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Loops

While Loops

Notepad.gif NOTE: under construction.

While loops are a programming statement that is used to run a section of code multiple times. The while loop consists of a condition and a block of code. The condition is used to check if the block of code should be run again. As long as the condition evaluates to true, the block of code will run.

The code block is a group of programming statements that are enclosed in curly brackets ({}). This block of code can be made up of any statements.

while(condition)
{
  //code to execute
}


For Loops

Notepad.gif NOTE: under construction.

For loops are a programming statement that is used to run a section of code multiple times. The for loop consists of an initialization section, a condition, an incrementer and a block of code. The initialization section sets a variable at a starting value. The condition is used to check if the loop should execute the block of code again. After every iteration of the loop, the incrementer will run. this is used to change the value of the variable from the initialization section.

The code block is a group of programming statements that are enclosed in curly brackets ({}). This block of code can be made up of any statements.

for(Initialization; Condition; incrementer)
{
  //code to execute
}

For loops can be thought of as a partially configured while loop.

for(int i=0; i<10; i++)
{
  //code to execute 10 times
}
is equivalent to
int i=0;
while(i<10)
{
  //code to execute 10 times
 
  i++;
}