If Statement

From Chilipedia
Revision as of 19:36, 13 June 2018 by Rudra (Talk | contribs) (Created page with "If statements are a way to make decisions in your program. e.g., if the player presses Space Bar, make the player jump or IF the health is 0, end the game. == Using If State...")

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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

Using If Statements

When making a decision in your program, two things need to happen

  1. You need to evaluate the condition.
  2. There should be a set of instructions(code) to execute based on that 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 followed by the block of code (surrounded by {}) that needs to be executed if that condition is 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 set PlayerHealth to 50. And somewhere in our game, we want to check if the player is still alive, if so we do some shit.

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

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

If statement is controlled by the value inside () - Only when it is true, is your code executed.

But what if you want something to happen if the condition is not true, you stick the else keyword at the end:

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

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
        }
}

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 this part of memory. If you care about performance(you should you are using c++) you should know that this jumping around does carry some overhead, very fast code does try to avoid if statements in clever ways. If you are just starting out, you don't need to worry about all this.

Tutorial Videos

Beginner C++ Game Programming Tutorial 3

See Also

https://www.youtube.com/watch?v=qEgCT87KOfc&t=904s

http://www.cplusplus.com/doc/tutorial/control/

https://www.tutorialspoint.com/cplusplus/cpp_if_else_statement.htm

https://www.cprogramming.com/tutorial/lesson2.html