1 of 17

Nested Loops

Lehigh University | CSE 002: Fundamentals of Programming | Lecture 1

Brian Y. Chen and Brian D. Davison

2 of 17

Chapter 4: Nested Loops

  • Loops can be inside the body of another loop

int i = 0, j;

while( i < 5 ){

j=0;

while( j < 6 ){

System.out.println(“Pawn at ” + i +” “ + j);

j++;

}

i++;

}

  • We re-run the middle loop 5 times.

Lehigh University | CSE 002: Fundamentals of Programming | Lecture 1

Brian Y. Chen and Brian D. Davison

3 of 17

Nested Loops can use the same variables

  • Ugly nested loop:

int i = 0;

while( i < 5 ){

i++;

while( i >= 0 ){

System.out.println(“i: ” + i);

i--;

}

}

  • This loop runs forever

Lehigh University | CSE 002: Fundamentals of Programming | Lecture 1

Brian Y. Chen and Brian D. Davison

4 of 17

Nested Loops and breaks

  • Break only breaks the closest loop

int i = 0, j ;

while( i < 5 ){

j=0;

while( j < 6 ){

System.out.println(“Pawn at ” + i + j);

j++;

break; }

i++;

}

  • The interior loop only runs once each time.

Lehigh University | CSE 002: Fundamentals of Programming | Lecture 1

Brian Y. Chen and Brian D. Davison

5 of 17

Nested Loops and breaks

  • Break only breaks the closest loop

int i = 0, j;

while( i < 5 ){

j=0;

while( j < 6 ){

System.out.println(“Pawn at ” + i + j);

j++;

if( j > 2 ) { break; }

}

i++; }

  • Interior loop only ever runs three times.

Lehigh University | CSE 002: Fundamentals of Programming | Lecture 1

Brian Y. Chen and Brian D. Davison

6 of 17

Lab 7, 8 are crucial for understanding loops

  • Shows you how all loops are similar, and yet work in different ways
  • Loops can all be interrupted with break points, but different loops interrupt differently
  • Continues also have different effects

  • Use lab 8 to explore the space of variations that can occur in loops.

Lehigh University | CSE 002: Fundamentals of Programming | Lecture 1

Brian Y. Chen and Brian D. Davison

7 of 17

The didactic purpose of loop conversion

  • When writing new code, you will rarely need to convert a for loop to a while loop.
    • Normally you’ve already picked the most appropriate loop
    • Often you will be maintaining others’ code, and you may have to do such conversions

  • Converting between loops forces you to think about how the loop executes, so that you learn exactly how they work
    • Expect to see loop conversions as possible exam questions

Lehigh University | CSE 002: Fundamentals of Programming | Lecture 1

Brian Y. Chen and Brian D. Davison

8 of 17

Be able to convert loops

  • Example 1:

int i, j;

for(i=0; i<2; i++){

for(j=0; j<3; j++){

..println(“i: ”+i+“j: ”+j);

}

}

  • This is your standard doubly nested loop
  • Straightline code

..println(“i: 0 j: 0);

..println(“i: 0 j: 1);

..println(“i: 0 j: 2);

..println(“i: 1 j: 0);

..println(“i: 1 j: 1);

..println(“i: 1 j: 2);

int i = 2, j = 3;

  • Same output
  • Same variable state after loop

Lehigh University | CSE 002: Fundamentals of Programming | Lecture 1

Brian Y. Chen and Brian D. Davison

9 of 17

Be able to convert loops

  • Example 1:

int i , j;

for(i=0; i<2; i++){

for(j=0; j<3; j++){

..println(“i: ”+i+“j: ”+j);

}

}

  • This is your standard doubly nested loop
  • Converted to while()

int i = 0, j;

while(i<2){

j=0;

while(j<3){

..println(“i: ”+i+“j: ”+j);

j++;

}

i++;

}

  • Move the interators into the loop

Lehigh University | CSE 002: Fundamentals of Programming | Lecture 1

