141
CONTROL FLOW STATEMENTS IN JAVA

java programming- control statements

Embed Size (px)

DESCRIPTION

control statements of java

Citation preview

Page 1: java programming- control statements

CONTROL FLOW STATEMENTS IN JAVA

Page 2: java programming- control statements

Java – Control Flow Statements

Control Statements

Selection

Iteration

Jump

Page 3: java programming- control statements

Java – Control Flow Statements

Selection

If Statement

Switch Statement

Selection Statements are also called Decision Making Statements

Page 4: java programming- control statements

Java – Control Flow StatementsIf Statements

Simple if

If else

If else if (if else Ladder)

Nested If

Page 5: java programming- control statements

Java – Control Flow Statements If Statement

It is used to take decision based on a single condition

Syntax: if(Condition){

Statements

}

True

False

If condition is True; control will enter the if block

If condition is False; control will execute statement followed by if block

Page 6: java programming- control statements

Java – Control Flow StatementsIf Statement

Start

End

Conditi on

Statements

FalseTrue

Flow Chart:

Page 7: java programming- control statements

Java – Control Flow Statements If Statement

Example: Read marks from user and state only if user is pass.

Page 8: java programming- control statements

Java – Control Flow Statements if-else Statement

It is used to take decision based on a single condition

Syntax: if(Condition){ Statements}Else{ Statements}

TrueFalse

Page 9: java programming- control statements

Java – Control Flow StatementsIf-else Statement

FalseTrue

Flow Chart: Start

End

Conditi on

True Block False Block

Page 10: java programming- control statements

Java – Control Flow StatementsIf-else Statement

If condition is True; control will execute the statement in the true block

If condition is False; control will execute the statement in false block

The if else statement has one condition and two statement blocks- True block and False block

Page 11: java programming- control statements

Java – Control Statements if-else Statement

Example: Read marks from user and state whether student is Pass or Fail

Page 12: java programming- control statements

if(Condition1){ Statements}else{ if(Condition2) { Statements }}

Java – Control Statements if-else-if Statement (Ladder if)

It is used to take decision based on two conditions.

Syntax: else part of if contains another if statement

Page 13: java programming- control statements

Java – Control StatementsIf-else-if Statement (Ladder if)

FalseTrue

Flow Chart: Start

End

Conditi on1

True Block

S t a r t

E n d

C o nd i ti

o n

T r u e B l o c k

F a l s e B l o c k

If within else

Page 14: java programming- control statements

Java – Control Statements

Example:

if-else-if Statement (Ladder if)

Page 15: java programming- control statements

if(Condition1){ Statements}else{ if(Condition2) { Statements }}

Java – Control Statements Nested-if Statement

The Nested if can be inside the if-part or else-part

Syntax: if(Condition1){ if(Condition2) { Statements }

}else{

Statements}

Page 16: java programming- control statements

Java – Control Statements

FalseTrue

Flow Chart: Start

End

Conditi on1

True Block

S t a r t

E n d

C o nd i ti

o n

T r u e B l o c k

F a l s e B l o c k

If within else

Nested-if Statement

Page 17: java programming- control statements

Java – Control Statements

FalseTrue

Flow Chart:

If within else

Nested-if Statement

Start

End

Conditi on

False Block

S t a r t

E n d

C o nd i ti o n

T r u e B l o c k

F a l s e B l o c k

Page 18: java programming- control statements

Java – Control Statements

Example:

Nested-if Statement

Page 19: java programming- control statements

Java – Control Statements

Example:

Nested-if Statement

Page 20: java programming- control statements

switch(expression/variable){Case value: case statements break // optionalCase value:

case statements break// optional

.

.

.

default://optional default statements

}

Java – Control Statements switch Statement

A switch statement is used to test many conditions

Syntax:

Page 21: java programming- control statements

Java – Control Statements

Flow Chart:

switch Statement

Switch Statement without “break”

Case A

Case B

default

False

False

False

Case A Statements

Case B Statements

Case C Statements

Default Statements

Start

Variable or Expression

True

True

True

End

Page 22: java programming- control statements

Java – Control Statements

Flow Chart:

switch Statement

Switch Statement with “break”

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 23: java programming- control statements

Java – Control Statements

Example:

switch Statement

