Lessons

Java tutorial

Java If and Else

if runs a block only when its condition is true. else if tests another condition, and else runs when none of the earlier conditions matched. Only one of these branches runs.

Example

Edit the code and run it again. This runner stays in your browser and covers the syntax used in these lessons.

Output

Run the example to see output here.

What this code does

hour is 9, which is less than 12, so the first branch prints Good morning and the other branches are skipped.