Difference between revisions of "If Statement"

From Chilipedia
Jump to: navigation, search
(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...")
 
Line 1: Line 1:
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.  
+
If statements are a way to make decisions in your program. e.g., <code>if</code> the player presses the space bar, make the player jump, or <code>if</code> health is 0, end the game.  
  
 
== Using If Statements ==
 
== Using If Statements ==
When making a decision in your program, two things need to happen  
+
When a decision is made at an if statement in your program, two things happen:
# You need to evaluate the condition.
+
# A condition is evaluated
# There should be a set of instructions(code) to execute based on that condition.
+
# A set of instructions (code) is executed (or skipped) based on the above condition
 
<source>
 
<source>
 
if(condition == true)
 
if(condition == true)
Line 11: Line 11:
 
}</source>
 
}</source>
  
We use the <code>if</code> keyword followed by opening and closing parentheses <code>()</code> which contain the condition we want to evaluate followed by the block of code (surrounded by <code>{}</code>) that needs to be executed if that condition is true.  
+
We use the <code>if</code> keyword followed by opening and closing parentheses <code>()</code> which contain the condition we want to evaluate. The condition is followed by block of code (surrounded by braces <code>{}</code>) that is executed if that condition evaluates to <code>true</code>.  
 
<source>
 
<source>
 
int PlayerHealth = 50;
 
int PlayerHealth = 50;
Line 19: Line 19:
 
}
 
}
 
</source>
 
</source>
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.  
+
Here we check if the player is still alive, and if so we do some shit.  
  
A boolean can also be used as a condition. The code above can be written as:  
+
A <code>bool</code> can also be used directly as a condition. The code above can be written as:  
 
<source>
 
<source>
 
int playerHealth = 50;
 
int playerHealth = 50;
Line 30: Line 30:
 
}
 
}
 
</source>
 
</source>
If statement is controlled by the value inside <code>()</code> - Only when it is true, is your code executed.  
+
An <code>if</code> statement is controlled by the value inside the parentheses <code>()</code>--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:  
+
But what if you want something to happen only if the condition is ''not'' true? Then you stick the <code>else</code> keyword after the <code>if</code> code block:  
 
<source>
 
<source>
 
int playerHealth = 0;  
 
int playerHealth = 0;  
Line 56: Line 56:
 
{
 
{
 
     if(condition)
 
     if(condition)
        {
+
    {
            // do some stuff  
+
        // do some stuff  
        }
+
    }
 
     else
 
     else
        {
+
    {
            // run this block of code
+
        // run this block of code
        }
+
    }
 
}
 
}
 
</source>
 
</source>
  
The code above can be simplified and written as:  
+
You can also chain multiple <code>if</code> statements together using the <code>else</code> keyword. The code above can be simplified and written as:  
 
<source>
 
<source>
 
std::string Weapon = "Gun";  
 
std::string Weapon = "Gun";  
Line 85: Line 85:
  
 
== What's really going on here tho ==  
 
== 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.  
+
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 <code>if</code> 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.
  
== Tutorial Videos ==  
+
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 highly 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.
 +
 
 +
== Tutorial Video ==  
 
[[Beginner C++ Game Programming Tutorial 3]]
 
[[Beginner C++ Game Programming Tutorial 3]]
  
 
== See Also ==  
 
== See Also ==  
https://www.youtube.com/watch?v=qEgCT87KOfc&t=904s
 
 
 
http://www.cplusplus.com/doc/tutorial/control/
 
http://www.cplusplus.com/doc/tutorial/control/
  
Line 98: Line 100:
  
 
https://www.cprogramming.com/tutorial/lesson2.html
 
https://www.cprogramming.com/tutorial/lesson2.html
 +
 +
https://www.youtube.com/watch?v=qEgCT87KOfc&t=904s

Revision as of 22:12, 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 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 highly 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.

Tutorial Video

Beginner C++ Game Programming Tutorial 3

See Also

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

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

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

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