Bool

closed account (iAk3T05o)
Can someone please explain bool and bool functions.
I can only use if/else/switch statements but i'm seeing bool this/that in my tutorial and i don't get it.
Thanks.
bool is a short name for boolean which refers to true/false, therefore bool functions are functions that return true/false.
closed account (iAk3T05o)
So how would you use it in a program?
1
2
3
4
5
bool isTrue = false;
if(isTrue)
{
    //do something
}
Anywhere you need a true or false in your program...your question is not very specific which leads me to believe that you haven't asked the question you came here to ask.

bool is a variable type just like int, long, char,... so you use it just as you would any of those other variable types.
Most likely, you would have a check somewhere in your program to see if something has failed, or a condition is fulfilled. For example, you might have a function to check to see if a string is null-terminated, so you could do it like this:
1
2
3
4
5
6
7
8
9
10
11
// returns true/false
bool isNullTerminated(const char* str, int len) {
    // repeat for every character in the string
    for (int i = 0; i < len; ++i) {
        // if the string has a NULL character somewhere
        if (str[i] == '\0')
            return true;  // say, 'yes it does'
    }
    // we have run out of characters, therefore it isn't NULL terminated
    return false;
}
closed account (iAk3T05o)
@dash: so
1
2
3
4
else if (!isTrue)
{
//do blah
}

is like
1
2
3
4
5
int a = 2;
if (a != 2)
{ 
//do something
}
Last edited on
closed account (iAk3T05o)
What of bool functions
They can be used the same way.

1
2
3
4
5
6
7
8
9
10
11
12
13
bool isTrue() 
{
    bool a; 
    // do something with a;

    return a;
}

if(isTrue())
{
     // do something
} 
Topic archived. No new replies allowed.