Iteration, repetition, or looping, they all mean the same thing. When you want to repeat some portion of the program, you will use some form of iteration. Looping is a very common term to describe this.
The while Statement
Whereas the if statement caused statement to be executed exactly once if expression
was true, the while statement causes statement to be executed repeatedly as long as
expression remains true. If expression becomes false, then the repetition stops.
Simple example:
The do Statement
Single statement Multiple statements (braces required)
while ( expression )
statement
while ( expression )
{
statements
}
Example with output (and a review of expressions). Note that, like the if statement, the braces are only
required with multiple (compound) statements:
int count = 5;
int i = 0;
while (i < count) /* controlling expression */
i++; /* body of the loop */
The output from all 3 examples:
Version #1 (braces required) Version #2 (braces optional) Version #3 (braces optional)
int count = 5;
int i = 0;
while (i < count)
{
i++;
printf("i is %i\n", i);
}
int count = 5;
int i = 0;
while (i < count)
{
printf("i is %i\n", ++i);
}
int count = 5;
int i = 0;
while (i++ < count)
printf("i is %i\n", i);
Bart Simpson could have used C to make his punishment less painful:
i is 1
i is 2
i is 3
i is 4
i is 5
(Partial) output:
int i = 1;
while (i <= 100)
{
printf("I will include the the course number in email correspondence with the teacher.\n");
i++;
}
Notes:
I will include the course number in email correspondence with the teacher.
I will include the course number in email correspondence with the teacher.
I will include the course number in email correspondence with the teacher.
I will include the course number in email correspondence with the teacher.
I will include the course number in email correspondence with the teacher.
I will include the course number in email correspondence with the teacher.
I will include the course number in email correspondence with the teacher.
I will include the course number in email correspondence with the teacher.
I will include the course number in email correspondence with the teacher.
I will include the course number in email correspondence with the teacher.
I will include the course number in email correspondence with the teacher.
I will include the course number in email correspondence with the teacher.
I will include the course number in email correspondence with the teacher.
I will include the course number in email correspondence with the teacher.
I will include the course number in email correspondence with the teacher.
I will include the course number in email correspondence with the teacher.
I will include the course number in email correspondence with the teacher.
I will include the course number in email correspondence with the teacher.
I will include the course number in email correspondence with the teacher.
I will include the course number in email correspondence with the teacher.
Infinite loop #1 Infinite loop #2 Infinite loop #3
int i = 1;
while (i != 10)
i += 2;
int i = 0;
while (i < 10)
printf("i is %i\n", i);
while (1)
printf("This loop never ends...\n");
Single statement Multiple statements do statement while ( expression ); do { statements } while ( expression );
The primary difference between the while statement and the do statement is that the body of the do statement is guaranteed to execute at least once. This is simply because the controlling expression is executed after the first iteration of the loop body:
Body executes 0 or more times Body executes 1 or more times while ( expression ) statement do statement while ( expression );
Example:
Sample run:int number; int choice; do { printf("Enter a number: "); scanf("%d", &number); printf("You entered %i\n", number); printf("Enter another number? (1=yes,0=no) "); scanf("%d", &choice); } while (choice);
There really isn't much difference between while statement and the do statement. If you need the loop to execute at least once, then the do statement is the one to use.Enter a number: 23 You entered 23 Enter another number? (1=yes,0=no) 1 Enter a number: 10 You entered 10 Enter another number? (1=yes,0=no) 1 Enter a number: 5 You entered 5 Enter another number? (1=yes,0=no) 3 Enter a number: 12 You entered 12 Enter another number? (1=yes,0=no) 0
In computer science, there are 3 general kinds of repetitions regarding the number of repetitions and the technique used:
Technically, the if isn't repeating anything. However, keep these keywords in mind when you have to do something in one of those three ways.
Repetitions Keyword 0 or 1 time if 0 or more times while 1 or more times do...while
The for Statement
Now we get to the most complex of the looping mechanisms: the for statement. The general form is:and the compound version:for ( expression1 ; expression2 ; expression3 ) statement
The meaning of this is a little involved:for ( expression1 ; expression2 ; expression3 ) { statements }
This also means that any for loop can be written as a while loop and vice-versa.expression1; while ( expression2 ) { statements expression3; }
Simple examples to print the numbers 1 through 10:
for loop #1 | for loop #2 | while loop |
---|---|---|
int i; for (i = 1; i <= 10; i++) printf("%i\n", i); |
int i; for (i = 0; i < 10; i++) printf("%i\n", i + 1); |
int i = 1; while (i <= 10) printf("%i\n", i++); |
The for loops above show the typical ways in which they are used. The variable i is sometimes called the loop control variable (or simply the counter) because it controls when the loop continues or stops. The three expressions generally do these things:
Count to 20 by 2 | Count down from 30 by 3 | Squares of 1 to 10 |
---|---|---|
int i; for (i = 2; i <= 20; i += 2) printf("%i\n", i); 2 4 6 8 10 12 14 16 18 20 |
int i; for (i = 30; i >= 3; i -= 3) printf("%i\n", i); 30 27 24 21 18 15 12 9 6 3 |
int i; for (i = 1; i <= 10; i++) printf("%i\n", i * i); 1 4 9 16 25 36 49 64 81 100 |
Of course, all of these could be written as while loops as well.
Count to 20 by 2 | Count down from 30 by 3 | Squares of 1 to 10 |
---|---|---|
int i = 2; while (i <= 20) { printf("%i\n", i); i += 2; } |
int i = 30; while (i >= 3) { printf("%i\n", i); i -= 3; } |
int i = 1; while (i <= 10) { printf("%i\n", i * i); i++; } |
More on Looping
Of course, this is nothing but a strange-looking while loop now.int i = 1; for (; i <= 10;) printf("%i\n", i++);
You can even omit the second expression, but this would lead to an infinite loop, since the default empty expression is true!int i = 1; while (i <= 10) printf("%i\n", i++);
If you want to exit from the loop prematurely, you can use the break statement:int i = 1; for (;;) printf("%i\n", i++);
breaking out of infinite for | breaking out of infinite while |
---|---|
int i = 1; for (;;) { printf("%i\n", i++); if (i > 10) break; } |
int i = 1; while (1) { printf("%i\n", i++); if (i > 10) break; } |
The break statement can be used in any of the looping mechanisms as well as the switch statement.
You can also have multiple expressions in between the semicolons:
Using a for loop | Using a while loop |
---|---|
int i; for (i = 0, j = 0; i < 16 || j < 3; i +=2, j++) printf("%i * %i = %i\n", i, j, i * j); 0 * 0 = 0 2 * 1 = 2 4 * 2 = 8 6 * 3 = 18 8 * 4 = 32 10 * 5 = 50 12 * 6 = 72 14 * 7 = 98 |
int i = 0; int j = 0; while (i < 16 || j < 3) { printf("%i * %i = %i\n", i, j, i * j); i += 2; j++; } |
Note the use of the comma operator used in the expressions. This operator has the form:
expression1 , expression2 , expression3 , etc...
i = 5; j = 3; c = ++i, ++j, j + i; printf("%i, %i, %i\n",i,j,c); |
i = 5; j = 3; c = (++i), (++j), (j + i); printf("%i, %i, %i\n",i,j,c); |
i = 5; j = 3; c = (++i, ++j, j + i); printf("%i, %i, %i\n",i,j,c); |
The continue statement is similar to the break statement in that it causes the loop to deviate from its prescribed course. The difference is subtle, but very important.
break statement | continue statement |
---|---|
for (/* expressions */) { /* first statement in loop */ /* second statement in loop */ /* etc... */ break; /* last statement in loop */ } [break jumps to here] /* first statement after loop */ |
for (/* expressions */) { /* first statement in loop */ /* second statement in loop */ /* etc... */ continue; /* last statement in loop */ [continue jumps to here] } /* first statement after loop */ |
This prints the even numbers from 2 to 20:
using for | using while | using while |
---|---|---|
for (i = 2; i <= 20; i++) { if ( (i % 2) == 1 ) continue; printf("%i\n", i); } 2 4 6 8 10 12 14 16 18 20 |
i = 2; while (i <= 20) { if ( (i % 2) == 1 ) { i++; continue; } printf("%i\n", i++); } |
i = 2; while (i <= 20) { if ( (i++ % 2) == 1 ) continue; printf("%i\n", i - 1); } |