+ 1
Explain me boolean variable
4 Respostas
+ 11
C++ Keywords: bool. The Boolean data type is used to declare avariable whose value will be set as true (1) or false (0). To declare such a value, you use the bool keyword. The variable can then be initialized with the starting value.
+ 8
Boolean variables are used to store conditions, which can only either be true, or false. E.g.
bool isRaining = true;
You can then use this variable to evaluate conditional statements:
if (isRaining)
{
// do something
}
+ 2
booleans are a type of variable that is either true or false. true and false can be represented in different ways such as yes or no, 1 or 0, on or off.
They are used in conditional statements so your program will do different things based on the state it's in.
for example, "if I'm hungry, I will eat" is a conditional expression. the boolean is "I'm hungry". There is only yes or no. the statement then has two states. I am hungry (I'm hungry is true), then I will eat. the other is I'm not hungry (I'm hungry is false), then I will not eat.
booleans can then be combined to create boolean expressions. expressions give your code even more power by allowing you to run code in even more specific situations. for example, on a hot and sunny day, you may want to go swimming. "If it's hot and sunny, then I will go swimming". the expression is "it's hot and sunny" which is the two booleans "it's hot" and "it's sunny" combined with an "and" boolean operator. the "and" boolean operator says that both booleans need to be true in order for the entire expression to be true.
I won't delve into operators too much, but Google boolean operators and boolean expressions on your own time
0
bool gameOver = false;
if(gameOver == false){
cout << "the game isn't over";
}