Introduction to Programming

If () Then Block

Lesson 6: Conditional Statements

Making Decisions in Code

Programs become truly powerful when they can make decisions. The If () Then block allows your program to execute a set of blocks only when a certain condition is true — just like saying 'If it is raining, then take an umbrella.'

The If () Then block checks a condition. If the condition is true, the blocks inside it run. If the condition is false, those blocks are skipped entirely.
PictoBlox If () Then block from the Control palette with an empty condition slot and space for blocks inside

Structure of the If () Then Block

The If () Then block has two parts: the condition slot (where you place a relational or Boolean operator block) and the body (where you place the blocks to run if the condition is true).

Parts of the If () Then Block

PartDescription
ConditionA Boolean expression that is either true or false — usually a relational operator block
BodyThe blocks that run only if the condition is true

Example: Checking a Score

Let's look at a simple example. We will check if the player's score is greater than 10, and if so, have Tobi say 'You are doing great!'

  • Create a variable called Score and set it to a value.
  • Add an If () Then block from the Control palette.
  • Place the condition (Score > 10) inside the condition slot.
  • Inside the body, add a Say () for () seconds block with the message 'You are doing great!'
  • Click the green flag — Tobi will only speak if the Score is greater than 10.
Block coding script showing when flag clicked, set Score to 15, if Score greater than 10 then say You are doing great for 2 seconds
The If () Then block only runs its body when the condition is true. If the condition is false, the program simply moves on to the next block without doing anything.

Subscribe to our newsletter.

Get updates to news and events.