csj-161127083146power point presentation

deepayaganti1 11 views 32 slides Sep 19, 2024
Slide 1
Slide 1 of 32
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32

About This Presentation

string


Slide Content

Presented By
Niloy Saha
Control Statements in Java
Departmentof Computer Science &Engineering

Control Statements
Selection
Iteration
Jump
Outlines
2

Selection
Selection Statements
Switch Statement
Selection Statements are also called Decision Making Statements.
Selection Statements
3

if Statements
Simple if
if else
if-else-if Ladder
Nested if
if Statements
4

Simple if
Syntax:
if(condition)
{
statement1;
}
Purpose:Thestatementswillbeevaluatedifthevalueoftheconditionistrue.
5

Simple if
Start
End
Condition
Statements
False
True
Flow Chart:
6

Example
7

if else
Syntax:
if(condition)
{
statement1;
}
else
{
statement2;
}
Purpose:Thestatement1isevaluatedifthevalueoftheconditionistrueotherwise
statement2istrue.
8

if else
False
True
Flow Chart:
Start
End
Condition
Statement 1 Statement 2
9

Example
10

If-else-if Ladder
Syntax :
if(condition)
statements;
else if(condition)
statements;
else if(condition)
statements;
...
...
else
statements;
11

Examples
import java.util.Scanner;
class Day
{
public static void main(String args[])
{
Scanner s = new Scanner(System.in);
System.out.println("Enetday between 0 to 6 Day = ");
intday = s.nextInt();
if (day == 0)
{
System.out.println("\n Sunday");
}
else if (day == 1)
{
System.out.println("\n Monday");
}
else if (day == 2)
{
System.out.println("\n Tuesday");
}
else if (day == 3)
{
System.out.println("\n Wednesday");
}
else if (day == 4)
{
System.out.println("\n Thursday");
}
else if (day == 5)
{
System.out.println("\n Friday");
}
else
{
System.out.println("\n Saturday");
}
}
}
12

Nested if
•A nested if is an if statement that is the target of another if or else.
•Nested ifs are verycommon in programming.
Syntax :
if(condition)
{
if(condition)
statements....
else
statements....
}
else
{
if(condition)
statements....
else
statements....
}
13

Example
14

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.
15

switch
Flow Chart:
Case A
Case B

default
False
False
False
Case A Statements
break;
Case B Statements
break;
Case C Statements
break;
Default Statements
Start
Variable or Expression
True
True
True
End
16

Example
17

Iteration Statements
Iterations/ Loops
while
do while
for
Each loop has four types of
statements :
Initialization
Condition checking
Execution
Increment / Decrement
18

while
Syntax:
initialization
while(final value)
{
statements;
increment/decrement;
}
Purpose: To evaluate the statements from initial value to final value with given
increment/decrement.
m=1
while(m<=20)
{
System.out.println(m);
m=m+1;
}
19

Example
class while1
{
public static void main(String args[])
{
inti=1;
while(i<=10)
{
System.out.println("\n" + i);
i++;
}
}
}
Output :
1
2
3
4
5
6
7
8
9
10
print values from 1 to 10
20

do while
Syntax:
initialization
do
{
statements;
increment/decrement;
}
while(finalvalue);
Purpose:Toevaluatethestatementsfrominitialvaluetofinalvaluewithgiven
increment/decrement.
m=1
do
{
System.out.println(m);
m=m+1;
}
while(m==20);
21

Example
class dowhile1
{
public static void main(String args[])
{
inti= 1;
intsum = 0;
do
{
sum = sum + i;
i++;
}while (i<=10);
System.out.println("\n\n\tThesum of 1 to 10 is .. " + sum);
}
}
Output :
The sum of 1 to 10 is .. 55 22

for
Syntax:
for(initialization;finalvalue;increment/decrement)
{
statements;
}
Purpose:Toevaluatethestatementsfrominitialvaluetofinalvaluewithgiven
increment/decrement.
for(m=1;m<=20;m=m+1)
{
System.out.println(m);
}
23

Example
class for1
{
public static void main(String args[])
{
inti;
for (i=0;i<5;i++)
{
System.out.println("\nExampleof for loop ");
}
}
Output :
Example of for loop
Example of for loop
Example of for loop
Example of for loop
Example of for loop
24

Jump Statements
Jump
break
continue
return
25

The break statement
Thisstatementisusedtojumpoutofaloop.
Breakstatementwaspreviouslyusedinswitch–casestatements.
Onencounteringabreakstatementwithinaloop,theexecutioncontinueswiththenext
statementoutsidetheloop.
Theremainingstatementswhichareafterthebreakandwithintheloopareskipped.
Breakstatementcanalsobeusedwiththelabelofastatement.
Astatementcanbelabeledasfollows.
statementName:SomeJavaStatement
Whenweusebreakstatementalongwithlabelas,
breakstatementName;
26

Example
class break1
{
public static void main(String args[])
{
inti= 1;
while (i<=10)
{
System.out.println("\n" + i);
i++;
if (i==5)
{
break;
}
}
}
}
Output :
1
2
3
4
27

continue Statement
Thisstatementisusedonlywithinloopingstatements.
Whenthecontinuestatementisencountered,thenextiterationstarts.
Theremainingstatementsintheloopareskipped.Theexecutionstartsfromthe
topofloopagain.
28

Example
class continue1
{
public static void main(String args[])
{
for (inti=1; i<1=0; i++)
{
if (i%2 == 0)
continue;
System.out.println("\n" + i);
}
}
}
Output :
1
3
5
7
9
29

The return Statement
Thelastcontrolstatementisreturn.Thereturnstatementisusedto
explicitlyreturnfromamethod.
Thatis,itcausesprogramcontroltotransferbacktothecallerofthe
method.
Thereturnstatementimmediatelyterminatesthemethodinwhichitis
executed.
30

Example
class Return1
{
public static void main(String args[])
{
booleant = true;
System.out.println("Before the return.");
if(t)
return; // return to caller
System.out.println("This won't execute.");
}
}
Output :
Before the return.
31

Thank You
Tags