C++ Booleans C-PLUS-PLUS

C++ Booleans  

C++ Booleans

C++ Booleans

For this, C++ has a bool datatype, which can take the values true (1) or false (0). A boolean variable is declared with the bool keyword and can only take the values true or false:

Example

#include <iostream>
using namespace std;

int main() {
  bool isCodingFun = true;
  bool isError = false;
  cout << isCodingFun << "\n";
  cout << isError;
  return 0;
}
 

Output:

1
0

Boolean Expression

Boolean expression is a C++ expression that returns a boolean value: 1 (true) or 0 (false). You can use a comparison operator, such as the greater than (>) operator to find out if an expression (or a variable) is true.

Example

#include <iostream>
using namespace std;

int main() {
  int x = 10;
  int y = 9;
  cout << (x > y);    // returns 1 (true), because 10 is higher than 9
  return 0;
}

Output:

1

Download free E-book of C-PLUS-PLUS


#askProgrammers
Learn Programming for Free


Join Programmers Community on Telegram


Talk with Experienced Programmers


Just drop a message, we will solve your queries