Page 24: java programming- control statements

Java – Control Statements switch Statement

·The variable used in a switch statement can only be a byte, short, int, or char

·You can have any number of case statements within a switch

·Each case is followed by the value to be compared to and a colon

·The value for a case must be the same data type as the variable in the switch and it must be a constant or a literal

·When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached

Page 25: java programming- control statements

Java – Control Statements switch Statement

·When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement

·Not every case needs to contain a break. If no break appears, the flow of control will fall through to subsequent cases until a break is reached

·A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case

Page 26: java programming- control statements

Java – Control Statements

Iterations / Loops

Page 27: java programming- control statements

Java – Control StatementsThe iterati ve statements/ loop executes a block of statements when a parti cular conditi on is true

Each loop has four types of statements :-Initi alizati on-Conditi on checking-Executi on-Increment / Decrement

Page 28: java programming- control statements

Java – Control Statements

Iterations/ Loops

while

do while

for

Types of Loops

Page 29: java programming- control statements

Java – Control Statements

Loops can be categorized on the basis of place where conditi on checking is being done:

Loops

Entry Controlled Exit Controlled

For

While

Do-While

Page 30: java programming- control statements

Java – Control StatementsDiff erence Between Entry Controlled and Exit Controlled Loops

Entry Controlled Exit Controlled

Condition is checked at the entry of the loop

Condition is checked at the exit of the loop

If condition is initially false, the loop never executes

i=1;while(i==0){ System.out.println(“In While loop”); }System.out.println(“out of the loop”);

Output:Out of the loop

If condition is initially false, then also the loop executes at least once

i=1;do{ System.out.println(“In While loop”); } while(i==0);System.out.println(“out of the loop”);

Output:In while loopOut of the loop

Example- for, while Example – do-while

Page 31: java programming- control statements

Java – Control Statements while Loop

Syntax

while(condition){

Statements

}

True

False

Page 32: java programming- control statements

Java – Control Statements while Loop

Example: print values from 1 to ten

Page 33: java programming- control statements

Java – Control Statements while Loop

Example: print values from 1 to ten

Page 34: java programming- control statements

Java – Control Statements while Loop

Functi onality

i=1;

while(i<=10){

System.out.println(i); i=i+1;

}

Initializationstatement

Initialization Statement is used to initialize a variable/ counter.

Page 35: java programming- control statements

Java – Control Statements while Loop

Functi onality

i=1;

while(i<=10){

System.out.println(i); i=i+1;

}

Condition Statement

The condition statement controls the execution of loop

The loop executes till the condition statement is true

Page 36: java programming- control statements

Java – Control Statements while Loop

Functi onality

i=1;

while(i<=10){

System.out.println(i);

i=i+1;

}

Execution StatementThe execution statements are the main body of a loop

All action statements of loop are written here

Page 37: java programming- control statements

Java – Control Statements while Loop

Functi onality

i=1;

while(i<=10){

System.out.println(i);

i=i+1;

}

Increment/ Decrement

This section is used to increment or decrement the variable value

Page 38: java programming- control statements

Java – Control Statements while Loop

Functi onality

i=1;

while(i<=10){

System.out.println(i);

i=i+1;

}

i

1

Variable i in memory

Output

Page 39: java programming- control statements

Java – Control Statements while Loop

Functi onality

i=1;

while(i<=10){

System.out.println(i);

i=i+1;

}

i

1

Variable i in memory

Output Condition is True

Page 40: java programming- control statements

Java – Control Statements while Loop

Functi onality

i=1;

while(i<=10){

System.out.println(i);

i=i+1;

}

i

1

Variable i in memory

1

Output

Check it out here

Page 41: java programming- control statements

Java – Control Statements while Loop

Functi onality

i=1;

while(i<=10){

System.out.println(i);

i=i+1;

}

i

2

Variable i in memory

1

Output

Check it out here

Page 42: java programming- control statements

Java – Control Statements while Loop

Functi onality

i=1;

while(i<=10){

System.out.println(i);

i=i+1;

}

i

2

Variable i in memory

1

Output Condition is checked again

Page 43: java programming- control statements

Java – Control Statements while Loop

Functi onality

i=1;

while(i<=10){

System.out.println(i);

i=i+1;

}

