Computer - Java Notes (If-Else, Switch) (Class-Ix, File-4) - 2023
Computer - Java Notes (If-Else, Switch) (Class-Ix, File-4) - 2023
Computer - Java Notes (If-Else, Switch) (Class-Ix, File-4) - 2023
Jump/unconditional
SELECTION/CONDITIONAL/DECISION MAKING STATEMENTS
The statement that allows us to alter the flow of control (top to bottom) of a program depending upon conditions is
known as conditional statement.
Selection statements are used in a program to check condition(s) to perform a specific action.
Java supports two selection statements:
* if-else statement
* switch-case statement
If-else STATEMENT
It is an example of bi-directional branching statement.
It checks the condition(s) and transfer the control to the execution of either of the two blocks of statement.
1
2
switch-case STATEMENT
The switch statement is Java’s multi-way branch statement. It provides an easy way to dispatch execution to
different parts of your code based on the value of an expression. As such, it often provides a better alternative
than a large series of if-else-if statement.
➢ By switch statement we can check any expression and transfer the control to the given case.
➢ The case is terminated by a break statement.
➢ If switch constant does not match with any of the case, then the default is executed.
a) “if” can evaluate complex relational or logical a) switch statement can only work for equality
expressions comparisons
b) Many different variables can be used to form b) Only a single value is used for Selection.
complex expression.
c) if – else can handle floating point tests c) Only byte, short, int, String or char can be used.