Lesson 8 of 34

&&, || and !: Combining Conditions

8 min 25 XP

&& (AND) is true only when both sides are true. || (OR) is true when either side is true. ! (NOT) flips true to false and false to true.

Example: edit me and press Run

Output Loading Java (first time takes a few seconds)
Press Run to see the result here.

A small if/else can be written on one line with the ternary operator: condition ? value1 : value2

Example: edit me and press Run

Output Loading Java (first time takes a few seconds)
Press Run to see the result here.

Challenge

+25 XP

Read a year. Print "Leap year" if it is a leap year, otherwise "Not a leap year". Rule: divisible by 400, or divisible by 4 but not by 100.

Output Loading Java (first time takes a few seconds)
Press Run to see the result here.