Loops (for, while, do) |
for |
the for statement has the form:
for (initial_value , test_condition , step){
// code to execute inside loop
};
initial_value sets up the initial value of the loop counter.
test_condition this is the condition that is tested to see if the loop is executed again.
step this describes how the counter is changed on each execution of the loop ( increment or decrement ).
Here is an example:
// The following code adds together the numbers 1 through 10
// this variable keeps the running total
int total=0;
// this loop adds the numbers 1 through 10 to the variable total
for (int i=1; i < 11; i++){
total = total + i;
}
So in the preceding chunk of code we have:
initial_condition is int i=0;
test_condition is i < 11;
step is i++;
So, upon initial execution of the loop, the integer variable
i
is set to 1. The statement total = total + i; is executed and the
value of the variable total becomes 1. The step code is now
executed and i is incremented by 1, so its new value is 2.
The test_condition is then checked, and since
i is less
than 11, the loop code is executed and the variable
total gets the
value 3 (since total was 1, and
i was 2.
i
is then incremented by 1 again.
The loop continues to execute until the condition
i<11 fails. At
that point total will have the value 1+2+3+4+5+6+7+8+9+10 = 55.
for (int i = 0, j = 0; j + i <= 10; j++, i++)
printf( "%d\n", j + i );
All three expressions of a for loop are optional
int i=0;
for(;;)
{
if(i<10)
{
printf("hi");
i++;
}
else
break;
}
while |
The while statement has the form:
while ( condition) {
// code to execute};
condition
is a boolean statement that is checked each time after
the final "}" of the while statement executes. If the
condition
is true then the while statement executes again.
If the
condition
is false, the while statement does not
execute again.
The for loop can be written as while loop
initial_value
while ( condition) {
// code to executestep
};
As an example, let's say that we wanted to write all the even numbers between 11 and 23 to the screen. The following is a full C++ program that does that.
#include <stdio.h> int main(){ // this variable holds the present number int current_number = 12; // initial value (1) // while loop that prints all even numbers between // 11 and 23 to the screen while (current_number < 23) // check condition (2){ printf("%d", current_number ); current_number += 2; // increment part(3) } }
The preceding example prints the value of
current_number to the
screen and then adds 2 to its value. As soon as the value of the variable
current_number goes above 23, the while loop exits and the next line is
executed.
The output of the preceding program would be:
12 14 16 18 20 22
do {statement;} while ( condition );
counter=1;do {printf( "%d ", counter );} while (++counter <= 10);
"Increment" may be negative (decrement).
If the loop continuation condition is initially false:
The body of the structure is not performed.
Control proceeds with the next statement after the for structure.
Control variable often printed or used inside for body, but not necessary.
In the loop condition we can use the logical operaters.