Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Iterations for loop. Outcome Introduction to for loop The use of for loop instead of while loop Nested for loops."— Presentation transcript:

1 Iterations for loop

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

3 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.

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

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

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

7 5-7 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

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

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

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

11 Example3 What is the output of the program? // hello3.java public class Hello2 { public static void main(String[] args) { int x = Integer.parseInt(args[0]); for (int i = 0 ; i< x; i++) { System.out.println(hello); } //end of program 11

12 Example4 What is the output of the program? // hello4.java Import java.util.Random public class Hello3 { public static void main(String[] arguments) { Random generator = new Random(); for (int i = 0 ; i< 10; i++) { int x = generator.nextInt(100); System.out.println(x); } //end of program 12

13 Example5 What is the output of the program? // sum1.java public class sum1 { 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

14 Example6 What is the output of the program? // factorial.java public class sum1 { 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

15 5-15 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

16 5-16 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

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

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

19 5-19 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

20 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.


Download ppt "Iterations for loop. Outcome Introduction to for loop The use of for loop instead of while loop Nested for loops."

Similar presentations


Ads by Google