control statements in java

32
Presented By Niloy Saha Control Statements in Java artment of Computer Science & Engineer

Upload: niloy-saha

Post on 14-Apr-2017

164 views

Category:

Engineering


0 download

TRANSCRIPT

Page 1: Control Statements in Java

Presented By Niloy Saha

Control Statements in Java

Department of Computer Science & Engineering

Page 2: Control Statements in Java

2

Control Statements

Selection

Iteration

Jump

Outlines

Page 3: Control Statements in Java

3

Selection

Selection Statements

Switch Statement

Selection Statements are also called Decision Making Statements.

Selection Statements

Page 4: Control Statements in Java

4

if Statements

Simple if

if else

if- else- if Ladder

Nested if

if Statements

Page 5: Control Statements in Java

5

Simple if Syntax :

if (condition){ statement1;}

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

Page 6: Control Statements in Java

6

Simple ifStart

End

Condition

Statements

FalseTrue

Flow Chart:

Page 7: Control Statements in Java

7

Example

Page 8: Control Statements in Java

8

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.

Page 9: Control Statements in Java

9

if else

FalseTrue

Flow Chart: Start

End

Condition

Statement 1 Statement 2

Page 10: Control Statements in Java

10

Example

Page 11: Control Statements in Java

11

If-else-if LadderSyntax :

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

Page 12: Control Statements in Java

12

Examplesimport java.util.Scanner;class Day{ public static void main(String args[]) { Scanner s = new Scanner(System.in); System.out.println("Enet day between 0 to 6 Day = "); int day = 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"); } }}

Page 13: Control Statements in Java

13

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

Page 14: Control Statements in Java

14

Example

Page 15: Control Statements in Java

15

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.

Page 16: Control Statements in Java

16

switchFlow Chart:

Case A

Case B

default

False

False

False

Case A Statementsbreak;

Case B Statementsbreak;

Case C Statementsbreak;

Default Statements

Start

Variable or Expression

True

True

True

End

Page 17: Control Statements in Java

17

Example

Page 18: Control Statements in Java

18

Iteration StatementsIterations/ Loops

while

do while

for

Each loop has four types of statements :

Initialization Condition checking Execution Increment / Decrement

Page 19: Control Statements in Java

19

whileSyntax:

initializationwhile(final value){ statements; increment/decrement;}

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

m=1while(m<=20){ System.out.println(m); m=m+1;}

Page 20: Control Statements in Java

20

Exampleclass while1{ public static void main(String args[]) { int i=1; while(i<=10) { System.out.println("\n" + i); i++; } }}

Output :12345678910

print values from 1 to 10

Page 21: Control Statements in Java

21

do whileSyntax:

initializationdo{ statements; increment/decrement;} while(final value);

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

m=1do{ System.out.println(m); m=m+1;}while(m==20);

Page 22: Control Statements in Java

22

Exampleclass dowhile1{ public static void main(String args[]) { int i = 1; int sum = 0; do { 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

Page 23: Control Statements in Java

23

forSyntax:

for(initialization;final value;increment/decrement){ statements;}

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

for(m=1;m<=20;m=m+1){ System.out.println(m);}

Page 24: Control Statements in Java

24

Exampleclass 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

Page 25: Control Statements in Java

25

Jump StatementsJump

break

continue

return

Page 26: Control Statements in Java

26

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;

Page 27: Control Statements in Java

27

Exampleclass break1{ public static void main(String args[]) { int i = 1; while (i<=10) { System.out.println("\n" + i); i++; if (i==5) { break; } } }}

Output :1234

Page 28: Control Statements in Java

28

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.

Page 29: Control Statements in Java

29

Exampleclass continue1{ public static void main(String args[]) { for (int i=1; i<1=0; i++) { if (i%2 == 0) continue; System.out.println("\n" + i); } }}

Output :13579

Page 30: Control Statements in Java

30

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.

Page 31: Control Statements in Java

31

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

Output :Before the return.

Page 32: Control Statements in Java

Thank You