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 decisionsswitch– for selecting one of many possible paths based on a single value- Ternary operator
? :– a shorthand for simpleif‑elseassignments
These constructs are used in almost every Java program, often in combination with loops (section 4.2) to build complex logic.