Home If statements

Branching semantics

Java allows you to combine if/else statements and branch in several different ways. Select the method that best suits what kind of branch you want to execute. Expand for more details.

Factor out common code

If you have common code at the beginning or end of a conditional, pull that code to avoid duplication and redundancy.

Avoid unnecessary tests

Avoid unnecessary tests. If you already know that something is true, then you shouldn't bother to have a test for it. Such tests just make your code longer and harder to understand.

Empty blocks

You should never have empty blocks in your code – never have at set of curly braces that contain nothing inside.


We do not expect you to know about or use switch-case in any of your assignments and exams. We don't explicitly forbid you from using it, but we strongly encourage you to use if/else statements instead.