Java control statements:
Java compiler executes the code from top to bottom.
The statements in the code are executed according to the order in which they appear.
Java provides statements that can be used to control the flow of Java code. Such statements are called control flow statements.
These statements decide whether a specific part of the code will be executed or not. So it is used to control the flow of the program by executing the code on a conditional basis.
TYPES OF CONTROL STATEMENTS:

CONDITIONAL STATEMENT: ( selection statements)
Statements which works based on the condition.
Condition – expression which gives either true or false.
Conditional statements helps us to make decision based on certain conditions.
Conditional statements allows us for execution control to follow a certain path if a condition is met and an alternate path if not.
SIMPLE IF:
The Java if statement is the most simple decision-making statement.
It is used to decide whether a certain statement or block of statements will be executed or not i.e if a certain condition is true then a block of statement is executed otherwise not.
SYNTAX:
if(condition)
{
}

EXAMPLE:1
int age= 25;
if (age>=18)
{
println(“eligible to vote”);
}
EXAMPLE:2
int i=15;
if(i>=10)
{
println(“i is greater than 10”);
}
If else:
If the condition is true, then the true block gets executed.
If the condition is false, then the true block gets skipped(won’t get executed).
SYNTAX:
if(condition)
{
//true block
statements
}
else
{
//false block
statements
}

EXAMPLE:1
int age=15;
if(age>=18)
{
println(“Eligible to vote”);
}
else
{
println(“Not eligible to vote”);
}
EXAMPLE:2
int a=5;
int b=25;
if(a>b)
{
println(“a is greater”);
}
else
{
println(“b is greater”);
}
Nested if else:
ifelse inside either true or false block of another ifelse.
Multiple ifelse statements.
Also called as if else ladder.
SYNTAX:
if(condition)
{
//true block
insert statement here
}
else
{
//false block
insert statement here
}

EXAMPLE:
int mark =55;
if(mark<=49)
{
println(“fail”);
}
else
{
println(“pass”);
//another if else statement
if(mark>=75)
{
println(“with distinction”);
}
else
{
println(“without distinction”);
}
}
Switch case:
Works based on multiple conditions.
Replacement for complicated nested ifelse.
Mandatory keywords are switch and case.
Optional keywords are default and break.
SYNTAX:
switch()
{
case1:
statements
case2:
statements
case3:
statements
case4:
statements
case5:
statements
default:
insert your statements
}

EXAMPLE:1
int mark = 76;
switch(1)
{
case1:
// Any number between 0 to 49 will be captured in this case
if(mark>=0 && mark<=49)
{
println(“fail“);
}
case2:
// Any number between 50 to 59 will be captured in this case
if(mark>=50 && mark<=59)
{
println(“E GRADE“);
}
case3:
// Any number between 60 to 69 will be captured in this case
if(mark>=60 && mark<=69)
{
println(“D GRADE“);
}
case4:
// Any number between 70 to 79 will be captured in this case
if(mark>=70 && mark<=79)
{
println(“C GRADE“);
}
case5:
// Any number between 80 to 89 will be captured in this case
if(mark>=80 && mark<=89)
{
println(“B GRADE“);
}
case6:
// Any number between 90 to 100 will be captured in this case
if(mark>=90 && mark<=100)
{
println(“A GRADE“);
}
default:
if(mark<0 || mark>100)
{
println(” INVALID MARK“);
}
}
EXAMPLE:2
switch(1)
{
case1:
system.out.println(“one”);
case2:
system.out.println(“two”);
case3:
system.out.println(“three”);
case4:
system.out.println(“four”);
default:
system.out.println(“Invalid”);
}
Leave a comment