Lesson 2.8
Nested conditions
Part 2: DecisionsReading, about 3 min1 exercise in this lesson
Java answers can't run on Spine yet. You can still read Java, trace it, and put an answer's lines in order.
Nested conditions
By the end of this you will be able to check one condition only when another is already true, by putting one if inside another.
In the last lesson you combined two conditions on one line with and (and in Python, && in Java). A nested if does something different: it checks a second condition only after the first one passes, and can give a different response when the first passes but the second does not.
A nested if
Checking a username, then a password:
user = input()
pw = input()
if user == "admin":
if pw == "secret":
print("Welcome")
else:
print("Bad password")
else:
print("Unknown user")import java.util.Scanner;
public static void main(String[] args) {
Scanner scanner
= new Scanner(System.in);
String user = scanner.nextLine();
String pw = scanner.nextLine();
if (user.equals("admin")) {
if (pw.equals("secret")) {
System.out.println("Welcome");
} else {
System.out.println("Bad password");
}
} else {
System.out.println("Unknown user");
}
}Three outcomes: welcome (both right), bad password (user right, password wrong), unknown user (user wrong). Joining both checks with and in one condition would give only two outcomes (both right, or not), losing the "bad password" message.
When to nest and when to use and
Use and when both conditions lead to the same action and there is nothing to say when only one is true. Use a nested if when the inner condition matters only after the outer one passes, or when the two failures need different messages.
Now try it yourself on the questions below.
Exercise 2.8.12.8.1 · Write itLogin check
Write a program that reads a username on the first line and a password on the second line, and prints a result.
- Always print
User:followed by the username on the first line. - If the username is
adminand the password issecret, printWelcome, admin. - If the username is
adminbut the password is wrong, printWrong password. - If the username is anything else, print
Unknown user. - Print exactly one of those three result lines. Spaces at the end of a line are ignored.
Your program is run several times. Only what it prints is checked.
In Python, use == for text. In Java, use the equals method.
Running Java here comes later. You can put this answer's lines in order instead. You can still write your Java answer (it stays while this page is open) and compare it with our worked solution. Or try this exercise in Python.
Examples
Your program is run 7 times: the 4 examples below, and 3 hidden ones that check the rules above. Only what it prints is checked.
correct username and password (rule 2)
You type
- typed: admin
- typed: secret
It prints
User: admin Welcome, admin
correct username, wrong password (rule 3)
You type
- typed: admin
- typed: wrong
It prints
User: admin Wrong password
wrong username, right password (rule 4)
You type
- typed: guest
- typed: secret
It prints
User: guest Unknown user
wrong username, wrong password (rule 4)
You type
- typed: guest
- typed: wrong
It prints
User: guest Unknown user