Skip to content

🤔 Part 3 - If Statements


Summary

If statements are a type of conditional logic. They check whether a condition is true and run one block of code if it is, and another block if it is not.


Syntax

if (condition)
{
    // Run this code
}
else
{
    // Run this code instead
}

Example

if (Input.GetAxisRaw("Horizontal") != 0)
{
    MovePlayer();
}
else
{
    return;
}
This checks for keyboard input. If the player presses A, D, Left Arrow, or Right Arrow, the player moves. Otherwise, the script returns without moving the player.

If statement example


Checklist

  • I can explain what an if statement does.
  • I can read basic if/else logic in a script.