If Statement

From Chilipedia
Jump to: navigation, search

If statements are a way to make decisions in your program. e.g., if the player presses the space bar, make the player jump, or if health is 0, end the game.

Using If Statements

Basics

When a decision is made at an if statement in your program, two things happen:

  1. A condition is evaluated
  2. A set of instructions (code) is executed (or skipped) based on the above condition
if(condition == true)
{
    // Do something
}

We use the if keyword followed by opening and closing parentheses () which contain the condition we want to evaluate. The condition is followed by a block of code (surrounded by braces {}) that is executed if that condition evaluates to true.

int PlayerHealth = 50;
if(PlayerHealth > 0)        // Check the condition, if it is evaluated to be true, the code inside the {} is executed 
{
    // If true, execute some code 
}

Here we check if the player is still alive, and if so we do some shit.

A bool can also be used directly as a condition. The code above can be written as:

int playerHealth = 50;
bool isAlive = (playerHealth > 0);
if(isAlive)
{
    // Do stuff 
}

An if statement is controlled by the value inside the parentheses ()--only when it is true is your code executed.

Else Statement

But what if you want something to happen only if the condition is not true? Then you stick the else keyword after the if code block:

int playerHealth = 0; 
bool isAlive = (playerHealth > 0);
if(isAlive) // Evaluated to false
{
    // Will not execute
}
else // Will execute
{
    // You dead 
}

Nesting

You can even stick if conditions inside if conditions, this is called 'nesting':

std::string Weapon = "Gun"; 
if(Weapon == "Gun") 
{
    // Pew Pew Pew 
}
else 
{
    if(condition)
    {
        // do some stuff 
    }
    else
    {
        // run this block of code
    }
}

Chaining

You can also chain multiple if statements together using the else keyword. The code above can be simplified and written as:

std::string Weapon = "Gun"; 
if(Weapon == "Gun") // Evaluate Condition 
{
    // Pew Pew Pew 
}
else if(condition) // *Only* if the first if condition fails, evaluate this, otherwise skip 
{
  // do some stuff   
}
else 
{
    // run this block of code if everything else fails 
}

C++ gives us the else if keyword that allows us to keep checking for different conditions. You can use as many else if as you want, but remember it will only run if the condition before it has failed.

What's really going on here tho

When you run your program all your C++ code is converted to machine code all of which is loaded into memory. When you use an if statement, you're effectively telling your computer - if this condition is true then jump to another location in memory, but if it is false, just continue executing the next instruction in memory.

If you care about performance, you should know that this jumping around (called branching) can carry some overhead. The CPU has highly specialized circuits that attempt to predict which branch of code the execution will follow. For many situations these circuits are very successful in making the predictions, and thus the cost of branching is effectively zero. But in some situations it is very difficult for the branch predictor to accurately predict which branch is going to be taken, and in these situations highly optimized code tries to avoid if statements in clever ways.

If you are just starting out, you don't need to worry about all this.

Planet Chili Tutorial Video

Other Tutorials

See Also