![]() |
VOOZH | about |
Decision-making statements in Java allow a program to choose different execution paths based on specified conditions. They help control the flow of a program by executing certain blocks of code only when particular conditions are true. These statements are essential for implementing logic, validations, and user-driven actions in applications.
Java provides the following decision-making statements:
The if statement is the simplest decision-making statement. It executes a block of code only if a given condition is true.
Condition is True
Note: If curly braces {} are omitted, only the next line after if is considered part of the block.
The below diagram demonstrates the flow chart of an "if Statement execution flow" in programming.
Execution Flow
The if-else statement allows you to execute one block if the condition is true and another block if it is false.
i is smaller than 15
The below diagram demonstrates the flow chart of an "if-else Statement execution flow" in programming
Execution Flow
A nested-if is an if statement inside another if statement. It is useful when a second condition depends on the first.
i is smaller than 15 i is exactly 10
The below diagram demonstrates the flow chart of an "nested-if Statement execution flow" in programming.
Execution Flow
The if-else-if ladder allows multiple independent conditions to be checked in order. As soon as one condition is true, its block executes, and the rest are skipped.
i is 20
The below diagram demonstrates the flow chart of an "if-else-if ladder execution flow" in programming
Execution Flow
The switch statement is a multiway branch statement. It provides an easy way to dispatch execution to different parts of code based on the value of the expression.
It is 20
The below diagram demonstrates the flow chart of a "switch Statements execution flow" in programming.
Note:
- The expression can be of type byte, short, int char, or an enumeration. Beginning with JDK7, the expression can also be of type String.
- Duplicate case values are not allowed.
- The default statement is optional.
- The break statement is used inside the switch to terminate a statement sequence.
- The break statements are necessary without the break keyword, statements in switch blocks fall through.
The ternary operator in Java is a conditional operator that provides a shorthand way to write simple if-else statements
Syntax:
condition ? expression_if_true : expression_if_false;
Maximum is 20
Explanation: This program uses the ternary operator ( ? : ) to find the maximum of two numbers. It checks the condition a > b; if true, it assigns a to the variable max, otherwise it assigns b. Finally, it prints the maximum value.
The table below demonstrates the difference between if-else and switch-case.
Features | if-else | switch-case |
|---|---|---|
Use Case | Suitable for condition-based checks | Best for exact value matching |
Readability | More readable for a few conditions | More readable and efficient for many cases |
Performance | Slower for many checks due to multiple conditions | Faster and optimized for handling many cases |
Flexibility | Supports ranges and complex conditions | Only supports exact matches of values |