You are on page 1of 36

Contents

 Introduction to Control Statement


 Types of Control Statement
 Jump Statement
 Loop in java
 Types of Loop
Control statement
 A statement that determines whether the other statements will be executed or not.
 It controls the flow of a program.
 Decision-Making statements allow you to make a decision, based upon the result of a condition. It tests
boolean condition: true or false.
 Here we represent flowchart of Decision-Making technique:
Types of control statement
There are following types of java selection statements in java:

1. If statement
2. If-Else statement
3. If-Else-If statement
4. Nested If statement
5. Switch statement
6. Jump statement
If Statement
An if statement consists of boolean expression followed by
one or more statements. Based on the result of these
expression programs are executed i.e. if condition true then
if block executed. If statement does not any other block
except if block.

Syntax –
if(Condition) {
// statements;
}
If statement
If_Example.java

public class If_Example {


public static void main(String args[]) {
int x = 10;
if(x < 20) {
System.out.println(“x is less than 20.”);
}
}
}

Output – x is less than 20.


If-else Statement
In this case an if statement can be followed by an else
statement which executes when the condition is false.

Syntax –
if(Condition) {
//Statements;
}
else {
// Statements;
}
If-else statement
IfElseExample.java
public class IfElseExample {
public static void main(String args[]) {
int x = 10;
if(x % 2 ==0) {
System.out.println(“x is even number.”);
}
else {
System.out.println(“x is odd number.”);
}
}
}

Output – x is even number.


If-else-if Statement
In this case, one condition executes from multiple
conditions.
Syntax –
if(condition-1) {
//Statements;
}
else if(Condition-2) {
//Statements;
}
else if(Condition-2) {
//Statements;
}
else {
//Statements;
}
If-else-if statement
IfElseIfExample.java
public class IfElseIfLadder { else if(marks >=75 && marks <100){
public static void main(String args[]) { System.out.println(“Excellent.”);
int marks = 75; }
if(marks < 33) { else {
System.out.println(“Fail.”); System.out.println(“Invalid !”);
} }
else if(marks >=33 && marks <45){ }
System.out.println(“Thired.”); }
}
else if(marks >=45 && marks <60){ Output - Excellent.
System.out.println(“Second.”);
}
else if(marks >=60 && marks <75){
System.out.println(“First.”);
}
Nested if Statement
In this case an if statement represents the if block within
another if block is called Nested If Statement. In this case,
inner if block executes only when the condition of outer if
block is true.

Syntax –
if(Condition) {
//Statements;
if(Condition) {
//Statements;
}
}
Nested if statement
NestedIfExample.java
public class NestedIf {
public static void main(String args[]) {
int marks = 75;
int age=21
if(age>= 21) {
if(marks >=75){
System.out.println(“Elligible.”);
}
}
}
}

Output - Elligible.
Switch Statement
Switch statement is work just like if-else-if ladder i.e. executes one statement from multiple conditions. There
are following important points about switch statement:

There can be one or n number of cases for switch expression.


The value of cases must be switch expression type and the case value must be literal or constant.
The case value must be unique.
The java switch expression must be of byte, short, int, long (with its Wrapper type), enums and string.
Each case statement can have a break statement which is optional.
The case value can have a default label which is optional.
Switch Statement
Syntax –
switch(expression) {
case value1:
//statements;
break;
case value2:
//statements;
break;
………..
default:
//statements;
(Executes if none of the case
is match)
}
Switch statement
public class SwitchExample2 {
SwitchExample1.java public static void main(String args []) {
char ch = ‘e’;

SwitchExample2.java
public class SwitchExample1 { switch(ch) {
public static void main(String args []) { case ‘a’: System.out.println(“Vowel”); break;
int num = 2; case ‘A’: System.out.println(“Vowel”); break;
switch(num) { case ‘e’: System.out.println(“Vowel”); break;
case 1: System.out.println(“Case-1”); break; case ‘E’: System.out.println(“Vowel”); break;
case 2: System.out.println(“Case-2”); break; case ‘i’: System.out.println(“Vowel”); break;
case 3: System.out.println(“Case-3”); break; case ‘I’: System.out.println(“Vowel”); break;
default: System.out.println(“Invalid”); case ‘o’: System.out.println(“Vowel”); break;
} case ‘O’: System.out.println(“Vowel”); break;
} case ‘u’: System.out.println(“Vowel”); break;
} case ‘U’: System.out.println(“Vowel”); break;
default: System.out.println(“Consonant”);
Output – Case-2 }}}
Output – Vowel
jump Statement
There are three types of Jump Statement in java:

