Lesson 5 of 20

Conditionals

Conditionals

if / else if / else

int score = 85;
if (score >= 90) {
    System.out.println("A");
} else if (score >= 80) {
    System.out.println("B");
} else if (score >= 70) {
    System.out.println("C");
} else {
    System.out.println("F");
}
// prints: B

Ternary Operator

A compact one-liner for simple conditions:

int x = 7;
String result = (x % 2 == 0) ? "even" : "odd";
System.out.println(result);  // odd

Logical Operators

  • && — and
  • || — or
  • ! — not
int age = 20;
boolean hasId = true;
if (age >= 18 && hasId) {
    System.out.println("entry allowed");
}

Your Task

Given score = 85 and n = 7:

  1. Print the letter grade (A ≥ 90, B ≥ 80, C ≥ 70, F otherwise)
  2. Print whether n is "even" or "odd" using the ternary operator
TeaVM (WASM) loading...
Loading...
Click "Run" to execute your code.