Conditional Statements

Without conditional statements, you can't really do anything useful. Everything would have to take the same path all the time.

Warning: This tutorial contains more code than more of the previous ones.

Bool

This is really a variable type, but you'll probably see it at some point, as it's pretty common. It has two possible values, true and false.1

If, Else

The most basic conditional statement is an if statement. If the condition is true, then the next line of code executes.

#include <iostream>
 
int main()
{
    int var1 = 2;
    int var2 = 4;
 
    if ( var1 * var2 == 8 )
        std::cout << "var1 * var2 == 8" << std::endl;
}

You'll probably notice a couple of different things:

  • The condition is in parenthesis. You must always have at least one surrounding all of the conditions, or else you'll get a syntax error.
  • There isn't a semicolon at the end of the if statement. If you put one, it will stop basically ignore the condition, and do the next statement regardless.
  • We only have one statement after the if statement. How does the compiler know which parts to execute if the conditions are true, and which parts to execute regardless? Here's the answer: it doesn't. It only executes one statement, and that's it. To tell it to do more, you have to use braces ({}), like we do around main:
if ( var1 * var2 == 8 )
{
    std::cout << "var1 * var2 == 8" << std::endl;
    std::cout << "By the way, var1 is " << var1 << ", and var2 is " << var2 << "." << std::endl;
}

You can also have multiple conditions, using and/&& and or/||. They act just like the real words sound:

if ( var1 == 2 or var2 == 4 )
{
    std::cout << "var1 is 2, or var2 is 4. Whichever way, this gets executed." << std::endl;
}

One thing you need to keep is mind and takes a precedence over or, kinda like you multiply/divide before you add/subtract with order of operations.

If you need to do something if a condition is true, and something else if it is false, you can use else, and even chain more if statements!

if ( var1 == 2 )
{
    std::cout << "var1 is 2" << std::endl;
}
else if ( var2 == 4 )
{
    std::cout << "var2 is 4" << std::endl;
}
else
{
    std::cout << "var1 isn't 2, and var2 isn't 4." << std::endl;
}

While

A while statement is just like an if statement, with two major differences:

  1. It keeps executing the code until the condition is false.
  2. You can't add an else at the end.
#include <iostream>
 
int main()
{
    int var = 0;
    while ( var < 10 )
    {
        std::cout << "We are looping until var is 10. var is currently " << var << "." << std::endl;
        ++var;
    }
 
    std::cout << "We finished our loop, and var is now " << var << "." << std::endl;
}

For

If you take a look at the example a minute ago, you can see we start at 0, and keep going until we get to 10. This actually happens fairly often. There is a way to simplify this, too:

#include <iostream>
 
int main()
{
    for ( int i = 0; i < 10; ++i )
    {
        std::cout << "We are looping until i is 10, except with a for statement this time. i is currently " << i << "." << std::endl;
    }
    std::cout << "We finished our loop!" << std::endl;
}

One thing you may have noticed is that on the last line, we don't print the current value of the variable anymore. Why? i went out of scope, so we can't access it anymore.

Conclusion

Okay, so I probably went through that a little too fast. Play around with it for a while, and see what you can come up with. :) It'll probably make more sense, then. If not, ask on the forums. :)

Next, you get to learn about a neat class for manipulating strings, called std::string.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution 3.0 License