Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
COM S 207 For-Loop Statement Instructor: Ying Cai Department of Computer Science Iowa State University [email protected] The for loop loop variable, initialized before the loop starts condition checked before each iteration for (int i=5; i<=10; i++) { Statement1; Statement2; body of loop ::: } // end for i update extended after each iteration no semi-column here A for loop is a count-controlled loop whereas a while loop is an event-controlled loop 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 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; 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 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 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 Write Loops that computes 1) the sum of all integers between -120 and 2312 (inclusive) 2) the sum of all EVEN integers between 78 and 3090 (exclusive) 3) the sum of all SQUARES of integers between -300 and +3300 (inclusive) 4) the average of all ODD integers between -123 and 321 (inclusive) Ex. 1: Given a number, check if it is a prime number Pseudocode 1. ask for number 2. test against denominator, starting from 2, 3, ..., number/2 3. if number % denominator = 0, then number is not a prime number 4. otherwise, number is a prime number More examples 1) Given a number, check if it is a prime number 2) Given a range, find out all prime numbers in between 3) Given a number, print its binary digits (e.g., 15 -> 1111, 20 ->11000) 4) Given a number, print its hexadecimal digits (e.g., 9>9, 10->A, 15 -> F, 20 -> 14) Sentinel loop (sec. 4.4) while (salary != 1) { salary = in.nextDouble(); if (salary != -1) { sum = sum + salary; count++; } // end if } // end for while 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 if (counter > 0) { double average = sum / count; System.out.println(“Average salary is “ + average); } else { System.out.println(“no data”); } Nested Loop A loop inside another loop X1 X2 X3 X4 1 2 1 4 1 8 1 16 27 81 3 9 : : : : 10 100 1000 1000