loops conditions

Hi guys I thought that when using a loop or a test condition I thought you could only test if a condition is true or not for example if(x == 20) or while(y==true) but I didn't know you could you statements in it like cin >> x; how come this is allowed?? coming from Java it does not make a whole lot of sense to me,could someone explain why this is allowed or how it works thanks

1
2
3
4
5
6
7
8
9
10
  #include <iostream>
using namespace std;

int main()
{
  int x;
  while(cin >> x){

  }
}
cin >> x has a return value.
http://www.cplusplus.com/reference/istream/istream/operator%3E%3E/
The return value is the istream object.

The istream object has its bool() operator applied to get a true/false result.
http://www.cplusplus.com/reference/ios/ios/operator_bool/

https://stackoverflow.com/questions/4600295/what-is-the-meaning-of-operator-bool-const
Last edited on
correction: operator>> returns a REFERENCE to an istream object.

Last edited on
Topic archived. No new replies allowed.