Nested Loops
Lehigh University | CSE 002: Fundamentals of Programming | Lecture 1
Brian Y. Chen and Brian D. Davison
Chapter 4: Nested Loops
int i = 0, j;
while( i < 5 ){
j=0;
while( j < 6 ){
System.out.println(“Pawn at ” + i +” “ + j);
j++;
}
i++;
}
Lehigh University | CSE 002: Fundamentals of Programming | Lecture 1
Brian Y. Chen and Brian D. Davison
Nested Loops can use the same variables
int i = 0;
while( i < 5 ){
i++;
while( i >= 0 ){
System.out.println(“i: ” + i);
i--;
}
}
Lehigh University | CSE 002: Fundamentals of Programming | Lecture 1
Brian Y. Chen and Brian D. Davison
Nested Loops and breaks
int i = 0, j ;
while( i < 5 ){
j=0;
while( j < 6 ){
System.out.println(“Pawn at ” + i + j);
j++;
break; }
i++;
}
Lehigh University | CSE 002: Fundamentals of Programming | Lecture 1
Brian Y. Chen and Brian D. Davison
Nested Loops and breaks
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++; }
Lehigh University | CSE 002: Fundamentals of Programming | Lecture 1
Brian Y. Chen and Brian D. Davison
Lab 7, 8 are crucial for understanding loops
Lehigh University | CSE 002: Fundamentals of Programming | Lecture 1
Brian Y. Chen and Brian D. Davison
The didactic purpose of loop conversion
Lehigh University | CSE 002: Fundamentals of Programming | Lecture 1
Brian Y. Chen and Brian D. Davison
Be able to convert loops
int i, j;
for(i=0; i<2; i++){
for(j=0; j<3; j++){
..println(“i: ”+i+“j: ”+j);
}
}
..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;
Lehigh University | CSE 002: Fundamentals of Programming | Lecture 1
Brian Y. Chen and Brian D. Davison
Be able to convert loops
int i , j;
for(i=0; i<2; i++){
for(j=0; j<3; j++){
..println(“i: ”+i+“j: ”+j);
}
}
int i = 0, j;
while(i<2){
j=0;
while(j<3){
..println(“i: ”+i+“j: ”+j);
j++;
}
i++;
}
Lehigh University | CSE 002: Fundamentals of Programming | Lecture 1
Brian Y. Chen and Brian D. Davison
Natural variations on this kind of question
Lehigh University | CSE 002: Fundamentals of Programming | Lecture 1
Brian Y. Chen and Brian D. Davison
A general approach
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
Be able to convert loops
int i, j;
for(i=0; i<2; i++){
for(j=0; j<3; j++){
..println(“i: ”+i+“j: ”+j);
}
}
int i = 0, j;
do{
j=0;
do{
..println(“i: ”+i+“j: ”+j);
j++;
}while(j<3);
i++;
}while(i<2);
Lehigh University | CSE 002: Fundamentals of Programming | Lecture 1
Brian Y. Chen and Brian D. Davison
A more complex conversion
int i;
for(i=0; i<5; i++){
if(i%2==0){
..println(“i: ” + i);
}
}
int i = 0;
while(i<5){
..println(“i: ” + i);
i += 2;
}
Lehigh University | CSE 002: Fundamentals of Programming | Lecture 1
Brian Y. Chen and Brian D. Davison
A more complex conversion
int i;
for(i=0; i<5; i++){
if(i%2==0){
..println(“i: ” + i);
}
}
int i;
for(i=0; i<3; i++){
..println(“i: ” + (2*i) );
}
Lehigh University | CSE 002: Fundamentals of Programming | Lecture 1
Brian Y. Chen and Brian D. Davison
Out of order conversion
int i;
for(i=0; i<5; i++){
if(i%2==0){
…print(“A”);
}
else{
…print(“B”);
}
}
int i;
for(i=1; i<6; i++){
if(i%2==0){
…print(“A”);
}
else{
…print(“B”);
}
}
Lehigh University | CSE 002: Fundamentals of Programming | Lecture 1
Brian Y. Chen and Brian D. Davison
Convert by adding continue statements
int i = 0;
for(i=0; i<5; i++){
if(i%2==0){
…print(“A”);
}
else{
…print(“B”);
}
}
int i;
for(i=0; i<5; i++){
if(i%2==0){
…print(“A”);
}
else{
continue;
…print(“B”);
} }
Lehigh University | CSE 002: Fundamentals of Programming | Lecture 1
Brian Y. Chen and Brian D. Davison
Questions
Lehigh University | CSE 002: Fundamentals of Programming | Lecture 1
Brian Y. Chen and Brian D. Davison