Control Statements
Decision Making statements
Java if Statement
• if statement is used to test the condition.
• Checks boolean condition: true or false.
• There are various types of if statement in Java.
1.if statement
2.if-else statement
3.if-else-if ladder
4.nested if statement
Java if Statement
if statement tests the condition.
It executes the if block if condition is true.
Syntax:
if(condition)
{
//code to be executed
}
Data-flow-diagram of if Block
Example
public class IfDemo1 {
public static void main(String[] args)
int marks=70;
if(marks > 65)
System.out.print("First division");
}
Java if-else Statement
• It executes the if block if condition is true otherwise else block is executed.
•Synatax:
if(condition)
{
//code if condition is true
}
else
{
//code if condition is false
}
Data-flow-diagram of if else Block
Example
public class IfElseDemo1 {
public static void main(String[] args)
{
int marks=50;
if(marks > 65)
{
System.out.print("First division");
}
else
{
System.out.print("Second division");
}
}
}
Java if-else-if ladder Statement
•The if-else-if ladder statement executes one condition from multiple statements.
if(condition1)
{
//code to be executed if condition1 is true
}
else if(condition2)
{
//code to be executed if condition2 is true
}
else if(condition3){
//code to be executed if condition3 is true
}
...
else{
//code to be executed if all the conditions are false
}
Data-flow-diagram of If Else If Block
int marks=75; System.out.println("B grade");
if(marks<50) }
{ else if(marks>=80 && marks<90)
System.out.println("fail"); {
} System.out.println("A grade");
else if(marks>=50 && marks<60) }
{ else if(marks>=90 && marks<100)
System.out.println("D grade"); {
} System.out.println("A+ grade");
else if(marks>=60 && marks<70) }else{
{ System.out.println("Invalid!");
System.out.println("C grade"); }
else if(marks>=70 && marks<80){
Java Nested if statement
•Nested if statement represents the if block within another if block.
•Inner if block condition executes only when outer if block condition is true.
•Syntax:
if(condition)
{
//code to be executed
if(condition)
{
//code to be executed
}
}
Data-flow-diagram of Nested if Block
int age=25;
int weight=70;
if(age>=18)
if(weight>50)
System.out.println("You are eligible");
}
Java Switch Statement
• switch statement executes one statement from multiple conditions.
• like if-else-if ladder statement
•Syntax
switch(expression)
{
case x: // code block
break;
case y: // code block
break;
default: // code block
}
• Switch expression is evaluated once.
• Value of the expression is compared with the values of each case.
• If there is a match, the associated block of code is executed.
• Break and default keywords are optional
• Case values must be unique. In case of duplicate value, it renders compile-
time error.
int number=20;
switch(number){
case 10: System.out.println("10");
break;
case 20: System.out.println("20");
break;
case 30: System.out.println("30");
break;
default:System.out.println("Not in 10, 20 or 30");
String name = "Mango";
switch(name){
case "Mango":
System.out.println("It is a fruit");
break;
case "Tomato":
System.out.println("It is a vegitable");
break;
case "Coke":
System.out.println("It is cold drink");
Loops in Java
Loop statements
Loop is designed to execute particular code block till the specified
condition is true
• do while loop
• while loop
• for loop
• for-each loop
Java While Loop
Loops through a block of code as long as a specified condition is true:
Syntax:
while (condition)
{
//code to be executed
Increment / decrement statement
}
Example
class Main {
public static void main(String[] args)
{
int i = 1, n = 5;
while(i <= n)
{
System.out.println(i);
i++;
}
}}
Note:
Do not forget to increase the variable used in the condition, otherwise the loop
will never end!
Java do-while Loop
• The do/while loop is a variant of the while loop.
• This loop will execute the code block once, before checking if the condition
is true, then it will repeat the loop as long as the condition is true.
• Syntax:
do{
//code to be executed / loop body
//update statement
}while(condition);
Flowchart of Java do while loop
Example
class Main
{
public static void main(String args[])
{
int i = 0;
do {
System.out.println(i);
i++;
}
while (i < 5);
}}
for Loop
• f or loop is used for executing a part of the program repeatedly.
• When the number of execution is fixed then it is suggested to use for loop
• Syntax:
for(initialization; condition; increment/decrement)
{
//statement or code to be executed
}
1. Initialization:
• It is the initial condition which is executed once when the loop starts.
• Here, we can initialize the variable, or we can use an already initialized
variable.
2. Condition:
• It is the second condition which is executed each time to test the condition
of the loop.
• It continues execution until the condition is false.
• It must return boolean value either true or false..
3. Increment/Decrement:
• It increments or decrements the variable value.
4. Statement:
• The statement of the loop is executed each time until the second
condition is false.
public class ForDemo1
{
public static void main(String[] args)
{
int n, i;
n=2;
for(i=1;i<=10;i++)
{
System.out.println(n+"*"+i+"="+n*i);
}
}
}
Java Nested for Loop
public class NestedForExample {
public static void main(String[] args)
{
for(int i=1;i<=3;i++)
{
for(int j=1;j<=3;j++)
{
System.out.println(i+" "+j);
}
}
}
}
Half pyramid pattern using nested loops
class Main {
public static void main(String[] args) {
int rows = 5;
for (int i = 1; i <= rows; ++i) {
for (int j = 1; j <= i; ++j) {
System.out.print(j + " ");
}
Output:
System.out.println(""); 1
} 12
123
} 1234
} 12345
Inverted half pyramid using *
public class Main {
public static void main(String[] args) {
int rows = 5;
for (int i = rows; i >= 1; --i) {
for (int j = 1; j <= i; ++j) {
*****
System.out.print("* "); ****
***
} **
*
System.out.println();
}
Java break Statement
• Break statement is used to break loop or switch statement.
• It breaks the current flow of the program at specified condition.
• We can use Java break statement in all types of loops such as
for loop
while loop
do-while loop
Example
class Test {
public static void main(String[] args) {
for (int i = 1; i <= 10; ++i) {
if (i == 5)
{
Output:
break; 1
} 2
3
System.out.println(i); 4
}
}
}
Break Statement with Inner Loop
//outer loop
for(int i=1;i<=3;i++){
//inner loop
for(int j=1;j<=3;j++){
if(i==2&&j==2)
{
Output:
break; 11
12
} 13
21
System.out.println(i+" "+j); 31
32
} 33
}
labeled break Statement
aa:
for(int i=1;i<=3;i++){
bb:
for(int j=1;j<=3;j++){
if(i==2&&j==2)
{
break aa;
Output:
}
11
System.out.println(i+" "+j); 12
13
} 21
}
Java continue Statement
• Continue statement breaks one iteration (in the loop), if a specified
condition occurs, and continues with the next iteration in the loop.
Working of Java continue Statement
Example
class Main {
public static void main(String[] args) {
for (int i = 1; i <= 10; ++i)
{
// if value of i is between 4 and 9 continue is executed
if (i > 4 && i < 9) {
continue; Output
1
} 2
3
System.out.println(i); 4
9
} 10
Continue Statement with Inner Loop
//outer loop
for(int i=1;i<=3;i++){
//inner loop
for(int j=1;j<=3;j++){
if(i==2&&j==2){
Output
continue; 11
12
} 13
21
System.out.println(i+" "+j); 23
31
32
}
33
Continue Statement with Labelled For Loop
aa:
for(int i=1;i<=3;i++){
bb:
for(int j=1;j<=3;j++){
if(i==2&&j==2){
continue aa;
} Output
11
System.out.println(i+" "+j); 12
13
} 21
31
} 32
33