Hi everyone

I'm studying the part about while-statement but I don't understand the explanation in the book really well and the example doesn't work for me so I can't see it with my own eyes how it works.
Can someone explain what the while-statement means and does? Also can you give an example that might work?

A while statement repeats something if a confition is true, i'm not a C++ programmer but if you wanted to repeat something forever you could say (this is in psudo code not C++):

x = 1
while(x == 1){
    do something
    }

So a while loop can repeat lines of code if something is true then it will stop if the condition isn't true.

For more info goto: http://www.youtube.com/watch?v=sPpZCOCVtec

commented: Thank you, the shortes but clearest explanationµ +0

The while statement is a loop that contininues to execute until some condition is met. For example consider vriable named x in this simple example

int x = 0;
while (x < 10)
{
    x++;
}

In the above the loop will continue until the value of x reaches the value of 10. Notice that the value of x is incremented inside the loop. If x was not incremented inside the loop, the loop would be considered an infinite loop because the value of x would never change. So there must be something inside the loop that will eventually make it stop.

Another way to make the loop stop is to use the break statement

int x = 0;
while( x < 10)
{
   if( x > 2)
      break;
   x++;
 }

I know that is kind of a stupid program but it illustrates how to stop the loop early.

That really all there is to a while loop -- nothing more difficult then that.

Technically, the while() loop is what is known as an 'indefinite iteration', which is to say that it repeats the body of the loop zero or more times, with the number of repetitions being unknown at the time the loop is begun. It repeats based on a condition which holds true, and stops repeating when the condition no longer holds. The form of the while() loop in EBNF looks like this:

while-loop ::= 'while' '(' <condition> ')' <body>

body ::= <statement>
       | '{ <statement>+ '}'

I doubt that this is any less confusing, however. The practical upshot of this is that the while loop has a conditional, similar to an if() statement, except that instead of selecting the body that follows it once it repeats until something changes the condition to false. So, let's say you want to read in something from the console, and keep repeating echoing back a line number and the string until it gets to an end-of-file marker (Ctrl-Z in Windows, Ctrl-D in Unix) is found, then you would write something like this:

#include <iostream>
#include <iomanip>
#include <string>

int main()
{
    int count;

    while (!std::cin.eof())
    {
        std::string line;
        std::getline(std::cin, line);
        std::cout << std::setw(4) << count << ": " << line << std::endl;
        count++;
    }

    return 0;
}

Now, you may wonder what the point of this program is, as it doesn't seem to be very useful; but this is actually a rather neat little utility of a type called a filter, which takes the contents of a redirected file and returns it, modified, to standard output. So, if you were to save this file as enumerate.cpp, and compiled it, then run it in the shell (or command prompt) as

enumerate < enumerate.cpp

you would get a listing of the program with line numbers added to it. Anyway, it should make the behavior of the while() loop a little clearer.

The while statement is a loop that contininues to execute until some condition is met.

I think you meant to say a while statement is a loop that continues to execute while some condition is met.

"while" will test a condition before it enters into its code block/body, so it may never ebter it at all. It's usefull if you need to only enter into a loop if its condition is met.

If you needed to guarantee loop for example to execute its body at least once you would use "do" and test its condition at the end of its body with "while"

//this operation might not be carried out at all
while(condition){
    //operation;
}

//this operation is guaranteed to be carried out at least once
do{
    //operation;
}while(condition);

//you can exit a loop at any time with "break"
//here is an imaginary endless loop which exits without its initial condition being false
while(1){
    if(4 == 4){
        break;
    }
}   

The syntax of a while loop in C++ is :

while(condition)
{
   statement(s);
}

Here statement(s) may be a single statement or a block of statements. The condition may be any expression, and true is any non-zero value. The loop iterates while the condition is true.

When the condition becomes false, program control passes to the line immediately following the loop.

Example :

i = 1;

while(i<=5)
{
   cout<<i;
}

Output : 12345

Your example is incomplete/incorrec -- where is the variable i changed? Answer: it isn't, so the output you posted is wrong. The correct output is "111111111..."

The syntax of a while loop in C++ is :

while(condition)
{
   statement(s);
}

Here statement(s) may be a single statement or a block of statements. The condition may be any expression, and true is any non-zero value. The loop iterates while the condition is true.

When the condition becomes false, program control passes to the line immediately following the loop.

Example :

i = 1;
while(i<=5)
{
   cout<<i;
   i = i+1;
}

Output : 12345

Thanks for the help, I understand it now.

-Question solved-

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.