25

What the difference between the following two loops and When each one will stopped ?

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main() {
    int x,y;
    while(cin >> x){
        // code
    }
    while(cin){
        cin >> y;
        //code
    }
    return 0;
}
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
Ahmed_Mohsen
  • 285
  • 1
  • 3
  • 6

4 Answers4

50

Let's look at these independently:

while (cin >> x) {
    // code
}

This loop, intuitively, means "keep reading values from cin into x, and as long as a value can be read, continue looping." As soon as a value is read that isn't an int, or as soon as cin is closed, the loop terminates. This means the loop will only execute while x is valid.

On the other hand, consider this loop:

while (cin){
    cin >> y;
    // code
}

The statement while (cin) means "while all previous operations on cin have succeeded, continue to loop." Once we enter the loop, we'll try to read a value into y. This might succeed, or it might fail. However, regardless of which one is the case, the loop will continue to execute. This means that once invalid data is entered or there's no more data to be read, the loop will execute one more time using the old value of y, so you will have one more iteration of the loop than necessary.

You should definitely prefer the first version of this loop to the second. It never executes an iteration unless there's valid data.

Hope this helps!

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
  • "and as long as a value can be read, continue looping" i.e. if reading was successful, execute the loop body. – dyp Oct 20 '13 at 21:45
  • `The statement while (cin) means "while all previous operations on cin have succeeded, continue to loop."` What about the first time through the loop? Why does it ever even start if you have not used `cin` yet? – eric Apr 23 '18 at 13:40
  • 1
    @neuronet In that case, it would be ([vacuously!](https://en.m.wikipedia.org/wiki/Vacuous_truth)) true that all previous operations have succeeded, so the condition would evaluate to true and the loop would execute. – templatetypedef Apr 23 '18 at 15:43
  • I would just be more literal: a stream can be in four possible states: `good`, `bad`, `fail`, or `eof`. By default, `cin` starts in state `good`, so the `while` block will run. For a few reasons, I would avoid explaining `while(cin)` in terms of the success or failure of previous uses of `cin`, when there was no previous use of `cin` in a program. Maybe it's a matter of taste. – eric Apr 23 '18 at 15:57
5

The difference is that if cin >> whatever evaluates to false, your second version still runs the rest of the loop.

Let's assume cin >> whatever fails. What will happen?

while(cin >> x){
    // code that DOESN'T RUN
}

while(cin){
    cin >> y;
    //code that DOES RUN, even if the previous read failed
}
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
4
while(cin >> x){
    // code
}

This reads integers until it encounters a non-integer, EOF, or other stream error. Whenever you use x inside the loop, you know it has been read successfully.

while(cin){
    cin >> y;
    //code
}

This reads integers until it encounters a non-integer, EOF, or other stream error. However, the stream is only checked before reading the integer. When you use y in the loop, you can't guarantee that it was successfully read.

paddy
  • 60,864
  • 6
  • 61
  • 103
2

cin >> x will store the input value into x.

As for while(cin), std::cin will return a boolean on whether an error flag is set. Therefore, you will continue in the while loop so long as std::cin has no error flag set internally. An error flag can be set if it finds an end of file character, or if it failed to read and store into the value.

Caesar
  • 9,483
  • 8
  • 40
  • 66