Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Boolean Data Type | Other Data Types and Concepts
C++ Data Types

bookBoolean Data Type

Boolean (bool) is a data type that can take one of two values: true or false. Syntax:

bool.h

bool.h

copy
1
bool alive = true;

They are mostly used in comparison operators. Those operators (==, !=, <, >, >=, <=) return bool.

Note

std::cout prints booleans as numbers, 1 if the boolean is true, and 0 if the boolean is false.

main.cpp

main.cpp

copy
123456789101112
#include <iostream> int main() { bool istrue = true; bool obv = (5 > 0); bool wrong = (2 * 2 == 5); std::cout << istrue << std::endl; std::cout << obv << std::endl; std::cout << wrong << std::endl; }

You can also flip the value of a boolean using the ! operator:

main.cpp

main.cpp

copy
123456789
#include <iostream> int main() { bool istrue = true; std::cout << istrue << std::endl; std::cout << !istrue << std::endl; }

And most frequently, they are used as conditions in if/while/... statements.

main.cpp

main.cpp

copy
123456789101112
#include <iostream> int main() { bool b = true; if (b) std::cout << "b is true" << std::endl; if (1 > 0) std::cout << "1 is greater than 0" << std::endl; }

The size of a bool is 1 byte. Yes, it would fit in 1 bit of memory, but you can't store a variable of size less than 1 byte (8 bit).

question mark

Choose the INCORRECT statement

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 4. Kapitel 1

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Awesome!

Completion rate improved to 4.35

bookBoolean Data Type

Svep för att visa menyn

Boolean (bool) is a data type that can take one of two values: true or false. Syntax:

bool.h

bool.h

copy
1
bool alive = true;

They are mostly used in comparison operators. Those operators (==, !=, <, >, >=, <=) return bool.

Note

std::cout prints booleans as numbers, 1 if the boolean is true, and 0 if the boolean is false.

main.cpp

main.cpp

copy
123456789101112
#include <iostream> int main() { bool istrue = true; bool obv = (5 > 0); bool wrong = (2 * 2 == 5); std::cout << istrue << std::endl; std::cout << obv << std::endl; std::cout << wrong << std::endl; }

You can also flip the value of a boolean using the ! operator:

main.cpp

main.cpp

copy
123456789
#include <iostream> int main() { bool istrue = true; std::cout << istrue << std::endl; std::cout << !istrue << std::endl; }

And most frequently, they are used as conditions in if/while/... statements.

main.cpp

main.cpp

copy
123456789101112
#include <iostream> int main() { bool b = true; if (b) std::cout << "b is true" << std::endl; if (1 > 0) std::cout << "1 is greater than 0" << std::endl; }

The size of a bool is 1 byte. Yes, it would fit in 1 bit of memory, but you can't store a variable of size less than 1 byte (8 bit).

question mark

Choose the INCORRECT statement

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 4. Kapitel 1
some-alt