Download presentation
Presentation is loading. Please wait.
Published byDarcy Scott Modified over 9 years ago
1
1 for Loops Computer Science is a science of abstraction - creating the right model for a problem and devising the appropriate mechanizable techniques to solve it. - A. Aho and J. Ullman Based on slides at buildingjavaprograms.com
2
2 Plan for Today Review - Increment, Decrement, Assignment operators System.out.print() for loops - our first repetition structure
3
3 To Increment or Decrement: That is the Question shortcuts to increase or decrease variable's value by 1 ShorthandEquivalent longer version variable ++; variable = variable + 1; variable --; variable = variable - 1; Example: int examGrade = 91; // pretty dang good examGrade++; // even better int countdownToWeekend = 1; // 1 more day countdownToWeekend--; // weekend at last!
4
4 Assignment Operators shortcuts to modify a variable's value ShorthandEquivalent longer version variable += value ; variable = variable + value ; variable -= value ; variable = variable - value ; variable *= value ; variable = variable * value ; variable /= value ; variable = variable / value ; variable %= value ; variable = variable % value ; x += 3;// x = x + 3; gpa -= 0.5;// gpa = gpa - 0.5; number *= 2;// number = number * 2;
5
5 System.out.print() System.out.println() command: prints some output and then goes to the next line. System.out.print(): prints output without advancing to new line. Allows you to print partial messages on the same line: System.out.print(“Ready! ”); System.out.print(“Set! ”); System.out.print(“Go! “); Output: Ready! Set! Go!
6
6 Repetition with for Loops Perform the same task multiple times without redundancy First way - same line of code, over and over: // echo System.out.println(“Helllooo…”); A for loop statement tells the computer to carry out a task many times: for(int i = 1; i <= 3; i++) { System.out.println(“Helllooo…”); }
7
7 Repetition over a range System.out.println("1 squared = " + 1 * 1); System.out.println("2 squared = " + 2 * 2); System.out.println("3 squared = " + 3 * 3); System.out.println("4 squared = " + 4 * 4); System.out.println("5 squared = " + 5 * 5); System.out.println("6 squared = " + 6 * 6); Intuition: "I want to print a line for each number from 1 to 6" The for loop does just that! for (int i = 1; i <= 6; i++) { System.out.println(i + " squared = " + (i * i)); } "For each integer i from 1 through 6, print..."
8
8 for loop syntax for ( ; ; ) { ;... ; } How to execute a for loop: Perform initialization once. Repeat the following: Check if the test is true. If not, stop. Execute the statements. Perform the update. body header
9
9 Initialization for (int i = 1; i <= 3; i++) { System.out.println(i + " squared = " + (i * i)); } Initializes (and possibly declares) the variable to be used in the loop Variable called a loop counter
10
10 Test for (int i = 1; i <= 6; i++) { System.out.println(i + " squared = " + (i * i)); } Tests the loop counter variable against a bound Uses comparison operators: < less than <= less than or equal to > greater than >= greater than or equal to
11
11 for Loop - Test and Update for (int i = 1; i <= 4; i++) { System.out.println(i + " squared = " + (i * i)); } The test i <= 4 compares the loop counter to some bound If test is true, the statements in loop body, and then the update statement, are executed The update i++ changes loop counter's value after each repetition (execution of loop body) Without an update, you would have an infinite loop Can be any expression: for (int i = 1; i <= 9; i += 2) { System.out.println(i); }
12
12 Loop walkthrough for (int i = 1; i <= 4; i++) { System.out.println(i + " squared = " + (i * i)); } System.out.println("Whoo!"); Output: 1 squared = 1 2 squared = 4 3 squared = 9 4 squared = 16 Whoo! 1 1 2 2 3 3 4 4 5 5
13
13 Another Loop Example System.out.println("+----+"); for (int i = 1; i <= 3; i++) { System.out.println("\\ /"); System.out.println("/ \\"); } System.out.println("+----+"); Output: +----+ \ / / \ \ / / \ \ / / \ +----+
14
14 Loop Variations 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!"); Output: T-minus 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, blastoff!
15
15 Mapping loops to numbers for (int count = 1; count <= 5; count++) {... } What statement in the body would cause the loop to print: 4 7 10 13 16 Or this: 2, 4, 6, 8, 10
16
16 Answers for (int count = 1; count <= 5; count++){ System.out.print(3 * count + 1 + " "); } for(int count = 1; count <= 5; count++){ System.out.print(count*2 + “, “); }
17
17 Loop tables 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. count number to print 5 * count 125 2710 31215 41720 52225 5 * count - 3 2 7 12 17 22
18
18 One Line Loop Body When a loop’s body contains only a single statement, the braces around the body may be eliminated: for(int i = 2; i <= 4; i++) System.out.println(“i = “ + i); // no braces Example - what’s the output? for(int i = 1; i <=3; i++) System.out.print(“Happy Birthday, “); System.out.println(“to you,”);
19
19 Loop Practice Write a loop that prints all the integers from -10 to 10. Write a loop that produces the following output: 840 -4-8 Write a loop that produces this output: * ** *** **** ***** ******
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.