Conditionals
if, else if and else choose between paths.
About this topic
Conditionals let a program make decisions by running different code depending on whether a condition is true or false. The basic form is if, optionally with else if and else. The condition is a boolean expression, such as age is at least 18.
The computer checks conditions from top to bottom and runs only the first block whose condition is true. For example, with marks, if marks is at least 90 then grade A, else if at least 80 then grade B, else grade C. Conditions can be combined with AND, OR and NOT, and conditionals can be nested inside each other, although too much nesting makes code hard to read.
Conditionals appear everywhere: a game checks if health is zero, a phone checks whether the password is correct, a thermostat checks if the temperature is below the target. Common errors include using one equals sign instead of two, wrong ordering of conditions so a general case blocks a specific one, and forgetting the else for unexpected input. Test boundary values such as exactly 90, because many bugs live at the edges.
Key ideas
- Condition is true or false
- Indentation matters in Python
- Nested conditions
Quick check
Use cases: where Conditionals helps
- Homework: review the key idea behind Conditionals before you start a problem set.
- Exam revision: use the quick check to test whether you can recall Conditionals without notes.
- Visual learning: play with the interactive lab to see how Conditionals changes when you adjust its inputs.
- Teaching a friend: explain Conditionals in your own words, then compare your version with the key ideas.
Computing ideas run phones, search engines, games and the wider internet.
Frequently asked questions
What is Conditionals?
if, else if and else choose between paths.
What will I learn in Conditionals?
Condition is true or false. Indentation matters in Python. Nested conditions.
Is Conditionals part of Computer Science?
Yes. It is one of 80 Computer Science topics on LearnViz HighSchoolEdu, with 3 key ideas, a live animation and a quick check.
Does the Conditionals lab work on a phone or tablet?
Yes. The animation resizes to fit your screen and supports touch, so you can drag, tap and slide with your finger.
How should I study Conditionals?
Read the summary and key ideas first, play with the interactive lab to see the idea in motion, then answer the quick check. If you get a question wrong, go back to the key idea it tests.
Do I need an account, and is my progress saved?
No account is needed. Marking a topic as learned is saved in your own browser, so it stays on that device and is not sent to a server.