1 for Loops Computer Science is a science of abstraction - creating the right model for a problem...

19
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

Transcript of 1 for Loops Computer Science is a science of abstraction - creating the right model for a problem...

Page 1: 1 for Loops Computer Science is a science of abstraction - creating the right model for a problem and devising the appropriate mechanizable techniques.

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

Page 2: 1 for Loops Computer Science is a science of abstraction - creating the right model for a problem and devising the appropriate mechanizable techniques.

2

Plan for TodayReview - Increment, Decrement, Assignment operatorsSystem.out.print() for loops - our first repetition structure

Page 3: 1 for Loops Computer Science is a science of abstraction - creating the right model for a problem and devising the appropriate mechanizable techniques.

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!

Page 4: 1 for Loops Computer Science is a science of abstraction - creating the right model for a problem and devising the appropriate mechanizable techniques.

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;

Page 5: 1 for Loops Computer Science is a science of abstraction - creating the right model for a problem and devising the appropriate mechanizable techniques.

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!

Page 6: 1 for Loops Computer Science is a science of abstraction - creating the right model for a problem and devising the appropriate mechanizable techniques.

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…”);}

Page 7: 1 for Loops Computer Science is a science of abstraction - creating the right model for a problem and devising the appropriate mechanizable techniques.

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 ..."

Page 8: 1 for Loops Computer Science is a science of abstraction - creating the right model for a problem and devising the appropriate mechanizable techniques.

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

Page 9: 1 for Loops Computer Science is a science of abstraction - creating the right model for a problem and devising the appropriate mechanizable techniques.

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

Page 10: 1 for Loops Computer Science is a science of abstraction - creating the right model for a problem and devising the appropriate mechanizable techniques.

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

Page 11: 1 for Loops Computer Science is a science of abstraction - creating the right model for a problem and devising the appropriate mechanizable techniques.

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);}

Page 12: 1 for Loops Computer Science is a science of abstraction - creating the right model for a problem and devising the appropriate mechanizable techniques.

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

Page 13: 1 for Loops Computer Science is a science of abstraction - creating the right model for a problem and devising the appropriate mechanizable techniques.

13

Another Loop ExampleSystem.out.println("+----+");for (int i = 1; i <= 3; i++) { System.out.println("\\ /"); System.out.println("/ \\");}System.out.println("+----+");

Output:+----+\ // \\ // \\ // \+----+

Page 14: 1 for Loops Computer Science is a science of abstraction - creating the right model for a problem and devising the appropriate mechanizable techniques.

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!

Page 15: 1 for Loops Computer Science is a science of abstraction - creating the right model for a problem and devising the appropriate mechanizable techniques.

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

Page 16: 1 for Loops Computer Science is a science of abstraction - creating the right model for a problem and devising the appropriate mechanizable techniques.

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 + “, “);

}

Page 17: 1 for Loops Computer Science is a science of abstraction - creating the right model for a problem and devising the appropriate mechanizable techniques.

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

Page 18: 1 for Loops Computer Science is a science of abstraction - creating the right model for a problem and devising the appropriate mechanizable techniques.

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,”);

Page 19: 1 for Loops Computer Science is a science of abstraction - creating the right model for a problem and devising the appropriate mechanizable techniques.

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:

*********************