Difference between revisions of "If Statement"

From Chilipedia
Jump to: navigation, search
(Planet Chili Tutorial Video)
(Other Tutorials)
Line 95: Line 95:
  
 
== Other Tutorials ==  
 
== Other Tutorials ==  
http://www.cplusplus.com/doc/tutorial/control/
+
* http://www.cplusplus.com/doc/tutorial/control/
  
https://www.tutorialspoint.com/cplusplus/cpp_if_else_statement.htm
+
* https://www.tutorialspoint.com/cplusplus/cpp_if_else_statement.htm
  
https://www.cprogramming.com/tutorial/lesson2.html
+
* https://www.cprogramming.com/tutorial/lesson2.html
  
https://www.youtube.com/watch?v=qEgCT87KOfc&t=904s
+
* https://www.youtube.com/watch?v=qEgCT87KOfc&t=904s
  
 
== See Also ==
 
== See Also ==
 
* [[Beginner_C++_Game_Programming_Series#Topics_Covered|List of Beginner C++ Topics]]
 
* [[Beginner_C++_Game_Programming_Series#Topics_Covered|List of Beginner C++ Topics]]

Revision as of 22:23, 17 June 2018

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

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.

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 
}

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

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 the attempt to predict with 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