KEMBAR78
Control Statements | PDF | Control Flow | Software Engineering
0% found this document useful (0 votes)
15 views31 pages

Control Statements

Uploaded by

k2768243
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views31 pages

Control Statements

Uploaded by

k2768243
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Outlines

Control Statements

Selection/Decision

Iteration/Looping

Jumping

1
Selection Statements
 Selection Statements are also called Decision Making Statements.

Selection

Selection Statements

Switch Statement
2
if Statements
if Statements

Simple if

if else

else- if Ladder

Nested if
3
Simple if
Syntax :

if (condition)
{
statement1;
}

Purpose: The statements will be evaluated if the value of the condition is true.

4
Simple if
Flow Chart: Start

True False
Condition

Statements

End
5
Example

6
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 evaluated.

7
if else
Flow Chart: Start

True False
Condition

Statement 1 Statement 2

End

8
Example

9
else-if Ladder
Syntax :

if(condition)
statements;
else if(condition)
statements;
else if(condition)
statements;
...
...
else
statements;

10
Examples else if (day == 3)
import java.util.Scanner; {
class Day System.out.println("\n Wednesday");
{ }
public static void main(String args[]) else if (day == 4)
{ {
Scanner s = new Scanner(System.in); System.out.println("\n Thursday");
System.out.println("Enterday between 0 to 6 Day = "); }
int day = s.nextInt(); else if (day == 5)
if (day == 0) {
{ System.out.println("\n Friday");
System.out.println("\n Sunday"); }
} else if (day==6)
else if (day == 1) {
{ System.out.println("\n Saturday");
System.out.println("\n Monday"); }
} else
else if (day == 2) {
{ System.out.println("\n Wrong input");
System.out.println("\n Tuesday");
} }
}
11
}
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....
}
12
Example

13
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.
14
switch
Start
Flow Chart:
Variable or Expression

Case A Case A Statements


break;
True
False

Case B Case B Statements


True
break;
False

… Case C Statements
True break;
False

default Default Statements

End
15
Example

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

for

17
while
Syntax:
m=1
initialization while(m<=20)
while(final value) {
{ System.out.println(m);
statements; m=m+1;
increment/decrement; }
}

Purpose: To evaluate the statements from initial value to final value with given
increment/decrement.

18
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
}

19
do while
Syntax:
initialization m=1
do do
{ {
statements; System.out.println(m);
increment/decrement; m=m+1;
} }
while(final value); while(m==20);

Purpose: To evaluate the statements from initial value to final value with given
increment/decrement.

20
Example
class dowhile1
{
public static void main(String args[])
{
int i = 1;
int sum = 0;
do
{ // sum 0, 1,3,6,10,,15,21,28,,36,45,55
// I =1 ,2,3,4,5,6,7,8,9,10,11
sum = sum + i;
i++;
}while (i<=10);
System.out.println("\n\n\tThe sum of 1 to 10 is .. " + sum);
}
}

Output :
The sum of 1 to 10 is .. 55 22
for
Syntax:

for(initialization; condition; increment/decrement) for(m=1;m<=20;m++)


{ {
statements; System.out.println(m);
} }

Purpose: To evaluate the statements from initial value to final value with given
increment/decrement.

22
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
23
Jump Statements
Jump

break

continue

return
24
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.
 Break statement can also be used with the label of a statement.
 A statement can be labeled as follows.

statementName : SomeJavaStatement

 When we use break statement along with label as,

break statementName;

25
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;
}
}
}
}
26
continue Statement
 This statement is used only within looping statements.

 When the continue statement is encountered, the next iteration starts.

 The remaining statements in the loop are skipped. The execution starts from the

top of loop again.

27
Example
class continue1
{ public static void main(String args[]) Output :
{ 1
for (int i=1; i<10; i++) 3
{ 5
if (i%2 == 0) 7
{ 9
continue;
}

System.out.println("\n" + i);
}
}
}

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

29
Example
class Return1
{
public int show(int c, int d)
{
return c+d; Output :
} 30
public static void main(String args[])
{
Return1 obj = new Return1();

System.out.println(" ---"+obj.show(10,20));
}
}

30
Thank You

You might also like