Skip to content

Conditional statements

Conditional statements allow a program to make decisions and execute different blocks of code based on certain conditions. They are fundamental to controlling the flow of execution.

Java provides several conditional constructs:

  • if, else if, else – for simple to multi‑branch decisions
  • switch – for selecting one of many possible paths based on a single value
  • Ternary operator ? : – a shorthand for simple if‑else assignments

These constructs are used in almost every Java program, often in combination with loops (section 4.2) to build complex logic.