CSE 142, Spring 2013: Lecture 2-2: The For Loop
CSE 142, Spring 2013: Lecture 2-2: The For Loop
// repeat 5 times
Check if the test is true. If not, stop. Execute the statements. Perform the update.
Control structures
Control structure: a programming construct that affects
Controlled code may include one or more statements The for loop is an example of a looping control structure
Initialization
for (int i = 1; i <= 6; i++) { System.out.println("I am so smart"); }
can use any name, not just i can start at any value, not just 1 only valid in the loop
Test
for (int i = 1; i <= 6; i++) { System.out.println("I am so smart"); }
less than less than or equal to greater than greater than or equal to
Modify-and-assign operators
shortcuts to modify a variable's value
Shorthand variable += variable -= variable *= variable /= variable %= x += 3; gpa -= 0.5; number *= 2; value; value; value; value; value; Equivalent longer version variable = variable + value; variable = variable - value; variable = variable * value; variable = variable / value; variable = variable % value; // x = x + 3; // gpa = gpa - 0.5; // number = number * 2;
9
Intuition: "I want to print a line for each number from 1 to 6"
10
1 2 3 for (int i = 1; i <= 4; i++) { 4 System.out.println(i + " squared = " + (i * i)); } 5 System.out.println("Whoo!"); 1 Output: 1 squared 2 squared 3 squared 4 squared Whoo! = = = = 1 4 9 16 2 4 3 5
Loop walkthrough
11
System.out.print
Prints without moving to a new line allows you to print partial messages on the same line
int highestTemp = 5; for (int i = -3; i <= highestTemp / 2; i++) { System.out.print((i * 1.8 + 32) + " "); }
Output:
26.6
28.4
30.2
32.0
33.8
35.6
Concatenate "
14
Rocket Exercise
Write a method that produces the following output:
T-minus 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, blastoff! The end.
15
Counting down
The update can use -- to make the loop count down. The test must say > instead of <
System.out.print("T-minus "); for (int i = 10; i >= 1; i--) { System.out.print(i + ", "); } System.out.println("blastoff!"); System.out.println("The end.");
Output:
16
Nested loops
reading: 2.3
17
Nested loops
nested loop: A loop placed inside another loop.
for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 10; j++) { System.out.print("*"); } System.out.println(); // to end the line }
Output:
********** ********** ********** ********** **********
18
Output:
* ** *** **** *****
19
Output:
1 22 333 4444 55555
20
Common errors
Both of the following sets of code produce infinite loops:
for (int i = 1; i <= 5; i++) { for (int j = 1; i <= 10; j++) { System.out.print("*"); } System.out.println(); } for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 10; i++) { System.out.print("*"); } System.out.println(); }
21
Complex lines
What nested for loops produce the following output?
inner loop (repeated characters on each line)
....1 ...2 ..3 .4 oop (loops 5 times because there are 5 lines) 5
We must build multiple complex lines of output using: an outer "vertical" loop for each of the lines inner "horizontal" loop(s) for the patterns within each line
22
Loop tables
for (int count = 1; count < 5; count++) { System.out.print(); }
What statement in the body would cause the loop to print: 2 7 12 17 22 To see patterns, make a table of count and the numbers.
Each time count goes up by 1, the number should go up by 5. But count * 5 is too great by 3, so we subtract 3.
5 * count - 3 2 7 12 17 22
25
25 20 15 10 5 0 -2 -5 -10 0 2 4 6
count (x) 1 2 3 4 5
27
always 1 (we increment along x by 1), we just need to look at the rise. The rise is the difference between the y values. Thus, the slope (m) is the difference between y values; in this case, it is +5. solve for b. In this case, y = 2.
y = m * x + b 2 = 5 * 1 + b Then b = -3
So the equation is
y = m * x + b y = 5 * x 3 y = 5 * count - 3
Copyright 2010 by Pearson Education
29
30
32
33