Lecture 5: For Loops Building Java Programs: A Back to Basics Approach

Slides:



Advertisements
Similar presentations
Copyright 2010 by Pearson Education Building Java Programs Chapter 4 Lecture 4-2: Advanced if/else ; Cumulative sum reading: 4.1, 4.3, 4.5; "Procedural.
Advertisements

Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos:
Copyright 2006 by Pearson Education 1 reading: 4.1 Cumulative sum.
Copyright 2008 by Pearson Education 1 Class constants and scope reading: 2.4 self-check: 28 exercises: 11 videos: Ch. 2 #5.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
1 for Loops Computer Science is a science of abstraction - creating the right model for a problem and devising the appropriate mechanizable techniques.
Building Java Programs
Copyright 2009 by Pearson Education Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos: Ch.
Building Java Programs Chapter 2 Primitive Data and Definite Loops.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-3: Loop Figures and Constants reading: self-checks: 27 exercises:
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
1 The for loop. 2 Repetition with for loops So far, repeating a statement is redundant: System.out.println("Homer says:"); System.out.println("I am so.
ADMIT TICKET WHAT DOES THIS OUTPUT? double y = 2.5; int x = 6 / (int) y; System.out.println(“x = “ + x);
CS 112 Introduction to Programming Variables; Type Casting; Using Variables in for Loops Yang (Richard) Yang Computer Science Department Yale University.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 4: Loop Figures and Constants reading:
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos:
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos:
Building Java Programs Chapter 2 Primitive Data and Definite Loops.
Building Java Programs Chapter 2 Primitive Data and Definite Loops Copyright (c) Pearson All rights reserved.
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
The for loop.
Topic 6 loops, figures, constants Based on slides bu Marty Stepp and Stuart Reges from "Complexity has and will maintain.
Copyright 2008 by Pearson Education 1 Nested loops reading: 2.3 self-check: exercises: videos: Ch. 2 #4.
CS 112 Introduction to Programming Loop Examples; Variable Scoping; Nested Loops; Yang (Richard) Yang Computer Science Department Yale University 208A.
Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs Chapter 2
Lecture 4: Program Control Flow
Primitive Data, Variables, Loops (Maybe)
Topic 5 for Loops -Arthur Schopenhauer
CSc 110, Autumn 2017 Lecture 5: The for Loop and user input
Building Java Programs Chapter 2
Building Java Programs Chapter 2
CSc 110, Spring 2017 Lecture 9: Advanced if/else; Cumulative sum
Building Java Programs
CSc 110, Autumn 2016 Lecture 10: Advanced if/else; Cumulative sum
CSc 110, Spring 2018 Lecture 5: The for Loop and user input
Building Java Programs
Building Java Programs
Variables variable: A piece of the computer's memory that is given a name and type, and can store a value. Like preset stations on a car stereo, or cell.
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Lecture 8:The For Loop AP Computer Science Principles.
Building Java Programs
Building Java Programs
Topic 6 loops, figures, constants
Building Java Programs
CSE 190D, Winter 2013 Building Java Programs Chapter 2
Building Java Programs
Building Java Programs
Building Java Programs
The for loop suggested reading:
Using Objects (continued)
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Suggested self-checks:
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Chapter 2 Lecture 2-2: The for Loop reading: 2.3
Building Java Programs
Presentation transcript:

Lecture 5: For Loops Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson 2013. All rights reserved.

Increment and decrement shortcuts to increase or decrease a variable's value by 1 Shorthand Equivalent longer version variable++; variable = variable + 1; variable--; variable = variable - 1; int x = 2; x++; // x = x + 1; // x now stores 3 double gpa = 2.5; gpa--; // gpa = gpa - 1; // gpa now stores 1.5

shortcuts to modify a variable's value Modify-and-assign shortcuts to modify a variable's value Shorthand Equivalent 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;

The for loop 4

Repetition with for loops So far, repeating a statement is redundant: System.out.println("Homer says:"); System.out.println("I am so smart"); System.out.println("S-M-R-T... I mean S-M-A-R-T"); Java's for loop statement performs a task many times. for (int i = 1; i <= 4; i++) { // repeat 4 times } also related to book exercise 1.10 about printing 1000 copies of "All work and no play makes Jack a dull boy"

for loop syntax for (initialization; test; update) { statement; ... } Perform initialization once. Repeat the following: Check if the test is true. If not, stop. Execute the statements. Perform the update. body header

Initialization Tells Java what variable to use in the loop for (int i = 1; i <= 6; i++) { System.out.println("I am so smart"); } Tells Java what variable to use in the loop Performed once as the loop begins The variable is called a loop counter can use any name, not just i can start at any value, not just 1

Test Tests the loop counter variable against a limit for (int i = 1; i <= 6; i++) { System.out.println("I am so smart"); } Tests the loop counter variable against a limit Uses comparison operators: < less than <= less than or equal to > greater than >= greater than or equal to

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 exactly that! for (int i = 1; i <= 6; i++) { System.out.println(i + " squared = " + (i * i)); } "For each integer i from 1 through 6, print ..."

Loop walkthrough Output: 1 squared = 1 2 squared = 4 3 squared = 9 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! 4 5 1 2 4 3 5

For Loop in Movies Groundhog Day(1993); Bill Murray. Looper(2010); Bruce Willis and Joseph Gordon-Levitt, Emily Blunt. Edge of Tomorrow(2014); Tom Cruise, Emily Blunt.

Expressions for counter int highTemp = 5; for (int i = -3; i <= highTemp / 2; i++) { System.out.println(i * 1.8 + 32); } Output: 26.6 28.4 30.2 32.0 33.8 35.6

isPrime Calls to methods returning boolean can be used as tests: public static boolean isPrime(int n) { int factors = 0; for (int i = 1; i <= n; i++) { if (n % i == 0) { factors++; } if (factors == 2) { return true; } else { return false; Calls to methods returning boolean can be used as tests: if (isPrime(57)) { ...

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 " " to separate the numbers

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: T-minus 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, blastoff! The end.

Example Write a for loop to print: 5 9 13 17 21 25 for (int i = 5; i <= 25; i+=4) System.out.print(i + ” “); }

Class constants and scope 17

Limitations of variables Idea: Make a variable to represent the size. Use the variable's value in the methods. Problem: A variable in one method can't be seen in others. public static void main(String[] args) { int size = 4; topHalf(); printBottom(); } public static void topHalf() { for (int i = 1; i <= size; i++) { // ERROR: size not found ... public static void bottomHalf() { for (int i = size; i >= 1; i--) { // ERROR: size not found 18

Scope scope: The part of a program where a variable exists. i's scope From its declaration to the end of the { } braces A variable declared in a for loop exists only in that loop. A variable declared in a method exists only in that method. public static void example() { int x = 3; for (int i = 1; i <= 10; i++) { System.out.println(x); } // i no longer exists here } // x ceases to exist here x's scope i's scope

Scope implications Variables without overlapping scope can have same name. for (int i = 1; i <= 100; i++) { System.out.print(“A"); } for (int i = 1; i <= 100; i++) { // OK System.out.print(”BB"); int i = 5; // OK: outside of loop's scope A variable can't be declared twice or used out of its scope. for (int i = 1; i <= 100 * line; i++) { int i = 2; // ERROR: overlapping scope System.out.print("/"); i = 4; // ERROR: outside scope

If Block if(x<=3) { int y=2; … } y=5; // error since y does not exist outside // the if block 21

Cumulative algorithms

Adding many numbers How would you find the sum of all integers from 1-1000? // This may require a lot of typing int sum = 1 + 2 + 3 + 4 + ... ; System.out.println("The sum is " + sum); What if we want the sum from 1 - 1,000,000? Or the sum up to any maximum? How can we generalize the above code?

Cumulative sum loop int sum = 0; for (int i = 1; i <= 1000; i++) { sum = sum + i; // or sum+=i; } System.out.println("The sum is " + sum); cumulative sum: A variable that keeps a sum in progress and is updated repeatedly until summing is finished. The sum in the above code is an attempt at a cumulative sum. Cumulative sum variables must be declared outside the loops that update them, so that they will still exist after the loop.

Cumulative product This cumulative idea can be used with other operators: int product = 1; for (int i = 1; i <= 20; i++) { product = product * 2; } System.out.println("2 ^ 20 = " + product); How would we make the base and exponent adjustable?

CodingBat CodingBat will be used to do some graded assignments. Go to the CodingBat Java at http://codingbat.com/java Create an account. Go to “prefs”(upper right hand, next to your login email) Enter my email lnguyen8@bostonpublicschools.org under Teacher Share and click on “Share”. This will give me access to see your completed work. Go to “pref” and under MEMO: add S1 or S2 according to your section in front of your name. For example: S1: Nguyen, Long

Lab Save your file in the ForLoop folder in CS50. public static void main(String[] args) { multipleOf3(7,18); // 9 12 15 18 multiple4Not5(21); // 4 8 12 16 printList(2,7); // 2 4 6 printList(9,3); // 3 5 7 9 } //print multiples of 3 from m to n inclusive. // MUST USE % public static void multipleOf3(int m, int n){…} // print multiples of 4 and not of 5 from 4 // to m inclusive. Assume m>=4 Must use % public static void multiple4Not5(int m){…} // see next page, Must use % public static void printList(int a, int b){}

Lab Write a method printList that accepts two integer as arguments. You can assume that these integers are different. If the first argument is smaller than the second, print the even integers in natural order between the arguments including, if necessary, the endpoints. If the first argument is larger than the second, print the odd numbers between them. Write out the complete method header. The following calls from the main method should have the output below. printList(2,7); // 2 4 6 printList(9,3); // 3 5 7 9