Skip main navigation

What are conditions?

A condition is a statement that can be either true or false, and programmers can use conditions to trigger actions. Read more about them here.

Conditions are a key part of programming, but are also something that you probably use every day without thinking about it. A condition is a statement that can be either true or false, and programmers can use conditions to trigger actions.

Think about the number of actions you’ve taken today based on whether a particular condition was met or not. You probably got out of bed this morning, but only when it reached a certain time. You probably ate something, but only when you felt hungry. Each time, you acted based on whether a particular condition was met or not — you used a condition to trigger when something happened.

You can think of conditions in terms of a statement which can be true or false, or in terms of a question that you can answer with a yes or no:

True or false statement Yes or no question
It is past 6:30am. Is it past 6:30am?
I am hungry. Am I hungry?

Of course, there are many questions in everyday life which do not have a yes or no answer but still help you to make decisions and take actions, such as:

  • What chocolate bar would I like?
  • What’s my favourite colour?
  • What’s the weather today?

However, conditions don’t work that way; conditions must be phrased as a question with just two options, yes or no. You need to make sure that you write an appropriate condition (or several) in your program, otherwise it won’t work.

A robot being asked the question “What’s the weather today?” and giving an error.

So instead of asking “What’s the weather today?” you could ask yes or no questions (or make true or false statements) to help you decide what to wear instead:

  • Is it sunny?
  • Is it raining?

An animation of a robot by a window. It is raining outside. The condition "Is it sunny?" appears, and the robot responds "FALSE". The condition "Is it raining?" appears. The robot responds "TRUE" and puts up an umbrella.

Using comparisons in conditions

You can also create conditions that compare two things, such as asking if one thing is greater than (>) or less than (<) another. Instead of asking “Is it hot today?” — the answer to which is subjective — you might instead ask “Is today’s temperature greater than (>) 25°C?”

Discussion

In the comments section, share more examples of conditions that you might consider in everyday life.

This article is from the free online

Introduction to Programming with Scratch

Created by
FutureLearn - Learning For Life

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now