Control Statement
Decision Making Statements
1. if
2. if-else
3.if-else-if Ladder
4. nested-if
5. switch-case
3.if-else-if Ladder
•These statements are similar to the if else
statements
•User can decide among multiple options.
•Conditions controlling the if is true, the statement
associated with that if is executed, and the rest of
the ladder is bypassed.
•If none of the conditions is true, then the final
else statement will be executed.
Syntax
if(condition)
{
Statements
}
else if(condition)
{
}
else if(condition)
{
}
.
.
else
{
Statements default
}
Example
class ifelseifDemo
{
public static void main(String args[])
{
int day=1;
if (day== 1)
{
System.out.println(“Monday");
}
else if (day== 2)
{
System.out.println(“Tuesday");
}
else if (day== 3)
{
System.out.println(“Tuesday");
}
else
{
System.out.println(“Other Day");
}
} }
Nested if Statements
A nested if is an if statement that is the target
of another if or else. Nested if statements
means an if statement inside an if statement.
Yes, java allows us to nest if statements
within if statements. i.e, we can place an if
statement inside another if statement.
syntax
if(condition)
{
GIFif statement
{
else
{
GIFif statement
}
Switch Statement
•The switch statement is a multiway branch
statement. It provides an easy way to dispatch
execution to different parts of code based on the
value of the expression.
•Select one statement at one time
Syntax
switch (expression)
{
case value1:
statement1;
break;
case value2:
statement2;
break;
.
.
case valueN:
statementN;
break;
default:
statementDefault;
break;
}
Example
class SwitchCaseDemo
{
public static void main(String args[])
{
int n = 0;
switch (n)
{
case 0:
System.out.println(“n is zero.");
break;
case 1:
System.out.println(“n is one.");
break;
case 2:
System.out.println(“n is two.");
break;
default:
System.out.println(“n is greater than 2.");
}
}
}
Loops Statements in Java
1.while loop
2.do-while loop
3.for loop
1.while loop
• while loop is a control flow statement that
allows code to be executed repeatedly based
on a given Boolean condition.
• The while loop can be thought of as a
repeating if statement.
Syntax
while(condition)
{
statements;
}
next
Syntax
Example
class sample
{
public static void main(String[] args)
{
int i = 1;
while (i < 3)
{
System.out.println(“Java"); i=1
i++
i++;
i=i+1
} i=1
} i=1+1
i=2
}
Java
Example
class sample
{
public static void main(String[] args)
{
int i = 1;
while (i < 3)
{
System.out.println(“Java");
i++; i=2
i++
}
i=i+1
} i=2
} i=2+1
i=3
Java
java
do while loop
Java do while loop executes the
statement first and then checks for the
condition.Other than that it is similar to
the while loop.
Syntax
do
{
statements
} while (testcondition);
Example
class sample
{
public static void main(String[] args)
{
int i = 1;
do
{
System.out.println(“Java");
i++;
}while (i < 3) ;
Java
} java
}
For loop in Java
•Java for loop consists of 3 primary factors
which define the loop itself.
•These are the initialization statement, a
testing condition, an increment or decrement
part for incrementing/decrementing the control
variable.
For loop in Java
syntax
for(initializing statement; testing condition; increment/decrement)
{
//code to be iterated
}
Example
class ForLoop
{
public static void main(String[] args)
{
int i; Java
Java
for (i = 1; i <=5; i++) Java
Java
{ Java
System.out.println(“Java");
}
}
}