C++ Booleans C-PLUS-PLUS
- C++ Introduction
- C++ Syntax
- C++ Output (cout <<)
- C++ Comments
- C++ Variables
- C++ User Input
- C++ Data Types
- C++ Operators
- C++ Strings
- C++ Math
- C++ Booleans
- C++ If Else
- C++ Switch
- C++ While Loop
- C++ Do/While Loop
- C++ For Loop
- C++ Break and Continue
- C++ Arrays
- C++ References
- C++ Pointers
- C++ Functions
- C++ Functions Parameters
- C++ Function Overloading
- C++ OOP
- C++ Classes and Objects
- C++ Class Methods
- C++ Constructors
- Destructors in C++
- C++ Access Specifiers
- C++ Encapsulation
- C++ Inheritance
- C++ Polymorphism
- C++ Files
- C++ Exception Handling
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
A 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