Purpose and Types
A Java program is a set of statements, which are executed sequentially in the order in which they appear. However, in some cases, the order of execution of statements may change based on the evaluation of certain conditions.
The java programming language possesses decision-making capabilities. The decision-making capabilities. The decision-making or control statements supported by java are:
- if statement
- if-else-if statement
- switch-case statement
Decision-making statements enable us to change the flow of the program. Based on the evaluation of a condition, a statement or a sequence of statements is executed.
if- statement
The if statement can be implemented in various forms based on the complexity of the conditions to be tested.
The different forms of the if statement are:
- Simple if statement : The if statement helps in decision- making based on the evaluation of a given condition to true or false. If the condition is true, then the statements in the if block get executed. If condition evaluates to false, the control is transferred directly outside the if block.
Syntax:
if (condition)
{
// one or more statements
}
- if-else statement: Generally, it is not only important to specify an action to be performed when the condition is true but also to specify what action is to be performed if the condition is false. To do this, the if-else statement can be used.
Syntax:
if(condition)
{
//one or more statements
else
}
{
// one or more statements
}
- Multiple if statements: The multiple if construct is known as the if-else-if ladder. The conditions are evaluated sequentially starting from the top of the ladder and moving downwards. When a true condition is found, the statement associated with the true condition is executed.
Syntax
If (condition)
{
// one or more statements
}
else if (condition)
{
// one or more statements
}
else
{
// one or more statements
}
Switch-case Statement
A program is difficult to comprehend when there are too many if statements are representing multiple selection consructs. To avoid this, the switch-case approach can be used as an alternative for multiple selections.
The switch-case statement is used when a variable needs to be compared against different values.
Important points in switch-case statement
- Switch: The switch keyword is followed by an integer experssion enclosed in parentheses. The expression must be of type int, char, byte or short. Each case value must be a unique literal. Thus, it must be a constant and not a variable. The switch statement executes the case corresponding to the value of the expression. The break statement terminates the statement sequence and continues with the statement following the switch. If there is no corresponding case value, the default clause is executed.
- Case: The case keyword is followed by an integer constant and a colon. Each case value is a unique literal. The case statement might be followed by a code sequence that are executed when the switch expression and the case value match.
- break: The break statement is used inside the switch-case statement to terminate the execution of the statement sequence. The control is then transferred to the first statement after the end of the switch. The break statement is optional. If there is no break, execution flows sequentially into the next case statement. Sometimes multiple cases can be present without break statement between them.
switch (expression)
{
case value1:
//statement sequence
break;
case value2:
//statement sequence
break;
……….
……….
case valueN:
//statement sequence
break;
default:
//default statement sequence
}
If-else-if |
Switch |
Each if has its own logical expression to be evaluated as true or false. |
Each case refers back to the original value of the expression in the switch statement. |
The variables in the expression may evaluate to value of any type. |
The expression must evaluate to a byte, short, char, or int. |
Only one of the blocks of code is executed. |
If the break statement is omitted, the execution will continue into the next block. |
For more go through this video:-