Coding Basics: Loops

Example 1:

x = 1;

sum = 0;

while (x <= 3)

{

    sum = sum + x;

   x = x + 1;

}

//sum holds the value 6 after the while loop.


Example 2:

number = 1;

for (i = 1; i <= 4; i++)

{

    number++;

}

//number holds the value 5 after the for loop.  The loop will iterate four times (when i is 1, 2, 3, and 4).  Number is incremented by 1 for each iteration.  Therefore, number is 5 upon termination of the loop.