while statement in C++

The second type of loop, the while loop, is used when we are not certain that the loop will be executed. After checking whether the initial condition is true or false and finding it to be true, then only while loop will enter into the loop operations.

The general form of the while loop for a single statement is:

The general form of the while loop for a block of statements is:

The expression can be any valid C++ language expression including the value of a variable, an unary or a binary expression, or the value returned by a function. The statement can be single or compount statement.

The statement will be executed repeatedly, as long as the expression is true (i.e., as long as expression has a non zero value). statement must include some features that eventually alters the value of the expression, thus providing a stopping condition for the loop.

Program to compute a sum of consecutive integers, i.e., the program to compute the sum 1 + 2 + 3 + .. + n, for an integer input n

You may also like...

5 Responses

  1. Frank Perrett says:

    I’m not that much of a internet reader to be honest but your sites really nice, keep it up!

    I’ll go ahead and bookmark your website to come back in the future.
    All the best

  2. Surya says:

    the final program will run forever. You aren’t incrementing variable ‘i’ inside the while loop so ‘i’ will never be <= 'n'.

  1. October 5, 2013

    […] while loop statement […]

Leave a Reply

Your email address will not be published. Required fields are marked *