i

2

Variable i in memory

1

Output Condition is True

Page 44: java programming- control statements

Java – Control Statements while Loop

Functi onality

i=1;

while(i<=10){

System.out.println(i);

i=i+1;

}

i

2

Variable i in memory

12

Output

Check it out here

Page 45: java programming- control statements

Java – Control Statements while Loop

Functi onality

i=1;

while(i<=10){

System.out.println(i);

i=i+1;

}

i

3

Variable i in memory

12

Output

Check it out here

Page 46: java programming- control statements

Java – Control Statements while Loop

Functi onality

i=1;

while(i<=10){

System.out.println(i);

i=i+1;

}

i

3

Variable i in memory

12

Output Condition is checked again

Page 47: java programming- control statements

Java – Control Statements while Loop

Functi onality

i=1;

while(i<=10){

System.out.println(i);

i=i+1;

}

i

3

Variable i in memory

12

Output Condition is True

Page 48: java programming- control statements

Java – Control Statements while Loop

Functi onality

i=1;

while(i<=10){

System.out.println(i);

i=i+1;

}

i

3

Variable i in memory

123

Output

Check it out here

Page 49: java programming- control statements

Java – Control Statements while Loop

Functi onality

i=1;

while(i<=10){

System.out.println(i);

i=i+1;

}

i

4

Variable i in memory

123

Output

Check it out here

Page 50: java programming- control statements

Java – Control Statements while Loop

Functi onality

i=1;

while(i<=10){

System.out.println(i);

i=i+1;

}

i

4

Variable i in memory

123

Output Condition is checked again

Page 51: java programming- control statements

Java – Control Statements while Loop

Functi onality

i=1;

while(i<=10){

System.out.println(i);

i=i+1;

}

i

4

Variable i in memory

123

Output Condition is True

Page 52: java programming- control statements

Java – Control Statements while Loop

Functi onality

i=1;

while(i<=10){

System.out.println(i);

i=i+1;

}

i

4

Variable i in memory

1234

Output

Check it out here

Page 53: java programming- control statements

Java – Control Statements while Loop

Functi onality

i=1;

while(i<=10){

System.out.println(i);

i=i+1;

}

i

5

Variable i in memory

1234

Output

Check it out here

Page 54: java programming- control statements

Java – Control Statements while Loop

Functi onality

i=1;

while(i<=10){

System.out.println(i);

i=i+1;

}

i

5

Variable i in memory

1234

Output Condition is checked again

Page 55: java programming- control statements

Java – Control Statements while Loop

This process will continue till the condition become false

Suppose value of i is 9 now

Page 56: java programming- control statements

Java – Control Statements while Loop

Functi onality

i=1;

while(i<=10){

System.out.println(i);

i=i+1;

}

i

9

Variable i in memory

12345678

Output Condition is True

Page 57: java programming- control statements

Java – Control Statements while Loop

Functi onality

i=1;

while(i<=10){

System.out.println(i);

i=i+1;

}

i

9

Variable i in memory

123456789

Output

Check it out here

Page 58: java programming- control statements

Java – Control Statements while Loop

Functi onality

i=1;

while(i<=10){

System.out.println(i);

i=i+1;

}

i

10

Variable i in memory

123456789

Output

Check it out here

Page 59: java programming- control statements

Java – Control Statements while Loop

Functi onality

i=1;

while(i<=10){

System.out.println(i);

i=i+1;

}

i

10

Variable i in memory

123456789

Output Condition is checked again

Page 60: java programming- control statements

Java – Control Statements while Loop

Functi onality

i=1;

while(i<=10){

System.out.println(i);

i=i+1;

}

i

10

Variable i in memory

123456789

Output Condition is Still True

Page 61: java programming- control statements

Java – Control Statements while Loop

Functi onality

i=1;

while(i<=10){

System.out.println(i);

i=i+1;

}

i

10

Variable i in memory

123456789

10

Output

Check it out here

Page 62: java programming- control statements

Java – Control Statements while Loop

Functi onality

i=1;

while(i<=10){

System.out.println(i);

i=i+1;

}

i

11

Variable i in memory

123456789

10

Output

Check it out here

Page 63: java programming- control statements

Java – Control Statements while Loop

