You must have observed that many steps in this program are similar and repetitive. Particularly, statements 7 to 14 are quite similar. It would be great if we could express it in some better way. Something like below:
1. #include <stdio.h>
2. int main()
3. {
4. int sum_till_now;
5.
6. sum_till_now = 1 + 2;
7. Repeat the step below till we have added 10:
8. sum_till_now = sum_till_now + 3;
9.
10. printf("The result is %d\n", sum_till_now);
11. }
See statement 7 above. We have expressed it in plain English, which the computer does not understand. What it understands is something perfectly formal, as below:
1. #include <stdio.h>
2. int main()
3. {
4. int sum_till_now = 0;
5. int next_number = 0;
6.
7. while (next_number < 10)
8. {
9. next_number = next_number + 1;
10. sum_till_now = sum_till_now + next_number;
11. }
12.
13. printf("The result is %d\n", sum_till_now);
14. }
Statements 7 introduces the while statement. Let’s understand how a while statement works.
Whenever the while statement is executed, the condition in the statement is evaluated. If it is true, the code block (i. e. the set of statements in the curly braces) following the while statement is executed. After this, in stead of moving to the next statement, the execution again continues from the while statement.
If the condition in the while statement evaluates to false, the following code block is skipped, i. e. execution continues at the next statement after the code block.
Coming back to our program. Let’s try to execute it manually, using our old rough pad. After statement 4, our rough pad looks like this:
After statement 5, our rough pad becomes:
Now to statement 7. It is a while statement. The condition is next_number < 10, which is true because next_number is now 0 (see our rough pad). Because the condition evaluates to true, execution enters the next code block, i. e. to statement 9. After statement 9 is executed, our rough pad looks like:
Then comes statement 10. After statement 10 is executed. The rough pad looks like:
Now, after statement 10 is executed, execution again continues from the while statement, i. e. statement 7.
In statement 7, the condition evaluates to true, because next_number is now 1, which is less than 10. So, execution enters into the next block, i. e., statement 9 is executed. After execution of statement 9, the rough pad looks like this:
Then, statement 10 is executed. Then execution goes to the while statement, i. e. statement 7.
If you carry on this process, after sometime, you will find that the condition will become false. The rough pad would be looking like this then:
At this point, the next code block, i. e. statements 8 through 11 will be skipped and execution will continue from statement 12. Statement 12, being blank, statement 13 will be executed, which will display the value of sum_till_now, which contains the desired result.
Done! This explains the use of loop. If now you are asked to write a program to add up to 10000, or 1000000, you need only to change at one place, i. e. in the condition in the while statement.