Conditionals & Debugging
Conditionals let a program make decisions, and debugging is how you find and fix mistakes in your code.
Reading is good — doing is better. Practice Conditionals & debugging as an interactive lesson.
Try the lessonDefinition
A conditional is a programming instruction that says 'IF something is true, THEN do this, OTHERWISE do that.' Debugging means reading through your code carefully to find errors (called bugs) and fixing them so the program works the way you want.
Remember the rule
IF (condition is true) → do something / ELSE → do something different. Always test BOTH paths to make sure each one works!
Key words
- Conditional
- A rule in code that checks if something is true or false and then decides what to do next.
- If statement
- The main way to write a conditional — it starts with the word IF followed by a question the computer checks.
- Boolean
- A value that is either TRUE or FALSE — conditionals always check a boolean.
- Bug
- A mistake in your code that makes the program do the wrong thing or crash.
- Debugging
- The process of finding and fixing bugs in your code, like being a detective for errors.
- Else
- The part of a conditional that runs when the IF condition is NOT true — the 'otherwise' option.
- Condition
- The question or test inside an IF statement, like 'is the score greater than 10?'
- Syntax error
- A bug where you typed something the computer cannot understand, like a missing parenthesis or misspelled word.
Worked examples
You want a game to say 'You win!' if the player's score is 10 or more. Write the conditional in plain English.
→ IF score >= 10, THEN display 'You win!' ELSE display 'Try again!' · The >= symbol means 'greater than or equal to,' so a score of exactly 10 also wins.
A program should turn a virtual light ON if it is dark outside and OFF if it is light. What does the conditional look like?
→ IF it_is_dark = TRUE, THEN turn light ON, ELSE turn light OFF · The condition checks a true/false (boolean) value, which is very common in conditionals.
Your Scratch project is supposed to make a cat move when you press the space bar, but nothing happens. How do you start debugging?
→ First, check that the 'When space key pressed' block is connected to the move block. If they are not snapped together, the code never runs. Reconnect them and test again. · Disconnected blocks are one of the most common bugs in Scratch.
A student writes: IF age > 12 print 'Teen'. The program crashes. What is the bug?
→ The print statement is missing quotation marks and a colon in Python. It should be: if age > 12: print('Teen'). A missing colon or parenthesis is a syntax error. · Even one missing character can stop the whole program from running.
A quiz app gives everyone 'Correct!' even when the answer is wrong. What kind of bug is this and how do you fix it?
→ This is a logic bug. Check the condition — it probably says IF answer == anything instead of IF answer == correct_answer. Fix the condition so it compares the player's answer to the right answer. · Logic bugs do not crash the program, but they make it behave incorrectly.
You want different messages for three score ranges: below 5, 5 to 9, and 10 and above. How do you structure the conditionals?
→ IF score < 5, THEN 'Keep practicing!' ELSE IF score < 10, THEN 'Good job!' ELSE 'Amazing!' · Chaining ELSE IF lets you check multiple conditions one after another.
Common mistakes
- Forgetting the ELSE branch — without it, the program does nothing when the condition is false, which can cause silent bugs.
- Testing only one path — always test what happens when the condition IS true AND when it is NOT true.
- Using = instead of == to compare values — a single equals sign sets a value, while double equals checks if two things are equal.
- Leaving blocks disconnected in Scratch or forgetting a colon or parenthesis in text-based code, causing syntax errors.
- Giving up when a bug is found instead of reading the code line by line — slow, careful reading almost always reveals the problem.
FAQs
What is the difference between a bug and a syntax error?
A syntax error is one type of bug — it happens when the code is written in a way the computer cannot read. Other bugs, like logic errors, are written correctly but still make the program do the wrong thing.
Does every IF statement need an ELSE?
No, ELSE is optional. If you leave it out, the program just skips to the next line when the condition is false. But adding ELSE often makes your program handle more situations correctly.
Why do programmers call mistakes 'bugs'?
The nickname comes from 1947 when a real moth got stuck inside a computer and caused it to malfunction. The team called it a 'bug,' and the name stuck for all kinds of programming errors.
How do I know where to look for a bug?
Start where the problem first shows up. Read each line of code out loud and ask yourself, 'What will the computer do here?' When you find a line that does not match what you expected, you have likely found the bug.
Can a program have more than one conditional?
Yes! Most programs have many conditionals. A game might check your score, your health, your level, and the timer all with different IF statements working together.
What does debugging feel like for real programmers?
Even professional programmers spend a lot of time debugging — it is a totally normal part of coding, not a sign that you are bad at it. Finding and fixing a bug feels like solving a puzzle!
Want the whole picture for your child?
Every K–6 subject, an AI tutor that teaches step by step, unlimited practice, and a reward world.
Start a 3-day free trial