Functi onality

i=1;

while(i<=10){

System.out.println(i);

i=i+1;

}

i

11

Variable i in memory

123456789

10

Output Condition is checked again

Page 64: java programming- control statements

Java – Control Statements while Loop

Functi onality

i=1;

while(i<=10){

System.out.println(i);

i=i+1;

}

i

11

Variable i in memory

123456789

10

Output Condition is FALSE

Page 65: java programming- control statements

Java – Control Statements while Loop

Functi onality

i=1;

while(i<=10){

System.out.println(i);

i=i+1;

}

i

11

Variable i in memory

123456789

10

Output

Skip the body of the lop and

executes the statement jast after the loop

Page 66: java programming- control statements

Java – Control Statements while Loop

Functi onality

i=1;

while(i<=10){

System.out.println(i);

i=i+1;

}

i

11

Variable i in memory

123456789

10

Output

Final value of i after the

complete loop

Page 67: java programming- control statements

Java – Control Statements while Loop

Functi onality

i=1;

while(i<=10){

System.out.println(i);

i=i+1;

}

i

11

Variable i in memory

123456789

10

Output Final output of the loop

Page 68: java programming- control statements

Java – Control Statementsfor Loop

Syntax

for( initi alizati on ; conditi on ; increment){

Statements

}

TrueFalse

Page 69: java programming- control statements

Java – Control Statements for Loop

Example: print values from 1 to ten

Page 70: java programming- control statements

Java – Control Statementsfor Loop

Example: print values from 1 to ten

Page 71: java programming- control statements

Java – Control Statementsfor Loop

Functi onality

for(i=1; i<=10; i=i+1){

System.out.println(i);

}

Initializationstatement

Initialization Statement is used to initialize a variable/ counter.

Page 72: java programming- control statements

Java – Control StatementsFor Loop

Functi onality The condition statement controls the execution of loopThe loop executes till the condition statement is true

for(i=1; i<=10; i=i+1){

System.out.println(i);

}

Condition Statement

Page 73: java programming- control statements

for(i=1; i<=10; i=i+1){

System.out.println(i);

}

Java – Control Statements for Loop

Functi onality

Increment/ Decrement

This section is used to increment or decrement the variable value

Page 74: java programming- control statements

for(i=1; i<=10; i=i+1){

System.out.println(i);

}

Java – Control Statements for Loop

Functi onality

Execution Statement

The execution statements are the main body of a loopAll action statements of loop are written here

Page 75: java programming- control statements

for(i=1; i<=10; i=i+1){System.out.println(i);

}

Java – Control Statements for Loop

Functi onality

i

1

Variable i in memory

Output Initialization

Page 76: java programming- control statements

for(i=1; i<=10; i=i+1){System.out.println(i);

}

Java – Control Statements for Loop

Functi onality

i

1

Variable i in memory

OutputCondition

is True

Page 77: java programming- control statements

for(i=1; i<=10; i=i+1){System.out.println(i);

}

Java – Control Statements for Loop

Functi onality

i

1

Variable i in memory

1

Output

Check it out here

Page 78: java programming- control statements

Java – Control Statements for Loop

Functi onality

i

2

Variable i in memory

1

Output

Check it out here

for(i=1; i<=10; i=i+1){System.out.println(i);

}

increment

Page 79: java programming- control statements

for(i=1; i<=10; i=i+1){System.out.println(i);

}

Java – Control Statements for Loop

Functi onality

i

2

Variable i in memory

1

OutputCondition is

checked again

Page 80: java programming- control statements

for(i=1; i<=10; i=i+1){System.out.println(i);

}

Java – Control Statements for Loop

Functi onality

i

2

Variable i in memory

1

OutputCondition

is True

Page 81: java programming- control statements

for(i=1; i<=10; i=i+1){System.out.println(i);

}

Java – Control Statements for Loop

Functi onality

i

2

Variable i in memory

12

Output

Check it out here

Page 82: java programming- control statements

Java – Control Statements for Loop

Functi onality

i

3

Variable i in memory

12

Output

Check it out here

for(i=1; i<=10; i=i+1){System.out.println(i);

}

Page 83: java programming- control statements

for(i=1; i<=10; i=i+1){System.out.println(i);

}

