1 for Loops Computer Science is a science of abstraction - creating the right model for a problem...
-
Upload
darcy-scott -
Category
Documents
-
view
216 -
download
0
Transcript of 1 for Loops Computer Science is a science of abstraction - creating the right model for a problem...
1
for LoopsComputer 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
Plan for TodayReview - Increment, Decrement, Assignment operatorsSystem.out.print() for loops - our first repetition structure
3
To Increment or Decrement: That is the Question
shortcuts to increase or decrease variable's value by 1
Shorthand Equivalent longer versionvariable++; variable = variable + 1;variable--; variable = variable - 1;
Example: int examGrade = 91; // pretty dang goodexamGrade++; // even better
int countdownToWeekend = 1; // 1 more daycountdownToWeekend--; // weekend at last!
4
Assignment Operatorsshortcuts to modify a variable's value
Shorthand Equivalent longer versionvariable += 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
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
Repetition with for LoopsPerform the same task multiple times without
redundancyFirst way - same line of code, over and over:// echoSystem.out.println(“Helllooo…”);System.out.println(“Helllooo…”);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
Repetition over a rangeSystem.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
for loop syntaxfor (<initialization>; <test>; <update>) { <statement>; <statement>; ... <statement>;}
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
Initializationfor (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
Testfor (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
for Loop - Test and Updatefor (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
Loop walkthroughfor (int i = 1; i <= 4; i++) { System.out.println(i + " squared = " + (i * i));}System.out.println("Whoo!");
Output:
1 squared = 12 squared = 43 squared = 94 squared = 16Whoo!
1
1
2
2
3
3
4
4
5
5
13
Another Loop ExampleSystem.out.println("+----+");for (int i = 1; i <= 3; i++) { System.out.println("\\ /"); System.out.println("/ \\");}System.out.println("+----+");
Output:+----+\ // \\ // \\ // \+----+
14
Loop VariationsThe 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
Mapping loops to numbersfor (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
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
Loop tablesWhat statement in the body would cause the loop to print:
2 7 12 17 22To 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
1 2 5
2 7 10
3 12 15
4 17 20
5 22 25
5 * count - 3
2
7
12
17
22
18
One Line Loop BodyWhen 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
Loop Practice Write a loop that prints all the integers from -10 to 10. Write a loop that produces the following output:
• 8 4 0 -4 -8
• Write a loop that produces this output:
*********************