Iterations for loop Outcome Introduction to for loop

  • Slides: 20
Download presentation
Iterations for loop

Iterations for loop

Outcome • Introduction to for loop • The use of for loop instead of

Outcome • Introduction to for loop • The use of for loop instead of while loop • Nested for loops

Reading • Read Chapter 8 on the study guide (pages 53 -62) • Read

Reading • Read Chapter 8 on the study guide (pages 53 -62) • Read Chapter 11 on the sudy guide. (pages 81 -87) • Do as much exercises as you can from These tow chapters.

The for Statement • A for statement has the following syntax: The initialization is

The for Statement • A for statement has the following syntax: The initialization is executed once before the loop begins The statement is executed until the condition becomes false for ( initialization ; condition ; increment ){ statement; } The increment portion is executed at the end of each iteration 5 -4

Logic of a for loop initialization condition evaluated true statement increment 5 -5 false

Logic of a for loop initialization condition evaluated true statement increment 5 -5 false

The for Statement • A for loop is functionally equivalent to the following while

The for Statement • A for loop is functionally equivalent to the following while loop structure: initialization; while ( condition ){ statement; increment; } 5 -6

The for Statement • An example of a for loop: for (int count=1; count

The for Statement • An example of a for loop: for (int count=1; count <= 5; count++){ System. out. println (count); } • The initialization section can be used to declare a variable • Like a while loop, the condition of a for loop is tested prior to executing the loop body • Therefore, the body of a for loop will execute zero or more times 5 -7

The for Statement • The increment section can perform any calculation for (int num=100;

The for Statement • The increment section can perform any calculation for (int num=100; num > 0; num -= 5){ System. out. println (num); } • A for loop is well suited for executing statements a specific number of times that can be calculated or determined in advance 5 -8

Example 1 • What is the output of the program? // hello. java public

Example 1 • What is the output of the program? // hello. java public class Hello 1 { public static void main(String[] arguments) { System. out. println(“hello”); System. out. println(“hello”); Prints hello 10 times } } //end of program 9

Example 2 • What is the output of the program? // hello. java public

Example 2 • What is the output of the program? // hello. java public class Hello 2 { public static void main(String[] arguments) { Prints hello 10 times for (int i = 0 ; i< 10; i++) { System. out. println(“hello”); i = i+1; } } //end of program 10

Example 3 • What is the output of the program? // hello 3. java

Example 3 • What is the output of the program? // hello 3. java public class Hello 2 { public static void main(String[] args) { int x = Integer. parse. Int(args[0]); for (int i = 0 ; i< x; i++) { System. out. println(“hello”); } } //end of program 11

Example 4 • What is the output of the program? // hello 4. java

Example 4 • What is the output of the program? // hello 4. java Import java. util. Random public class Hello 3 { public static void main(String[] arguments) { Random generator = new Random(); for (int i = 0 ; i< 10; i++) { int x = generator. next. Int(100); System. out. println(x); } } //end of program 12

Example 5 • What is the output of the program? // sum 1. java

Example 5 • What is the output of the program? // sum 1. java public class sum 1 { public static void main(String[] arguments) { int sum =0; for (int i = 1 ; i<= 100; i++) { sum = sum+i ; } System. out. println(sum); } // end of main } //end of program 13

Example 6 • What is the output of the program? // factorial. java public

Example 6 • What is the output of the program? // factorial. java public class sum 1 { public static void main(String[] arguments) { int i =1; int factorial =1; for (int i = 1 ; i<= 100; i++) { fact= fact*i ; } System. out. println(factorial); } }//end of program 14

Infinite Loops • To exit the loop – The body of a while loop

Infinite Loops • To exit the loop – The body of a while loop has to make the control condition false – Otherwise, the loop will on go on for ever. This is called an infinite loop. – Press Control-C to stop the execution of an infinite loop. – This is a common logical error 5 -15

Infinite Loops • An example of an infinite loop: int i = 0; while

Infinite Loops • An example of an infinite loop: int i = 0; while (i<=0) { System. out. println (i); i = i - 1; } • • This loop will continue executing for ever. Control-C to stop the execution 5 -16

Nested Loops • Similar to nested if statements, loops can be nested as well

Nested Loops • Similar to nested if statements, loops can be nested as well • The body of a loop can contain another loop. 5 -17

Nested Loops • What is the output of the following program? for (int count

Nested Loops • What is the output of the following program? for (int count 1 = 1 ; count 1<= 10; count 1++) { System. out. println(“hello") for (int count 2 = 1 ; count 2<= 20; count 2++) { System. out. println(“lahcen"); } } 5 -18

The for Statement • Each expression in the header of a for loop is

The for Statement • Each expression in the header of a for loop is optional • If the initialization is left out, no initialization is performed • If the condition is left out, it is always considered to be true, and therefore creates an infinite loop – We usually call this a “forever loop” • If the increment is left out, no increment operation is performed 5 -19

Home Work • • • Do all exercises on the my website for week

Home Work • • • Do all exercises on the my website for week 9. Read Chapter 8 on the study guide (pages 53 -62) Redo all examples and exercises in Chapter 3 Read Chapter 11 on the sudy guide. (pages 81 -87) Do as much exercises are you can.