Java – Control Statements for Loop

Functi onality

i

3

Variable i in memory

12

OutputCondition is

checked again

Page 84: java programming- control statements

for(i=1; i<=10; i=i+1){System.out.println(i);

}

Java – Control Statements for Loop

Functi onality

i

3

Variable i in memory

12

OutputCondition

is True

Page 85: java programming- control statements

for(i=1; i<=10; i=i+1){System.out.println(i);

}

Java – Control Statements for Loop

Functi onality

i

3

Variable i in memory

123

Output

Check it out here

Page 86: java programming- control statements

for(i=1; i<=10; i=i+1){System.out.println(i);

}

Java – Control Statements for Loop

Functi onality

i

4

Variable i in memory

123

Output

Check it out here

Page 87: java programming- control statements

for(i=1; i<=10; i=i+1){System.out.println(i);

}

Java – Control Statements for Loop

Functi onality

i

4

Variable i in memory

123

OutputCondition is

checked again

Page 88: java programming- control statements

for(i=1; i<=10; i=i+1){System.out.println(i);

}

Java – Control Statements for Loop

Functi onality

i

4

Variable i in memory

123

OutputCondition

is True

Page 89: java programming- control statements

for(i=1; i<=10; i=i+1){System.out.println(i);

}

Java – Control Statements for Loop

Functi onality

i

4

Variable i in memory

1234

Output

Check it out here

Page 90: java programming- control statements

Java – Control Statements for Loop

Functi onality

i

5

Variable i in memory

1234

Output

Check it out here

for(i=1; i<=10; i=i+1){System.out.println(i);

}

Page 91: java programming- control statements

for(i=1; i<=10; i=i+1){System.out.println(i);

}

Java – Control Statements for Loop

Functi onality

i

5

Variable i in memory

1234

Output Condition is checked again

Page 92: java programming- control statements

Java – Control Statements for Loop

This process will continue till the condition become false

Suppose value of i is 9 now

Page 93: java programming- control statements

for(i=1; i<=10; i=i+1){System.out.println(i);

}

Java – Control Statements for Loop

Functi onality

i

9

Variable i in memory

12345678

OutputCondition

is True

Page 94: java programming- control statements

for(i=1; i<=10; i=i+1){System.out.println(i);

}

Java – Control Statements for Loop

Functi onality

i

9

Variable i in memory

123456789

Output

Check it out here

Page 95: java programming- control statements

Java – Control Statements for Loop

Functi onality

i

10

Variable i in memory

123456789

Output

Check it out here

for(i=1; i<=10; i=i+1){System.out.println(i);

}

Page 96: java programming- control statements

for(i=1; i<=10; i=i+1){System.out.println(i);

}

Java – Control Statements for Loop

Functi onality

i

10

Variable i in memory

123456789

OutputCondition is

checked again

Page 97: java programming- control statements

for(i=1; i<=10; i=i+1){System.out.println(i);

}

Java – Control Statements for Loop

Functi onality

i

10

Variable i in memory

123456789

Output

Condition is Still True

Page 98: java programming- control statements

for(i=1; i<=10; i=i+1){System.out.println(i);

}

Java – Control Statements for Loop

Functi onality

i

10

Variable i in memory

123456789

10

Output

Check it out here

Page 99: java programming- control statements

Java – Control Statements for Loop

Functi onality

i

11

Variable i in memory

123456789

10

Output

Check it out here

for(i=1; i<=10; i=i+1){System.out.println(i);

}

Page 100: java programming- control statements

for(i=1; i<=10; i=i+1){System.out.println(i);

}

Java – Control Statements for Loop

Functi onality

i

11

Variable i in memory

123456789

10

OutputCondition is

checked again

Page 101: java programming- control statements

for(i=1; i<=10; i=i+1){System.out.println(i);

}

Java – Control Statementsfor Loop

Functi onality

i

11

Variable i in memory

123456789

10

Output Condition is FALSE

Page 102: java programming- control statements

for(i=1; i<=10; i=i+1){System.out.println(i);

}

Java – Control Statements for Loop

Functi onality

i

11

Variable i in memory

123456789

10

Output

Skip the body of the lop and

executes the statement just after the loop

Page 103: java programming- control statements

Java – Control Statements for Loop

