Control Flow Statements-
Control Flow Statements-
-> Control Flow statements are also known as decision making statements.
-> Control low statements are used to control the flow of execution.
-> In java there are 4 decision statements:-
a.) if statement
b.) if else statement
c.) else if statement
d.) switch case statement
A.) If statement:-
-> Syntax:-
if(condition)
{
statements;
}
if(condition)
{
statements;
}
else
{
statements;
}
if(condition 1)
{
statements;
}
else if(condition 2)
{
statements;
}
else if(condition 3)
{
statements;
}
.
.
.
.
else
{
statements;
}
-> if condition 1 is false, then it goes and checks for the next condition. If the
next conditions are true then that else if block gets executed and vice-versa.
-> else block get's executed if all the conditions are false.
switch(value/variableName/expression)
{
case 1:
{
statements;
}
break;
case 2:
{
statements;
}
break;
case 3:
{
statements;
}
break;
.
.
.
.
.
default:
{
statements;
}
}
Working:-
-> switch accepts value( literal ) or variableName( identifier ).
Based on the switch value, that particular case block get's executed.
-> default block gets executed if switch value and case value does not match.
-> In switch case statement - long, float, double, char Boolean datatypes are not
allowed in switch case.