Presentation is loading. Please wait.

Presentation is loading. Please wait.

Department of Computer Science

Similar presentations


Presentation on theme: "Department of Computer Science"— Presentation transcript:

1 Department of Computer Science
COM S 207 For-Loop Statement Instructor: Ying Cai Department of Computer Science Iowa State University

2 The for loop condition checked before each iteration
loop variable, initialized before the loop starts update extended after each iteration no semi-column here for (int i=5; i<=10; i++) { Statement1; Statement2; ::: } // end for i body of loop A for loop is a count-controlled loop whereas a while loop is an event-controlled loop

3 for loop vs. while loop A for loop is a count-controlled loop whereas a while loop is an event-controlled loop for (int i=5; i<=10; i++) { Statement1; Statement2; ::: } // end for i while (condition) { Statement1; Statement2; ::: } // end while A count-controller loop is executed a definite number of times. In an event-controlled loop, we do not know how many iterations of the loop body may be executed – it is also called an infinite loop

4 A for loop can be countdown instead of up
for (int counter = 10; counter >= 0; counter--) { System.out.println(counter); } // end for counter body executed 11 times counter = 10; counter = 9; :: counter = 0;

5 The increment or decrement to the loop variable does not have to be 1 -- it can be any other number, but the same value is used for each iteration for (int cntr = 10; cntr >= 0; cntr = cntr - 2) { System.out.println(counter); } // end for cntr

6 Comprehension Checks for (i = 0; i<=5; i++) {
System.out.println(i); } // end for i for (i=5; i<=0; i--) { System.out.println(i); } // end for i for (i = 0; i<=9; i = i + 2) { System.out.println(i); } // end for i for (i = 0; i!=9; i = i + 2) { System.out.println(i); } // end for i

7 Comprehension Checks for (i = 1; i <= 20; i = i * 2) {
System.out.println(i); } // end for i for (i = 1; i <= str.length(); i++) { System.out.println(i); } // end for i for (int n = 10; n >= 0; n--) { System.out.print(n + “ “); } // end for n

8 Write Loops that computes
the sum of all integers between -120 and 2312 (inclusive) the sum of all EVEN integers between 78 and 3090 (exclusive) the sum of all SQUARES of integers between -300 and (inclusive) the average of all ODD integers between -123 and 321 (inclusive)

9 Ex. 1: Given a number, check if it is a prime number
Pseudocode ask for number test against denominator, starting from 2, 3, ..., number/2 if number % denominator = 0, then number is not a prime number otherwise, number is a prime number

10 More examples Given a number, check if it is a prime number
Given a range, find out all prime numbers in between Given a number, print its binary digits (e.g., 15 -> 1111, 20 ->11000) Given a number, print its hexadecimal digits (e.g., 9->9, 10->A, 15 -> F, 20 -> 14)

11 Sentinel loop (sec. 4.4) loops that have a pre-determined sentinel value for termination. Example: compute the average of a set of salary values and use -1 as a sentinel while (salary != 1) { salary = in.nextDouble(); if (salary != -1) sum = sum + salary; count++; } // end if } // end for while if (counter > 0) double average = sum / count; System.out.println(“Average salary is “ + average); } else System.out.println(“no data”);

12 Nested Loop A loop inside another loop X1 X2 X3 X4 1 1 1 1 2 4 8 16
: : : :


Download ppt "Department of Computer Science"

Similar presentations


Ads by Google