Tutorial 6.1: while Repetition Structure

INTRODUCTION
In the previous tutorial, we have learned about sequence and selection structures. We know that selection structure allows us to specify how the computer will react to certain inputs. But there is still a limitation in what we can do in programming. For example what if we want to display numbers from 1 to 100? Of course we can do hundred lines of printf(), but what if we want to repeat a thousand times of 10 statements. We would have to write ten thousand of statements to achieve it and it is possible to do it. But it is not a convenience and it would take a long time to complete. Thus the ability to specify repetition of compound statements is as important in programming. Repetition of statements execution is called loop. Looping in a program is the process of executing series of instruction repeatedly in a given number of times.

There are two types of loop namely definite loop and indefinite loop. Definite loop also called counter-controlled loop is a repetition in which it will continue looping until certain condition is met. On the contrary indefinite loop has no certain condition in which it will continue looping forever. Thus statements enclosed by indefinite loop should contain the means of exiting from the loop. Typically, there are three requirements have to be specified in defining a definite loop. They are counter initialization, increment of counter and loop condition. The counter will determine how many times the loop will be executed. There are three types of repetition structure available in C namely while loop, do while loop and for loop.

But before going further, you should understand the concept of True and False in C. We have covered this concept in Tutorial 5. You might want to look at it if you still have not grasped the basic concept. We begin by discussing the while loop followed by do while loop. Then we will discuss for loop because I believe for loop is a bit complicated compared to the other loops.

while LOOP
while loop allows the repetition of a set of statements execution to continue for as long as a specified expression (condition) is true. When the condition becomes false, the repetition terminates and the first statement after the repetition structure is executed. Basically while loop is similar to if selection in the sense that their set of statements are executed if the condition is true. The different is while loop repeats the statement execution as long as the condition is true and if selection does not have the repetition feature.

Lets look at an example to have an idea what is while loop all about.
#include <stdio.h>

int main()
{
 int count = 0;
 while(count < 10) {
  printf("%d\n", count);
  ++count;
 } 
 return 0;
}
Sample output:

0
1
2
3
4
5
6
7
8
9

Explanation:

This is a very simple while loop. It has an expression as the mechanism of repetition and a set of statements. Now lets look at the first statement.
int count = 0;
Here, we declare an integer variable count and initialize it to 0. Variable count acts as the loop counter for the while loop.
while(count < 10) {
 printf("%d\n", count);
 ++count;
}
Now we look at our while loop. It contains two statements action printing the value of count and ++count. If you are not sure what ++count is doing, you might want to look at Tutorial 4 Increment and Decrement Operators.

Lets go through it line-by-line how while loop works in this example. It begins by testing the while loop condition. Variable count has value of 0, thus the condition of count < 10 is true. printf() prints the value of count and count is incremented. When it reaches the close brace, it jump back up to the beginning of the loop and the condition is tested again. This process is repeated until the condition becomes false and the loop is terminated.

Next: do while Loop

No comments:

Post a Comment