1. Break Statement
2. Continue Statement
3. Return Statement
break Statement
The java break statement is used to break loop or switch statement. Java Break statement breaks the current
flow of the program i.e. in case of inner loop, it breaks only inner loop not outer and control will resumes
at the next statement.
Break statement
BreakLoopDemo.java
// Break to exit a loop
class BreakLoopDemo
{
public static void main(String args[])
{
for (int i = 0; i < 10; i++)
{
Output –
if (i == 5)
i: 0
break;
i: 1
i: 2
System.out.println("i: " + i);
i: 3
}
i: 4
System.out.println("Loop complete.");
Loop complete.
}
}
break Statement
// with label inside an inner loop to break outer loop
Java Break statement with Labeled public class BreakExample3 {
For loop: public static void main(String[] args) {
aa: // Name of label
Java does not have goto statement as for(int i=1;i<=3;i++){
java uses label. We can use break bb: // Name of label
statement with a label. This feature of for(int j=1;j<=3;j++){
break statement introduced since if(i==2&&j==2){
JDK1.5.
break aa;
}
System.out.println(i+" "+j); Output –
} 11
} 12
} 13
} 21
continue Statement
The continue statement is used in loop control structure when you need to jump to the next iteration of the
loop immediately. The Java continue statement is used to continue the loop. It continues the current flow
of the program and skips the remaining code at the specified condition. In case of an inner loop, it
continues the inner loop only.
continue statement
ContinueExample.java
// Continue Statement

public class ContinueExample {


public static void main(String[] args) {
//for loop
for(int i=1;i<=10;i++){ Output –
if(i==5){ 1
continue;//it will skip the rest statement 2
} 3
System.out.println(i); 4
} 6
} 7
} 8
9
As you can see in the above output, 5 is not printed on the console. It is
because the loop is continued when it reaches to 5. 10
continue Statement
// ContinueExample3.java
Java Continue statement with Labeled public class ContinueExample3 {
For loop: public static void main(String[] args) {
aa:
We can use continue statement with a for(int i=1;i<=3;i++){
label. This feature is introduced since bb:
JDK 1.5. So, we can continue any loop for(int j=1;j<=3;j++){
in java now whether it is outer or inner if(i==2&&j==2){ Output –
loop. continue aa; 11
} 12
System.out.println(i+" "+j); 13
} 21
} 31
} 32
} 33
Return Statement
The return statement is used to explicitly return from a method. That is, it causes a program control to transfer
back to the caller of the method.
// Java program to illustrate using return
class ReturnStatement {
int add() {
int result = 10+20;
return result;
}
public static void main(String args[]) {
Abc a = new Abc();
int x = a.add();
System.out.println(x);
}}
Output –
30
Loops in java

In programming, loops are used to execute instruction again & again (i.e. repeatedly) when some condition
become true.

There are three types of loops in java:

•for loop
•while loop
•do-while loop
For Loop
The for loop is used to execute a sequence of statements multiple times. If the number of execution is fixed, it
is recommended to use for loop. The syntax of for statement contains initialization, condition and
increment/decrement in one line with separated by semi-colon.

Syntax –

for(initialization; test-condition; increment/decrement) {


Statements;
}
For Loop
ForLoopExample.java

public class ForLoopExample {


public static void main(String args[]) {
for(int i = 1; i<=5; i++) {
System.out.println(i);
} Output –
} 1
2
} 3
4
5
For Loop
Nested For Loop

A for loop inside another for loop is called nested for loop. In nested for loop, inner loop executes completely
whenever outer loop executes.

Syntax - for(initialization; condition; increment/decrement) {


for(initialization; condition; increment/decrement) {
Statements; }
}
For Loop
NestedForLoopExample.java

public class NestedForLoopExample {


public static void main(String args[]) {
for(int i = 1; i<=2; i++) {
for(int j = 1; j<=2; j++) { // Inner loop
System.out.println(i+” “+j); Output –
} 11
12
} 21
} 22
}
For Loop
Types of For Loop

•For-each or Enhanced For Loop


•Labeled For Loop

For-each Loop - In java for-each loop is used to traverse an array or collection. It returns one by one element
in the defined variable.

Syntax - for(Data Type variable : array ) {


// Statements;
}
For Loop
ForEachLoopExample.java

public class ForEachLoopExample {


public static void main(String args[]) {
int arr[]={12,23,44,56,78};
for(int i:arr){
System.out.println(i); Output –
} 12
23
} 44
} 56
78
For Loop
Labeled For Loop - It is used in nested for loop, to break or continue specific for loop.

Syntax -
labelname;
for(initialization; condition; increment/decrement) {
Statements;
}
For Loop
LabeledForLoopExample.java

public class LabeledForLoopExample {


public static void main(String args[]) {
first:
for(int i=1;i<=3;i++) {
second:
for(int j=1;j<=3;j++) {
if(i==2 && j==2) {
break first; Output –
} 11
System.out.println(i+" "+j); 12
} 13
} 21
}
}
while Loop
It is also used to iterate the part of a program several times. It is recommended to use while loop when the
number of iteration is not fixed.

Syntax –

while(condition) {
Statements;
}
while Loop
// While_Example.java

public class While_Example {


Output –
public static void main(String args[]) { 10
int i=10; 11
12
while(i < 21) { 13
System.out.println(i); 14
15
i++; 16
} 17
18
} 19
} 20
Do-while Loop
It is also used to execute a part of the program several times. It is recommended to use do-while loop, when
number iteration is not fixed & you must have to execute the loop at least once.

Syntax –

do{
Statements;
}while(condition);
Do-while Loop
// Do_While_Example.java

public class Do_While_Example {


public static void main(String args[]) {
int i=10;
do {
System.out.println(i); Output –
10
i++;
11
}while(i<15); 12
13
}
14
}

You might also like