java · level 1 · unit 4 · lesson 3
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
adds more paths to the fork.
Some questions have more than two answers. else if adds another condition to check when the ones above it were false. Java checks from the top down and runs only the first block whose condition is true.
Every gives true or false. < means less than, > more than, <= less than or equal to, >= more than or equal to, and != not equal to.
Text is different. To compare strings, use , like pet.equals("cat"). == asks whether two strings are the very same box, not the same letters, so it can say false when the text matches.
public class Main {
public static void main(String[] args) {
int temperature = 12;
String pet = "cat";
if (temperature < 5) {
System.out.println("Wear a big coat.");
} else if (temperature < 15) {
System.out.println("A jumper will do.");
}
System.out.println(pet.equals("cat"));
}
} In <= and >= the = always comes second, never first.
guess first
Big slide or small slide? Guess first.
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
public class Main {
public static void main(String[] args) {
int height = 120;
if (height > 120) {
System.out.println("Hop on the big slide!");
} else if (height >= 100) {
System.out.println("Try the small slide.");
}
}
}
your turn
Your turn. How many sweets have you got?
Choose a number of sweets for line 3 and a comparison sign for line 6, like < or >=, so the else if message shows.
output
press run to see what happens
fix it
Paris is marked wrong. Can you fix it?
output
press run to see what happens
- The answer typed is Paris, yet no points appear. Look at how line 7 compares the two strings.
==asks whether two strings are the very same box. To compare the letters, strings use a method.- Change line 7 to
if (answer.equals("Paris")) {, then press run.
nailed it
your code can pick from many paths. :D
else ifchecks a new condition when the ones above werefalse.- Java runs only the first block whose condition is
true. - Compare strings with
.equals, never with==.
new words in your collection
- else if
- comparison
- equals
This lesson isn’t saved. A free account remembers every lesson you finish.