Brian Y. Chen and Brian D. Davison

10 of 17

Natural variations on this kind of question

  • Convert between two kinds of loops and also alter the output to something else (specified)

  • Remove the break/continue statement and convert to another kind of loop, while maintaining the same output

  • Convert do-while to while statements while maintaining the order of the inside of the loop and also maintaining the same output.

Lehigh University | CSE 002: Fundamentals of Programming | Lecture 1

Brian Y. Chen and Brian D. Davison

11 of 17

A general approach

  • Break down the steps that happen in one loop and resequence them into the other loop.

while( test ){ statement; }

do { statements; }

while( test )

for (start; test; increment) {statement;}

TEST

Run Statements

TEST

Run Statements

IF

TRUE

Stop

IF FALSE

IF

TRUE

Stop

If

False

Start

TEST

IF

TRUE

Stop

IF

FALSE

Increment

Run Statements

Lehigh University | CSE 002: Fundamentals of Programming | Lecture 1

Brian Y. Chen and Brian D. Davison

12 of 17

Be able to convert loops

  • Example 1:

int i, j;

for(i=0; i<2; i++){

for(j=0; j<3; j++){

..println(“i: ”+i+“j: ”+j);

}

}

  • This is your standard doubly nested loop
  • Do-while loops

int i = 0, j;

do{

j=0;

do{

..println(“i: ”+i+“j: ”+j);

j++;

}while(j<3);

i++;

}while(i<2);

  • Do-while loops always execute at least once.

Lehigh University | CSE 002: Fundamentals of Programming | Lecture 1

Brian Y. Chen and Brian D. Davison

13 of 17

A more complex conversion

  • Example 2:

int i;

for(i=0; i<5; i++){

if(i%2==0){

..println(“i: ” + i);

}

}

  • Convert this into a while loop without an if statement
  • While loop conversion

int i = 0;

while(i<5){

..println(“i: ” + i);

i += 2;

}

  • Here we control the interator to avoid having to use an if statement

Lehigh University | CSE 002: Fundamentals of Programming | Lecture 1

Brian Y. Chen and Brian D. Davison

14 of 17

A more complex conversion

  • Example 2:

int i;

for(i=0; i<5; i++){

if(i%2==0){

..println(“i: ” + i);

}

}

  • Convert this into a for loop that interates from 0 to 3, but makes the same output
  • For loop conversion

int i;

for(i=0; i<3; i++){

..println(“i: ” + (2*i) );

}

  • To go from 0 to 3, we modify the output function to get the same values

Lehigh University | CSE 002: Fundamentals of Programming | Lecture 1

Brian Y. Chen and Brian D. Davison

15 of 17

Out of order conversion

  • Example 3:

int i;

for(i=0; i<5; i++){

if(i%2==0){

…print(“A”);

}

else{

…print(“B”);

}

}

  • Make this loop output BABAB without changing the if statement
  • Extent conversion

int i;

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

if(i%2==0){

…print(“A”);

}

else{

…print(“B”);

}

}

  • Modify the extents to change the output

Lehigh University | CSE 002: Fundamentals of Programming | Lecture 1

Brian Y. Chen and Brian D. Davison

16 of 17

Convert by adding continue statements

  • Example 3:

int i = 0;

for(i=0; i<5; i++){

if(i%2==0){

…print(“A”);

}

else{

…print(“B”);

}

}

  • Make this loop output AAA by adding continue statements only.
  • Added continue

int i;

for(i=0; i<5; i++){

if(i%2==0){

…print(“A”);

}

else{

continue;

…print(“B”);

} }

  • The continue statement prevents case B

Lehigh University | CSE 002: Fundamentals of Programming | Lecture 1

Brian Y. Chen and Brian D. Davison

17 of 17

Questions

Lehigh University | CSE 002: Fundamentals of Programming | Lecture 1

Brian Y. Chen and Brian D. Davison