while loop. First let's take a look at the syntax of while:
while (condition)
statement;
or
while (condition) {
statement;
statement;
statement;
etc...;
}
while will repeat the stętements as long as the condition is true. If while is used without { }, it will repeat only one statement right after. If while is used with { }, it will repeat all the statements surrouneded by { }. For example:
#include < stdio.h >
main() {
int i = 0;
while ( i < 10 )
i++;
printf ("i = %d\n", i);
}
The program above will repeat i++; until i becomes 10, and then print the value of i. The flow fo the program is as follows.
int i = 0; /* initialize i to 0 */
while ( i < 10 ) /* i < 10 ( i = 0 ),
thus keep going */
i++; /* i = 1 now */
while ( i < 10 ) /* i < 10 ( i = 1 ),
thus keep going */
i++; /* i = 2 now */
while ( i < 10 ) /* i < 10 ( i = 2 ),
thus keep going */
i++; /* i = 3 now */
while ( i < 10 ) /* i < 10 ( i = 3 ),
thus keep going */
...
...
i++; /* i = 8 now */
while ( i < 10 ) /* i < 10 ( i = 8 ),
thus keep going */
i++; /* i = 9 now */
while ( i < 10 ) /* i < 10 ( i = 9 ),
thus keep going */
i++; /* i = 10 now */
while ( i < 10 ) /* i is no longer < 10
( i = 10),
thus stop the loop! */
printf ( "i = %d\n", i );
/* prints out the value of i
which is 10 */
Notice that the it skips the i++; part does not get executed after the last while condition check..sum = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10;i = 1; sum += i; i = 2; sum += i; i = 3; sum += i; i = 4; sum += i; i = 5; sum += i; i = 6; sum += i; i = 7; sum += i; i = 8; sum += i; i = 9; sum += i; i = 10; sum += i;Notice that i gets incremented by one, and then added to sum. Thus, it can be written as:
i = 1; sum += i; i++; sum += i; i++; sum += i; ... ... i++; sum += i;Notice that the
i++; and sum +=i; will be repeated 10 times..
i = 1 sum += i; /* REPEAT 10 TIMES */ i++; /* REPEAT 10 TIMES */Unfortunately, computer won't understand REPEAT 10 TIMES.. =(
while.
i = 1;
while ( some_condition ) {
sum +=i;
i++;
}
Now.. how would you set the some_condition so sum += i; i++; will repeat exactly 10 times, not 9 or 11 times? You have to think a bit carefully here because often times condition is the tricky part. Let's try i < 10 like we did in the previous example.
i = 1;
sum = 0;
while ( i < 10 ) {
sum += i;
i++;
}
printf ("sum = %d\n", sum);
printf ("i = %d\n", i);
Although it seems perfect at first, this program does not perform as you might have guessed. Let's examine by line by line to see what happens in this program.
| i = 1; | /* initialize i with 1 */ |
| while ( i < 10 ) | /* i < 10, (i = 1) thus keep going*/ |
| sum += i; | /* add i to sum. sum = 1 now */ |
| i++; | /* increment i, i = 2 now*/ |
| while ( i < 10 ) | /* i < 10, ( i = 2) thus keep going*/ |
| sum += i; | /* add i to sum. sum = 3 now */ |
| i++; | /* increment i, i = 8 now*/ |
| while ( i < 10 ) | /* i < 10, (i = 8) thus keep going*/ |
| sum += i; | /* add i to sum, sum = 36 now*/ |
| i++; | /* increment i, i = 9 now */ |
| while ( i < 10 ) | /* i < 10. ( i = 9 ) thus, keep going*/ |
| sum += i; | /* add i to sum, sum = 45 now*/ |
| i++; | /* increment i, i = 10 now |
| while ( i < 10 ) | /* i is not < 10, ( i == 10) thus, finish the loop */ |
| printf ("sum = %d\n", sum); | /* print sum to screen. 45 gets printed to screen*/ |
| printf ("i = %d\n", i); | /* print i to screen. 10 gets printed to screen*/ |
sum += i; i++; part won't be executed when i == 10 because the condition was set as i < 10.i <= 10 instead. Thus, the complete code will look like:
/*
* Name : Ji Lee
* Login : cs5a6
* Descrip : this program will add
* 1 to 10 and print the sum
* to screen.
*/
#include < stdio.h >
void main()
{
/* initializing variables */
int i = 1, sum = 0;
/* getting the sum of 1 + 2 + .. 10 */
while ( i <= 10) {
sum += i;
i++;
}
/* print out the result */
printf ("sum = %d\n", sum);
printf ("i = %d\n", i);
}
This program will run as follows:| i = 1; | /* initialize i with 1 */ |
| while ( i <= 10 ) | /* i <= 10, (i = 1) thus keep going*/ |
| sum += i; | /* add i to sum. sum = 1 now */ |
| i++; | /* increment i, i = 2 now*/ |
| while ( i <= 10 ) | /* i <= 10, ( i = 2) thus keep going*/ |
| sum += i; | /* add i to sum. sum = 3 now */ |
| i++; | /* increment i, i = 8 now*/ |
| while ( i <= 10 ) | /* i <= 10, (i = 8) thus keep going*/ |
| sum += i; | /* add i to sum, sum = 36 now*/ |
| i++; | /* increment i, i = 9 now */ |
| while ( i <= 10 ) | /* i <= 10. ( i = 9 ) thus, keep going*/ |
| sum += i; | /* add i to sum, sum = 45 now*/ |
| i++; | /* increment i, i = 10 now |
| while ( i <=10 ) | /* i <= 10, ( i = 10), thus keep going*/ |
| sum += i; | /* add i to sum, sum = 55 now*/ |
| i++; | /* increment i, i = 11 now |
| while ( i <= 10 ) | /* i is not <= 10, ( i == 11) thus, finish the loop */ |
| printf ("sum = %d\n", sum); | /* print sum to screen. 55 gets printed to screen*/ |
| printf ("i = %d\n", i); | /* print i to screen. 11 gets printed to screen*/ |
sum = 45 i = 11to screen. Notice the use of indent within
while loop for readability. It is a convention which most programmers, if not all, follow..