Lesson 6 of 34

if and else: Making Decisions

8 min 25 XP

if runs code only when a condition is true; otherwise the else part runs. The condition goes inside ( ) brackets.

Example: edit me and press Run

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

Comparison operators: == equal, != not equal, > greater, < less, >= greater or equal, <= less or equal. Each gives a boolean (true/false).

Example: edit me and press Run

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

= stores a value, == compares. Mixing them up is one of the most common mistakes.

Challenge

+25 XP

Read a student's mark. Print "Pass" if it is 35 or more, otherwise print "Fail".

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