A selection statement is a control statements that allows the option between 2 or more execution paths in a program.
Java supports 2 Types of selection statements i.e if and switch.
If statement in Java:
It is also called conditional branch statement
Simple if statement :
In simple if statement if a condition specified is satisfied then the if block will be executed.
Syntax:
Example:
Output:
The if…else statement
In if…else statement if a condition specified is satisfied then the if block will be executed otherwise else block will be executed.
Syntax:
Example:
Output:
Nested if…else statement
In nested if…else statement you can use one if or else if statement inside another else if statement
Syntax:
Example:
Output:
The if..else..if ladder
The sequence of nested if statement is called if..else…if ladder.
Syntax
Example:
Output:
switch Statement:
The switch is the selection statement which is the multiway branch statement in java . We can say it is the best alternative for large series of if…else..if statement.
Syntax:
Example:
Output:
break statement is optional and if you omit , it will execute the next all case statements without checking the condition.
If you omit the break statement from above Eg:
The output will be as below even though a=3 ideally it should not.
Nested switch statement:
We can use the switch statement inside another switch statement.
Syntax:
Difference between IF and Switch statement:
1) If statement is used to select among 2 alternatives . switch statement is used to select among multiple alternatives.
2) In if statement you compare about 2 objects using =,<,> but switch statement compares 2 objects only using equal.
3) If statement works under Boolean literal where as switch statement works under integer/character literals
4) Need to write complex code using if…else..if statement where as switch simplifies the complexity of the code .
If statement in Java:
It is also called conditional branch statement
Simple if statement :
In simple if statement if a condition specified is satisfied then the if block will be executed.
Syntax:
Example:
Output:

The if…else statement
In if…else statement if a condition specified is satisfied then the if block will be executed otherwise else block will be executed.
Syntax:
Example:
Output:

Nested if…else statement
In nested if…else statement you can use one if or else if statement inside another else if statement
Syntax:
Example:
Output:

The if..else..if ladder
The sequence of nested if statement is called if..else…if ladder.
Syntax
Example:
Output:

switch Statement:
The switch is the selection statement which is the multiway branch statement in java . We can say it is the best alternative for large series of if…else..if statement.
Syntax:
Example:
Output:

break statement is optional and if you omit , it will execute the next all case statements without checking the condition.
If you omit the break statement from above Eg:
The output will be as below even though a=3 ideally it should not.

Nested switch statement:
We can use the switch statement inside another switch statement.
Syntax:
Difference between IF and Switch statement:
1) If statement is used to select among 2 alternatives . switch statement is used to select among multiple alternatives.
2) In if statement you compare about 2 objects using =,<,> but switch statement compares 2 objects only using equal.
3) If statement works under Boolean literal where as switch statement works under integer/character literals
4) Need to write complex code using if…else..if statement where as switch simplifies the complexity of the code .
No comments:
Post a Comment