CS100J Lecture 3 Previous Lecture This Lecture Programming Concepts variables and declarations, assignment, expressions Java Constructs variable = expression ; expressions in.readInt() if ( expression ) statement1 else statement2 System.out.println( expression ); // any-text-to-end-of-line /* any-text */ { list-of-statements } boilerplate Reading: L&L, Ch. 2, 3.1-3.5; or S. 2, 3.1 This Lecture iteration while-loops Reading: L&L, Sec. 3.6-3.7; or S., Sec. 3.2-3.4 CS 100 Lecture 3
Iteration while ( expression ) Template: The expression is called the condition and the statement is called the body Meaning: Execute the body 0 or more times until the condition is false When several statements must be executed on each iteration, use a block statement while ( expression ) { list-of-statements } while ( expression ) statement CS 100 Lecture 3
Iteration, Example 1 Problem statement A list of integers is given as input data. For each integer, output “even” if it is even, otherwise output “odd”. Program Segment in Java while ( true ) { num = in.readInt(); rem = num % 2; if ( rem == 0 ) System.out.println("even"); else System.out.println("odd"); } CS 100 Lecture 3
Example 1: Execution Trace Sample input data: 7 8 11 ... Variables as a function of time: time Sample output: odd even Remark: If the condition never becomes false, the body is repeated forever CS 100 Lecture 3
Pattern for Iterating n Times /* Do whatever n times. */ j = 1; while ( j <= n ) { whatever j = j + 1; } Remarks: Think of such a pattern as a whole, e.g., as “do whatever n times” whatever can refer to variable j, Any variables can be used instead of j and n CS 100 Lecture 3
Iteration, Example 2 Problem statement An integer n is given as input data. Output a list of the perfect squares between 1 and n2 inclusive. Program Segment in Java n = in.readInt(); /* Output the perfect squares between 1 and n*n inclusive. */ j = 1; while ( j <= n ) { System.out.println( j*j ); j = j + 1; } CS 100 Lecture 3
Example 2: Execution Trace Sample input data: 3 Variables as a function of time: time Sample output: 1 4 9 Remark: If the condition is initially false, the body is executed 0 times, e.g., if the input data is negative or 0 CS 100 Lecture 3
Iteration: Example 3 Problem statement Assume variable m contains some integer. Output a line of m asterisks (*’s). Program Segment in Java /* Output a line of m *’s. */ k = 1; while ( k <= m ) { System.out.print( ”*” ); k = k + 1; } CS 100 Lecture 3
Iteration: Example 4 Problem statement An integer n is given as input data. Output a triangle of asterisks with base and height n , e.g., if n is 3: * ** *** Program Segment in Java n = in.readInt(); /* Output n lines of triangle. */ j = 1; while ( j <= n ) { /* Output line of j *’s. */ ... j = j + 1; } CS 100 Lecture 3
Iteration: Example 4, cont. Program Segment in Java n = in.readInt(); /* Output n lines of triangle */ j = 1; while ( j <= n ) { /* Output line of j *’s. */ k = 1; while ( k <= j ) System.out.print( ”*” ); k = k + 1; } System.out.println(); j = j + 1; CS 100 Lecture 3
Example 4: Execution Trace Sample input data: 2 Variables as a function of time: time Sample output: * ** CS 100 Lecture 3
Iteration: Example 5 Problem statement Program Segment in Java An integer p is given as input data. If the smallest integer >= 2 that divides p is p itself, output “prime”, otherwise output “composite” Program Segment in Java p = in.readInt(); // Let d be smallest integer >= 2 that // divides p exactly. d = 2; while ( (p % d) != 0 ) d = d + 1; if ( d == p ) System.out.print(”prime”); else System.out.print(”composite”); CS 100 Lecture 3