Drill 20: Why this way — Syntax and control flow
Explain why the example is written this way rather than another way.
What you will be able to do
- Explain why the example is written this way rather than another way.
Practice task
Explain why the example is written this way rather than another way.
public class ControlFlow {
public static void main(String[] args) {
int minutes = 12;
String level = minutes < 10 ? "short" : "long";
if (minutes >= 14) {
System.out.println("deep dive");
} else if (minutes >= 8) {
System.out.println("steady lesson");
} else {
System.out.println("quick intro");
}
switch (level) {
case "short" -> System.out.println("under 10 minutes");
case "long" -> System.out.println("10 minutes or more");
default -> System.out.println("unknown");
}
int total = 0;
for (int i = 0; i < 3; i++) {
total += i;
}
while (total < 5) {
total += 1;
}
System.out.println(total);
}
}Your turn
EasyWrite your answer
Explain why the example is written this way rather than another way.
TEXT · starter
Your answer…Study tools
Make it yours.
Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.
Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign inFinished reading?
Lock in what you just learned.
0%
Mark this lesson complete to save your place and move one step closer to the certificate.