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;
}
-> Condition is in Boolean format i.e either true or false.
-> if condition is true, then if block get's executed.
B.) If else statement:-
-> Syntax:-
if(condition)
{
statements;
}
else
{
statements;
}
-> Condition is in Boolean format i.e either true or false.
-> if condition is true, then if block gets executed.
-> if condition is false, then else block gets executed.
C.) else if statement:-
-> Syntax:-
if(condition 1)
{
statements;
}
else if(condition 2)
{
statements;
}
else if(condition 3)
{
statements;
}
.
.
.
.
else
{
statements;
}
-> else if statement is used to make multiple conditions at a time.
-> if block gets executed if the first condition is true and the rest blocks will
be
ignored.
-> 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.
D.) switch case statement:-
-> switch case statements are used to create Menu Driven Programs.
-> A menu based program is a console based program by choosing an option that
perform some tasks.
-> Syntax:-
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.