If and else
After this: Run some lines only when a condition is true, and other lines otherwise.
On this page · 6 sections · 3 checks
Picture the entrance to a concert. A person walks up. If they are 18 or older, the guard says "Welcome". Otherwise the guard says "Sorry, not yet". One question, two paths, and only one of the paths happens for each person.
In the last lesson you learned to ask a question and get a yes/no answer. Now the program will use that answer to decide which lines to run.
Run these lines only if
The word if tells the computer: ask this question, and run the lines that belong to me only when the answer is yes. When the answer is no, skip those lines and carry on below.
The smallest version is in the trace below. Read its code, then work through it with age = 20.
- The first line puts 20 in the box
age. - The
ifline asks: isage >= 18? The computer looks in the box, sees 20, and answers yes. - Because the answer is yes, the line that belongs to the
ifruns and prints Welcome. - The line after that is not part of the
if. It runs whatever the answer was, and prints Done.
Now the same lines with age = 15. The question gets the answer no, so the Welcome line is skipped. Only Done is printed.
The trace below runs both cases, one after the other. When a condition is false, the trace says that the program jumps to the line after the skipped lines. Watch for that jump the second time round.
- Run the programRun it. The first time, age is 20 and the Welcome line runs. The second time, age is 15: watch the trace jump over the Welcome line.
age = 20if age >= 18: print("Welcome")print("Done")age = 15if age >= 18: print("Welcome")print("Done")run the program to see its boxes
nothing printed yetPress Run it, then step through the program one line at a time.
- lines
- 0
- prints
- 0
- calls
- 0
Otherwise: else
Often you want something to happen on the other path too. The word else means "otherwise". The lines that belong to else run only when the answer was no.
age = 15if age >= 18: print("Welcome")else: print("Sorry, not yet")print("Done")run the program to see its boxes
nothing printed yetPress Run it, then step through the program one line at a time.
- lines
- 0
- prints
- 0
- calls
- 0
With age = 15 the condition is false, so the Welcome line is skipped, the program jumps to the else part and prints Sorry, not yet. Then Done is printed, because it sits after the whole decision and belongs to neither part.
The rule: exactly one of the two parts runs. Never both, and never neither.
Which lines belong to the if?
The computer needs to know where the if part ends and the rest of the program begins. The two languages mark this differently.
Run it
Change 15 to 18 and run again: the other path should run. Then try 17.
Check yourself
A program has an if with the condition age >= 18, and an else. The box age holds 15. Which part runs?
What does this print?
temp = 30if temp > 25: print("Hot")else: print("Mild")print("Bye")int temp = 30;if (temp > 25) { System.out.println("Hot");} else { System.out.println("Mild");}System.out.println("Bye");Complete the missing line so that Welcome is printed for anyone who is 18 or older, and Sorry, not yet for everyone else. Write the whole line, including the colon at the endbrackets and the opening brace.
age = 20____________________ # ← the if line, with its condition print("Welcome")else: print("Sorry, not yet")int age = 20;____________________ // ← the if line, with its condition System.out.println("Welcome");} else { System.out.println("Sorry, not yet");}Honesty notes
- Write an if, so that some lines run only when the condition is True.
- Add an else, so that other lines run when the condition is False.
- Say which lines belong to the if (indented in Python, inside braces in Java) and which come after it.
- Follow a trace that jumps over the lines a False condition skips.