Functi onality

i

11

Variable i in memory

123456789

10

Output

Final value of i after the

complete loop

for(i=1; i<=10; i=i+1){System.out.println(i);

}

Page 104: java programming- control statements

for(i=1; i<=10; i=i+1){System.out.println(i);

}

Java – Control Statements for Loop

Functi onality

i

11

Variable i in memory

123456789

10

Output Final output of the loop

Page 105: java programming- control statements

Java – Control Statements do-while Loop

Syntax

while(condition){

Statements

}

True

False

Page 106: java programming- control statements

Java – Control Statements do-while Loop

Example: print values from 1 to ten

Page 107: java programming- control statements

Java – Control Statements do-while Loop

Example: print values from 1 to ten

Page 108: java programming- control statements

Java – Control Statements do-while Loop

Functi onality

i=1;do{

System.out.println(i);i=i+1;

}while(i<=10);

InitializationstatementInitialization Statement is used to initialize a variable/ counter.

Page 109: java programming- control statements

i=1;do{

System.out.println(i);i=i+1;

}while(i<=10);

Java – Control Statements do-while Loop

Functi onality

Condition Statement

The condition statement controls the execution of loop

The loop executes till the condition statement is true

Page 110: java programming- control statements

i=1;do{

System.out.println(i);i=i+1;

}while(i<=10);

Java – Control Statements do-while Loop

Functi onality

Execution StatementThe execution statements are the main body of a loop

All action statements of loop are written here

Page 111: java programming- control statements

i=1;do{

System.out.println(i);i=i+1;

}while(i<=10);

Java – Control Statements do-while Loop

Functi onality

Increment/ Decrement

This section is used to increment or decrement the variable value

Page 112: java programming- control statements

Java – Control Statements do-while Loop

Functi onality

i

1

Variable i in memory

Output

i=1;do{

System.out.println(i);i=i+1;

}while(i<=10);

Page 113: java programming- control statements

Java – Control Statements do-while Loop

Functi onality

i

1

Variable i in memory

OutputNo condition

checking at entry of the loop

i=1;do{

System.out.println(i);i=i+1;

}while(i<=10);

Page 114: java programming- control statements

i=1;do{

System.out.println(i);i=i+1;

}while(i<=10);

Java – Control Statements do-while Loop

Functi onality

i

1

Variable i in memory

1

Output

Check it out here

Page 115: java programming- control statements

Java – Control Statements do-while Loop

Functi onality

i

2

Variable i in memory

1

Output

Check it out here

i=1;do{

System.out.println(i);i=i+1;

}while(i<=10);

Page 116: java programming- control statements

i=1;do{

System.out.println(i);i=i+1;

}while(i<=10);

Java – Control Statements do-while Loop

Functi onality

i

2

Variable i in memory

1

Output

Condition is checked again

Page 117: java programming- control statements

i=1;do{

System.out.println(i);i=i+1;

}while(i<=10);

Java – Control Statements do-while Loop

Functi onality

i

2

Variable i in memory

1

Output

Condition is True

Page 118: java programming- control statements

i=1;do{

System.out.println(i);i=i+1;

}while(i<=10);

Java – Control Statements do-while Loop

Functi onality

i

2

Variable i in memory

12

Output

Check it out here

Page 119: java programming- control statements

Java – Control Statements do-while Loop

Functi onality

i

3

Variable i in memory

12

Output

Check it out here

i=1;do{

System.out.println(i);i=i+1;

}while(i<=10);

Page 120: java programming- control statements

i=1;do{

System.out.println(i);i=i+1;

}while(i<=10);

Java – Control Statements do-while Loop

Functi onality

i

3

Variable i in memory

12

Output

Condition is checked again

Page 121: java programming- control statements

i=1;do{

System.out.println(i);i=i+1;

}while(i<=10);

Java – Control Statements do-while Loop

Functi onality

i

3

Variable i in memory

12

Output

Condition is True

Page 122: java programming- control statements

i=1;do{

System.out.println(i);i=i+1;

}while(i<=10);

Java – Control Statements do-while Loop

Functi onality

i

3

Variable i in memory

123

Output

Check it out here

Page 123: java programming- control statements

Java – Control Statements do-while Loop

Functi onality

i

4

