Thursday, 17 October 2013

While Loop In C++

While Loop In C++:

In C++, there are times when it becomes necessary to repeat a set of statements several times. One way is to write the code in the program over and over but that would make the code very long and it would become a messy code. Just imagine that if you have to repeat a set of statements 100 times then you have to write that 100 times again and imagine where the length of the code would go.
Fortunately, we have a way that finishes all this fuss and that is repetition structures. There are three repetition structures but in this C tutorial for beginners, we are going to study c while loop.

General form:

while (expression)
    statement




while loop in C++


In C++, while is a reserved keyword. The statement is a simple or compound statement. The expression acts as a decision maker and it is usually a logical expression. The body of the c while loop is between parenthesis.
The expression provides an entry condition. If the condition is true then the program enters into the while loop else the loop doesn’t execute. If the value is true then the while loop c will keep on going until the expression value becomes false or any break statement executes. But if the expression value never changes into false and there is no break statement then the while loop becomes an infinite loop. Infinite loop means that it will keep on executing.

Designing while loops:

There are many ways to design a while loop in c. Below are some cases that gives us the idea of the usefulness of this structure.

Counter-controlled while loops in c:

Suppose that we want to execute a simple or compound statement known number of times then we used an int variable which acts as counter and initialize it with zero and increment its value till the number of times we want to execute the statement. 

Below is an example:

int counter=0;
while(counter < size)            //size is the number of times 

                                           //we want to executes this while 
                                           //loop
    {
        .
        .
        counter++;
        .
        .   
    }

Sentinel-controlled while loop in c:

Sometimes, we don’t know that how many pieces of data need to be read but you know that there is a value on which this loop will terminate. Such special value is called sentinel. In this case, you read one value before the loop and then while loop will execute. 

Below is the example of this type of loop:


cin>>variable;                //taking the input before the loop
while (variable != sentinel)        //test the loop control //variable and sentinel is the terminating value that you know
{
    cin>>variable
    .
    .
    .
}

Flag-controlled while loop:

Flag controlled while loop uses a bool variable to control the loop. This loop looks like the one shown below:

bool found = false;
while (!found)
{
    If (expression)
        found = true;
    .
    .
}

Final words:

C while loop with its different types has been discussed in this c tutorial for beginners. By studying the given examples one can easily understand how to use while loop in c.

0 comments:

Post a Comment