Boolean Question

Would someone please clarify why entering 1 would return true and 0 would return false?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<iostream>
using namespace std;

int main() {
int x = -1;
cout << "Enter a 0 or a 1 from the keyboard: ";
cin >> x;
if (x)
   cout << "true" << endl;
else
   cout << "false" << endl;
   
  return 0;

}
Because that's the way the language works. The expression inside an if statement is converted to bool. For integral types, zero converts to false and anything else converts to true.
got it thanks!
Topic archived. No new replies allowed.