Variable i in memory

123

Output

Check it out here

i=1;do{

System.out.println(i);i=i+1;

}while(i<=10);

Page 124: java programming- control statements

i=1;do{

System.out.println(i);i=i+1;

}while(i<=10);

Java – Control Statements do-while Loop

Functi onality

i

4

Variable i in memory

123

Output

Condition is checked again

Page 125: java programming- control statements

i=1;do{

System.out.println(i);i=i+1;

}while(i<=10);

Java – Control Statements do-while Loop

Functi onality

i

4

Variable i in memory

123

Output

Condition is True

Page 126: java programming- control statements

i=1;do{

System.out.println(i);i=i+1;

}while(i<=10);

Java – Control Statements do-while Loop

Functi onality

i

4

Variable i in memory

1234

Output

Check it out here

Page 127: java programming- control statements

Java – Control Statements do-while Loop

Functi onality

i

5

Variable i in memory

1234

Output

Check it out here

i=1;do{

System.out.println(i);i=i+1;

}while(i<=10);

Page 128: java programming- control statements

i=1;do{

System.out.println(i);i=i+1;

}while(i<=10);

Java – Control Statements do-while Loop

Functi onality

i

5

Variable i in memory

1234

Output

Condition is checked again

Page 129: java programming- control statements

Java – Control Statements do-while Loop

This process will continue till the condition become false

Suppose value of i is 9 now

Page 130: java programming- control statements

i=1;do{

System.out.println(i);i=i+1;

}while(i<=10);

Java – Control Statements do-while Loop

Functi onality

i

9

Variable i in memory

12345678

Output

Condition is True

Page 131: java programming- control statements

i=1;do{

System.out.println(i);i=i+1;

}while(i<=10);

Java – Control Statements do-while Loop

Functi onality

i

9

Variable i in memory

123456789

Output

Check it out here

Page 132: java programming- control statements

Java – Control Statements do-while Loop

Functi onality

i

10

Variable i in memory

123456789

Output

Check it out here

i=1;do{

System.out.println(i);i=i+1;

}while(i<=10);

Page 133: java programming- control statements

i=1;do{

System.out.println(i);i=i+1;

}while(i<=10);

Java – Control Statements do-while Loop

Functi onality

i

10

Variable i in memory

123456789

Output

Condition is checked again

Page 134: java programming- control statements

i=1;do{

System.out.println(i);i=i+1;

}while(i<=10);

Java – Control Statements do-while Loop

Functi onality

i

10

Variable i in memory

123456789

Output

Condition is Still True

Page 135: java programming- control statements

i=1;do{

System.out.println(i);i=i+1;

}while(i<=10);

Java – Control Statements do-while Loop

Functi onality

i

10

Variable i in memory

123456789

10

Output

Check it out here

Page 136: java programming- control statements

i=1;do{

System.out.println(i);i=i+1;

}while(i<=10);

Java – Control Statements do-while Loop

Functi onality

i

11

Variable i in memory

123456789

10

Output

Check it out here

Page 137: java programming- control statements

i=1;do{

System.out.println(i);i=i+1;

}while(i<=10);

Java – Control Statements do-while Loop

Functi onality

i

11

Variable i in memory

123456789

10

Output Condition is checked again

Page 138: java programming- control statements

i=1;do{

System.out.println(i);i=i+1;

}while(i<=10);

Java – Control Statements do-while Loop

Functi onality

i

11

Variable i in memory

123456789

10

Output

Condition is FALSE

Page 139: java programming- control statements

i=1;do{

System.out.println(i);i=i+1;

}while(i<=10);

Java – Control Statements do-while Loop

Functi onality

i

11

Variable i in memory

123456789

10

Output

Stop the execution of the

loop and executes the

statement just after the loop

Page 140: java programming- control statements

Java – Control Statements do-while Loop

Functi onality

i

11

Variable i in memory

123456789

10

Output

Final value of i after the

complete loop

i=1;do{

System.out.println(i);i=i+1;

}while(i<=10);

Page 141: java programming- control statements

Java – Control Statements do-while Loop

Functi onality

i

11

Variable i in memory

123456789

10

Output

i=1;do{

System.out.println(i);i=i+1;

}while(i<=10);

Final output of the loop