Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Booleans | Conditional Statements
Introduction to C++

book
Booleans

Let’s discuss boolean (or logical) data type in more detail. The variable type of bool can take only two values - True (1) or False (0). The boolean can be used when we want to check some logical conditions in our code. For example, you want to know if the variable x is bigger than y to use this information by looking for the difference between them.

For this reason, there are some logical operators you can use in comparison:

Use them in boolean expressions, which return the output as a boolean value: True. If the condition holds or False otherwise.

For example:

int x = 5;
int y = 4;
cout << (x > y) << endl; // will return true, because 5 is higher than 4
cout << (x <= 5) << endl; // will return true, because x is equal to 5
cout << ("hello" == "Hello"); // will return false, because these string aren't the equal
12345
int x = 5; int y = 4; cout << (x > y) << endl; // will return true, because 5 is higher than 4 cout << (x <= 5) << endl; // will return true, because x is equal to 5 cout << ("hello" == "Hello"); // will return false, because these string aren't the equal
copy

The program returned 1 where the statement was true and 0 where was false.

question-icon

Check if the variable x is equal to the variable y and if x is greater than 450.

#include
using namespace std;

int main() {
    int x = 24 * 25;
    int y = 19 * 32;
    cout << (x
y) << endl;
    cout << (x
450);
    return 0;
}
0
1

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 3. Kapittel 1
some-alt