Introduction to Programming

If () Then Else Block

Lesson 6: Conditional Statements

What is the If () Then Else Block?

The If () Then block is great, but what if you want your program to do something different when the condition is false? That's where the If () Then Else block comes in. It handles both cases — when the condition is true and when it is false.

The If () Then Else block checks a condition. If the condition is true, the first set of blocks runs. If the condition is false, the second set of blocks (the 'else' part) runs instead.
PictoBlox If () Then Else block from the Control palette showing two sections — one for when condition is true and one for when it is false

If () Then vs If () Then Else

Comparing the Two Blocks

BlockWhen Condition is TrueWhen Condition is False
If () ThenRuns the body blocksSkips everything, moves on
If () Then ElseRuns the 'then' blocksRuns the 'else' blocks instead

Example: Pass or Fail

Let's build a script that asks the user for a score, then tells them whether they passed or failed. A score of 50 or above is a pass.

  • Ask the user 'What is your score?' and save the answer to a variable called Score.
  • Add an If () Then Else block.
  • Set the condition to (Score ≥ 50).
  • In the 'then' section, add Say (Congratulations! You passed!) for 3 seconds.
  • In the 'else' section, add Say (Sorry, you failed. Try again!) for 3 seconds.
  • Click the green flag and enter a score to test both outcomes.
Block coding script showing when flag clicked, ask for score, set Score to answer, if Score >= 50 then say Congratulations You passed else say Sorry you failed
If () Then Else blocks can also be nested inside each other to handle multiple conditions. This is how programs make complex decisions!

Subscribe to our newsletter.

Get updates to news and events.