java · level 1 · unit 4 · lesson 4
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
Build bigger questions with , and .
&& means and. It gives true only when both sides are true. A cake needs flour and eggs: if either one is missing, there is no cake.
|| means or. It gives true when at least one side is true, like a lie-in on Saturday or Sunday. ! means not. It flips an answer, so !true is false.
A boolean box already counts as a condition. So if (!hasEggs) reads like plain English: if we do not have eggs.
public class Main {
public static void main(String[] args) {
boolean hasFlour = true;
boolean hasEggs = false;
if (hasFlour && hasEggs) {
System.out.println("Time to bake a cake!");
}
if (!hasEggs) {
System.out.println("Off to the shop for eggs.");
}
}
} Write a full condition on each side, like day == 6 || day == 7.
guess first
Did you earn pudding? Make a guess.
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
public class Main {
public static void main(String[] args) {
boolean atePeas = true;
boolean ateCarrots = true;
if (atePeas || ateCarrots) {
System.out.println("Pudding time!");
} else {
System.out.println("Veg first, please.");
}
}
}
your turn
Your turn. Get on the Twister.
The Twister has two rules: you must be 8 or older, and at least 120 cm tall. Choose an age and a height, in whole numbers, that get you on.
output
press run to see what happens
fix it
No lie-in on a Sunday? Fix the check.
output
press run to see what happens
- Day 6 is Saturday and day 7 is Sunday. Can day be 6 and 7 at the same time?
&&needs both sides to betrue. You want a lie-in when either one is.- Change
&&to||on line 4, then press run.
nailed it
you can ask smarter questions now. :D
&&needs both sidestrue.||needs at least one.!flipstruetofalse, andfalsetotrue.- Write a full condition on each side of
&&or||.
new words in your collection
- &&
- ||
- !
This lesson isn’t saved. A free account remembers every lesson you finish.