Presented By: Control Statements in Java
Presented By: Control Statements in Java
Presented By: Control Statements in Java
Presented By
MANISH KUMAR SINGH
M.Sc. Computational Science and Applications
About control statement
For Control the order of execution of the program,
based on logic and values, we use control
statements.
TYPES OF CONTROL STATEMENT
• Selection
• Iteration
• Jump
Outlines
Control Statements
Selection
Iteration
Jump
3
Selection Statements
▪ Selection Statements are also called Decision Making Statements. It
allows us to control the flow of program execution on the basis of
the outcome of an expression (that is condition)
Selection
Selection Statements
Switch Statement
4
if Statements
if Statements
Simple if
if else
if- else- if
Nested if
5
Simple if
Syntax :
if (condition)
{
statement1;
}
Purpose: The statements will be evaluated if the value of the condition is true.
6
Simple if
Flow Chart: Start
True False
Condition
Statements
End
7
Example
Class greater1
{
public static void main (string[] args)
{
int a=10,b=20;
if (a>b)
{
system.out.println(“a is greater”);
}
}
}
if else
Syntax :
if (condition)
{
statement1;
}
else
{
statement2;
}
Purpose: The statement 1 is evaluated if the value of the condition is true otherwise
statement 2 is true.
9
if else
Flow Chart:
True False
Condition
Statement 1 Statement 2
End
10
Example
class Greatertwo
{
public static void main(string[] args)
{
int a=10,b=20;
if(a>b)
{
system.out.println(“a is greater);
}
else
{
system.out.println(“b is greater”);
}
}
}
If-else-if Ladder
Syntax :
if(condition)
statements;
else if(condition)
statements;
else if(condition)
statements;
...
...
else
statements;
12
Example
Class Greater1
{
public static void main(string[] args)
{
int a=10,b=20,c=30
if(a>b)
{
if(a>c)
{
system.out.println(“a is greater”);
}
else
{
system.out.println(“c is greater”);
}
}
else if(b>c)
{
system.out.println(“b is greater”);
}
}
}
Nested if
• A nested if is an if statement that is the target of another if or else.
• Nested ifs are very common in programming.
Syntax :
if(condition)
{
if(condition)
statements....
else
statements....
}
else
{
if(condition)
statements.... else
statements....
}
14
Example
Class leapyear1
{
{
system.out.println(“leap year”);
public static void main(string[] args)
}
{
}
int year=2016
else
if(year%4 == 0)
{
{
system.out.println(“not a leap year’);
if(year%100 ==0)
}
{
if(year%400 == 0)
}
{
system.out.println(“leap year”)
}
}
else
{
system.out.println(“not leap year”);
}
}
else
switch
Syntax :
switch (expression)
{
case value 1 :
statement 1 ; break;
case value 2 :
statement 2 ; break;
...
...
case value N :
statement N ; break;
default :
statements ; break;
}
Purpose: The statements N will be evaluated if the value of the logical expression is true.
16
switch
Start
Flow Chart:
Variable or Expression
… Case C Statements
True break;
False
End
17
Example
18
Iteration Statements
Iterations/ Loops
Each loop has four types of
statements :
while
✓ Initialization
✓ Condition checking
✓ Execution
✓ Increment / Decrement do while
for
19
while
Syntax:
initialization
while(test condition)
{
statements;
increment/decrement;
}
Purpose: To evaluate the statements from initial value to final value with given
increment/decrement.
20
Example
▪ print values from 1 to 10
class while1
Output :
{
1
public static void main(String args[])
2
{
3
int i=1;
4
while(i<=10)
5
{
6
System.out.println("\n" + i);
7
i++;
8
}
9
}
10
}
21
do while
Syntax: m=1
initialization do do
{ {
statements; System.out.println(m);
increment/decrement; m=m+1;
} }
while(test condition); while(m==20);
Purpose: To evaluate the statements from initial value to final value with given
increment/decrement.
22
Example
Class Test
{
public static void main(string arg[])
{
int i=2;
do
{
system.out.println(i);
i=i+2;
}
while(i<=100);
}
}
for
Syntax:
for(initialization;finalvalue;increment/decrement) for(m=1;m<=20;m=m+1)
{ {
statements; System.out.println(m);
} }
Purpose: To evaluate the statements from initial value to final value with given
increment/decrement.
24
Example
Class Test
{
public static void main(string arg[])
{
int i;
for(i=2,i<=100,i=i+2)
{
system.out.println(i);
}
}
}
Example
class for1
{
public static void main(String args[])
{
int i;
for (i=0;i<5;i++)
{
System.out.println("\nExample of for loop ");
}
}
Output :
Example of for loop
Example of for loop
Example of for loop
Example of for loop
Example of for loop 26
Jump Statements
Jump
break
continue
return
27
The break statement
✓This statement is used to jump out of a loop.
✓Break statement was previously used in switch – case statements.
✓On encountering a break statement within a loop, the execution
continues with the next statement outside the loop.
✓The remaining statements which are after the break and within the
loop are skipped.
28
Example
class break1 Output :
{ 1
public static void main(String args[]) 2
{ 3
int i = 1; 4
while (i<=10)
{
System.out.println("\n" + i); i++;
if (i==5)
{
break;
}
}
}
}
29
continue Statement
✓ This statement is used only within looping statements.
✓ The remaining statements in the loop are skipped. The execution starts from the
30
Example
class continue1 Output : 1
{ 3
public static void main(String rags[]) 5
{ 7
for (int i=1; i<=10; i++) 9
{
if (i%2 == 0)
continue;
System.out.println("\n" + i);
}
}
}
31
The return Statement
✓ The last control statement is return. The return statement is used to
explicitly return from a method.
✓ That is, it causes program control to transfer back to the caller of the
method.
✓ The return statement immediately terminates the method in which it is
executed.
32
Example
class Return1
{
public static void main(String rags[])
{
boolean t = true; System.out.println("Before the Output :
return."); if(t) Before the return.
return; // return to caller
System.out.println("This won't execute.");
}
}
33
Thank You