18
How to write a good FOR LOOP Academic Resource Center

How to write a good FOR LOOP › ... › pdfs › for_loop.pdfResult • this is the second for loop • this is the second for loop • this is the second for loop • this is a first

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: How to write a good FOR LOOP › ... › pdfs › for_loop.pdfResult • this is the second for loop • this is the second for loop • this is the second for loop • this is a first

How to write a good FOR LOOP

Academic Resource Center

Page 2: How to write a good FOR LOOP › ... › pdfs › for_loop.pdfResult • this is the second for loop • this is the second for loop • this is the second for loop • this is a first

What’s For loop?

• Welcome to this workshop

Welcome to this workshop

Welcome to this workshop

Welcome to this workshop

Welcome to this workshop

Welcome to this workshop

Welcome to this workshop

Welcome to this workshop

Welcome to this workshop

Welcome to this workshop

Page 3: How to write a good FOR LOOP › ... › pdfs › for_loop.pdfResult • this is the second for loop • this is the second for loop • this is the second for loop • this is a first

Or

• *

**

***

****

*****

******

*******

********

*********

Page 4: How to write a good FOR LOOP › ... › pdfs › for_loop.pdfResult • this is the second for loop • this is the second for loop • this is the second for loop • this is a first

Or something even harder

• 0 x 0 = 0; 0 x 1 = 0; 0 x 2 = 0; 0 x 3 = 0; 0 x 4 = 0; 0 x 5 = 0; 0 x 6 = 0; 0 x 7 = 0; 0 x 8 = 0;

1 x 0 = 0; 1 x 1 = 1; 1 x 2 = 2; 1 x 3 = 3; 1 x 4 = 4; 1 x 5 = 5; 1 x 6 = 6; 1 x 7 = 7; 1 x 8 = 8;

2 x 0 = 0; 2 x 1 = 2; 2 x 2 = 4; 2 x 3 = 6; 2 x 4 = 8; 2 x 5 = 10; 2 x 6 = 12; 2 x 7 = 14; 2 x 8 = 16;

3 x 0 = 0; 3 x 1 = 3; 3 x 2 = 6; 3 x 3 = 9; 3 x 4 = 12; 3 x 5 = 15; 3 x 6 = 18; 3 x 7 = 21; 3 x 8 = 24;

4 x 0 = 0; 4 x 1 = 4; 4 x 2 = 8; 4 x 3 = 12; 4 x 4 = 16; 4 x 5 = 20; 4 x 6 = 24; 4 x 7 = 28; 4 x 8 = 32;

Page 5: How to write a good FOR LOOP › ... › pdfs › for_loop.pdfResult • this is the second for loop • this is the second for loop • this is the second for loop • this is a first

• System.out.println("Welcome to the \"For Loop\" workshop");

System.out.println("Welcome to the \"For Loop\" workshop");

System.out.println("Welcome to the \"For Loop\" workshop");

System.out.println("Welcome to the \"For Loop\" workshop");

System.out.println("Welcome to the \"For Loop\" workshop");

System.out.println("Welcome to the \"For Loop\" workshop");

System.out.println("Welcome to the \"For Loop\" workshop");

System.out.println("Welcome to the \"For Loop\" workshop");

System.out.println("Welcome to the \"For Loop\" workshop");

System.out.println("Welcome to the \"For Loop\" workshop");

• for(int i = 0; i<10; i++) {

System.out.println("Welcome to the \"For Loop\" workshop");

}

Page 6: How to write a good FOR LOOP › ... › pdfs › for_loop.pdfResult • this is the second for loop • this is the second for loop • this is the second for loop • this is a first

• System.out.println("*");

System.out.println("**");

System.out.println("***");

System.out.println("****");

System.out.println("*****");

System.out.println("******");

System.out.println("*******");

System.out.println("********");

System.out.println("*********");

System.out.println("**********");

• for(int i = 0; i< 10; i++){

for(int j = 0; j<i; j++){

System.out.print("*");}

System.out.println(); }

Page 7: How to write a good FOR LOOP › ... › pdfs › for_loop.pdfResult • this is the second for loop • this is the second for loop • this is the second for loop • this is a first

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

for (int j = 0; j < 9; j++) {

System.out.print(i + " x " + j + " = " + j*i + "; ");

}

System.out.println();

Page 8: How to write a good FOR LOOP › ... › pdfs › for_loop.pdfResult • this is the second for loop • this is the second for loop • this is the second for loop • this is a first

How to use the for loop

• For most of the computer languages available:

for (statement 1 ; statement 2; increment step)

{

Your command;

}

Page 9: How to write a good FOR LOOP › ... › pdfs › for_loop.pdfResult • this is the second for loop • this is the second for loop • this is the second for loop • this is a first

Statement 1

• Declare and initial the counter

• I.e.

Int I;

i = 0;

Page 10: How to write a good FOR LOOP › ... › pdfs › for_loop.pdfResult • this is the second for loop • this is the second for loop • this is the second for loop • this is a first

Statement 2

• A Boolean expression to check the condition:

• I.e.

I < 10;

I >= 100;

Page 11: How to write a good FOR LOOP › ... › pdfs › for_loop.pdfResult • this is the second for loop • this is the second for loop • this is the second for loop • this is a first

Increment

• i.e.

i++;

i+= 5;

i+= 10;

Page 12: How to write a good FOR LOOP › ... › pdfs › for_loop.pdfResult • this is the second for loop • this is the second for loop • this is the second for loop • this is a first

How the complete for loop look like • Java: for(int i = 0; i< 10; i++){

your command;

}

• C/C++: int I;

For( I =0; i<10; i++){

Your command;

}

Page 13: How to write a good FOR LOOP › ... › pdfs › for_loop.pdfResult • this is the second for loop • this is the second for loop • this is the second for loop • this is a first

Sequence of For Loop

• For(int I = 0; I < 3; i++){

System.out.println(“this is a first for loop”);

}

For(int I = 0; i<3;i++){

System.out.println(“this is the second for loop”);

}

Page 14: How to write a good FOR LOOP › ... › pdfs › for_loop.pdfResult • this is the second for loop • this is the second for loop • this is the second for loop • this is a first

Result:

• this is a first for loop

• this is a first for loop

• this is a first for loop

• this is the second for loop

• this is the second for loop

• this is the second for loop

Page 15: How to write a good FOR LOOP › ... › pdfs › for_loop.pdfResult • this is the second for loop • this is the second for loop • this is the second for loop • this is a first

Nested for loop

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

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

System.out.println("this is the second for loop");

}

System.out.println("this is a first for loop");

}

Page 16: How to write a good FOR LOOP › ... › pdfs › for_loop.pdfResult • this is the second for loop • this is the second for loop • this is the second for loop • this is a first

Result • this is the second for loop • this is the second for loop • this is the second for loop • this is a first for loop • this is the second for loop • this is the second for loop • this is the second for loop • this is a first for loop • this is the second for loop • this is the second for loop • this is the second for loop • this is a first for loop

Page 17: How to write a good FOR LOOP › ... › pdfs › for_loop.pdfResult • this is the second for loop • this is the second for loop • this is the second for loop • this is a first

Different kind of for loop

• For each loop:

• for ( type a : Array){

your command;

}

Page 18: How to write a good FOR LOOP › ... › pdfs › for_loop.pdfResult • this is the second for loop • this is the second for loop • this is the second for loop • this is a first

• For ever loop:

For ( ; ; ){

} • For loop with one condition:

For (; input == ‘q’ ;){

}