Can someone explain to me what's the use of bool true and false and when to use it. It's my first day reading the tutorials and it's really confusing. I never did programming. Thank you.
bool isGameRunning = true;
while(isGameRunning)
{
//play game stuff here
//check if the game is over and set isGameRunning to true or false accordingly
}
//the code will continue here, only after isGameRunning is set to false
//or if a break control statement is encountered
Of course bools are more versatile than just this. Really you can use them for anything that toggles on and off in a program. It is up to you.
Bool (true or false) is used mainly in loop or if/else conditions. Or maybe you want to have a function that checks something and returns 'true' if its right or 'false' if its wrong.
#include <iostream>
usingnamespace std;
bool isEven(int n) //This checks whether a given number is even or not
{
if(n%2 == 0)
returntrue; //By true we mean its even
elsereturnfalse; //By false we mean its odd
}
int main()
{
int num;
cout << "Enter a number: ";
cin >> num;
if(isEven(num)) //If true
cout << "Number is even." << endl;
else //If false
cout << "Number is odd." << endl;
return 0;
}
I'm not sure why many new comers have trouble understanding booleans. A boolean represents a true or false value. Every software you have ever used will have depend on some boolean (true or false) value in order to stay running.
On the programming level, booleans are the basics of most control structures you will ever come across. From if statements to for loops, you need some sort of Boolean expression to make them do anything.
The following are all valid boolean expressions in c++:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
while (something is true)
{ /*execute body*/ }
while (something is not true)
{ /*execute body*/ }
if (something is true)
{ /*execute body*/ }
if (something is not true)
{ /*execute body*/ }
for (expression; something is true; expression)
{ /*execute body*/ }
for (expression; something is false; expression)
{ /*execute body*/ }
All those conditions have to be true in order for the control structure associated with them to execute any statements in the body
And comparing a boolean with == true is OK to do, but to me it just looks odd when it can be left out.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
bool example = true; //try also setting this to false and running the code
if(example)
{
std::cout << "example is true" << std::endl;
}
else
{
std::cout << "example is false" << std::endl;
}
if(example == true)
{
std::cout << "a true thing equaled a true thing. wow. simply amazing." << std::endl;
}
else
{
std::cout << "a false thing did not equal a true thing. wow. simply amazing." << std::endl;
}
Also the second example is not a great one either on how to use std::cin. Typing "true" at this blank prompt will actually store a false value to the bool variable input. (blank prompt: std::cin at a blank screen makes the user think the program is doing something, but in reality it is actually waiting on the user and just did not tell the user with std::cout first)
Programs should not have to ask for boolean inputs with std::cin. Programs might ask for yes or no inputs or maybe even checkboxes, but usually not plain boolean inputs.