Page 1 of 1

## The Boolean Type Rate Topic: 1 Votes //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=251625&amp;s=cc49665a03c1c351f1b58d33abc466a3&md5check=' + ipb.vars['secure_hash'], cur_rating: 5, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Gungnir

Reputation: 152
• Posts: 527
• Joined: 21-May 11

Posted 16 October 2011 - 09:40 PM

The Boolean Type

I've seen a few threads around the C/C++ section where people have been having trouble with with bool type variables. This tutorial will explain the basics of the bool type and explain to you how to use it with great efficiency.

So what does boolean mean? Well basically it means 'true of false', and that's exactly what the bool type will allow you to evaluate! Let's look at how to initialize a boolean variable.

First of all, we need to assign the variable with an identifier, or a 'name', if you will. This is no different to any other type. Let's call it Gungnir.

```bool Gungnir;
```

Now, it has to be assigned a value before we can use it. This can be done a few ways.

One way is to assign it a value when we're initializing the variable. This is done the following way.

```bool Gungnir = true;
//OR
bool Gungnir(true);

```

Quote

You can use either assignment technique, they both do the same thing, except it's important to note that (like all other types) AFTER the declaration of the variable you CANNOT use parenthesis (curved brackets) to assign a value. for example
```bool Gungnir(true); //CORRECT

bool Gungnir;
Gungnir(true); //INCORRECT

```

An alternative way that we can initialize a boolean variable is by relation to another value or variable.

```bool notGungnir(false);
bool Gungnir(!notGungnir);

//OR simply

bool Gungnir(!false);

```

Using any of these, the variable Gungnir of the type bool will be set to true.

That's all well and good, but what can we do with a boolean variable?

Well it's important to note that true and false are boolean constants. Their values will never change. A boolean variable on the other hand, can switch between true and false at any time.

Let's look at a few examples of what we can do with booleans.

First of all, lets look at a boolean type function.

Boolean Functions

```bool setGungnir(bool value)
{
return value;
}

bool Gungnir = setGungnir(true)

```

This is an oversimplified function, basically it just gets a boolean value as a parameter and returns that value, which is then used by Gungnir to assign it to either true or false. There are times where a function such as this would be invaluable (such as when dealing with classes), but for this example it doesn't do a hell of a lot. If you're familiar with functions you can probably see how a bool function would be invaluable to your program. Something like this would be very useful.

```bool did_he_died(int param1, int param2)
{
//Algorithm to check if he died based on the parameters
}

```

You might also check whether something is locked, or whether two objects are colliding, the possibilities aren't exactly endless but they come pretty close.

Okay, so moving on. Earlier we assigned a bool called Gungnir with the value of true. Now we're going to talk about the significance of this.

Firstly, let's recap, and then we'll learn about what we can do with our variable.

Boolean Conditions

```bool Gungnir(true);

/* Let's use our variable in a while statement. This will keep
looping until Gungnir is equal to false. This gives us
remarkable power over loops, because we can choose to stop
the loop at any time based on user input or other triggers. */

while(Gungnir == true)
{
//...Do stuff while Gungnir = true
}

/* Or an if statement. This will allow us to start this if
statement when we feel like it, based on the state of the
boolean Gungnir.                                          */

if(Gungnir)
{
//...Do stuff if Gungnir = true
}

```

We've barely scraped the surface of the power that is the boolean. Much of its functionality is reliant on knowledge of other programming concepts. However, by now you should have a good idea on how to use these tools to create more comprehensive and efficient programs.

If you have any further questions don't hesitate to ask, I'd be more than happy to help

Is This A Good Question/Topic? 1

## Replies To: The Boolean Type

### #2 hulla

• Writing Lines

Reputation: 49
• Posts: 733
• Joined: 05-March 11

Posted 17 November 2011 - 03:37 AM

On your second paragraph you made a typo here: true of false. It's supposed to be true or false, isn't it?

This post has been edited by hulla: 17 November 2011 - 03:37 AM