I don't understand bool true and false

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 is the conditional type in c++. It is primarily used in control flow statements.
http://www.cplusplus.com/doc/tutorial/control/

Example:
1
2
3
4
5
6
7
8
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.

Take this as an example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>

using namespace std;

bool isEven(int n) //This checks whether a given number is even or not
{
    if(n%2 == 0)
        return true; //By true we mean its even
    else
        return false; //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;
}
in for example a game loop
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

bool runnng == true;

int main()
{
 while (running == true)
{
 //game stuff like updating screen
 if (ESC KEY pressed)
 {
  running == false;//ends loop and program
 }
}
 return 0;
}


EDIT: just relised i posted basicly the same thing kevinjt2000 said
so hears another example:

1
2
3
4
5
6
bool input;
cin >> input;
if (input == true)
     cout <<"true you said";
else
     cout <<"false your input was!";
Last edited on
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
On line 3 and 12 of tomdacat's first example change == to =.

The assignment operator (=) http://en.wikipedia.org/wiki/Assignment_operator_(C%2B%2B)
The comparison operators (==, !=, <, >, <=, >=) http://en.cppreference.com/w/cpp/language/operator_comparison

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.
Last edited on
Topic archived. No new replies allowed.