CS 112 Introduction to Programming Loop Examples; Variable Scoping; Nested Loops; Yang (Richard) Yang Computer Science Department Yale University 208A.

Slides:



Advertisements
Similar presentations
Nested for loops Cont’d. 2 Drawing complex figures Use nested for loops to produce the following output. Why draw ASCII art? –Real graphics require a.
Advertisements

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 6, 2005.
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:
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Copyright 2006 by Pearson Education 1 reading: 4.1 Cumulative sum.
CS305j Introduction to ComputingNested For Loops 1 Topic 6 Nested for Loops "Complexity has and will maintain a strong fascination for many people. It.
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.
CS 112 Introduction to Programming Variable Scoping; Nested Loops; Parameterized Methods Yang (Richard) Yang Computer Science Department Yale University.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-3: Loop Figures and Constants reading: self-checks: 27 exercises:
CS 112 Introduction to Programming Arrays; Loop Patterns (break) Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone:
CS 112 Introduction to Programming Graphics; Animation Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone:
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
1 BUILDING JAVA PROGRAMS CHAPTER 2 Pseudocode and Scope.
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.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I 12/11/20151.
CS 112 Introduction to Programming Variable Scoping; Nested Loops; Parameterized Methods 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.
Nested for loops.
Printing with for Loops To print a character multiple times, use a for loop. for (int j = 1; j
The for loop.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I 2/4/20161.
Copyright 2008 by Pearson Education 1 Nested loops reading: 2.3 self-check: exercises: videos: Ch. 2 #4.
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
CS 112 Introduction to Programming Nested Loops; Parameterized Methods Yang (Richard) Yang Computer Science Department Yale University 208A Watson, Phone:
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Building Java Programs
Building Java Programs
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
Lecture 5: For Loops Building Java Programs: A Back to Basics Approach
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
CSE 190D, Winter 2013 Building Java Programs Chapter 2
Building Java Programs
The for loop suggested reading:
Building Java Programs
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
Chapter 2 Lecture 2-2: The for Loop reading: 2.3
Building Java Programs
CIS 110: Introduction to Computer Programming
Chapter 2 Lecture 2-3: Loop Figures and Constants reading:
Presentation transcript:

CS 112 Introduction to Programming Loop Examples; Variable Scoping; Nested Loops; Yang (Richard) Yang Computer Science Department Yale University 208A Watson, Phone:

Outline  Admin and recap  Loop examples  Variable scoping  Nested loops 2

Admin  PS2 questions  Coding style review? 3

Recap  Data conversions  Assignment operators  for loops 4

5 Recap: The for Statement: Syntax for ( initialization ; condition ; increment ) statement;Reservedword The initialization portion is executed once before the loop begins The statement is executed until the condition becomes false The increment portion is executed at the end of each iteration Both semi-colons are always required

Recap: Flexibility of for Loop with Counter for (int i = 1; i <= 6; i ++) { System.out.println("I am so smart"); } Loop counter: can use any name, not just i can start at any value, not just 1 only valid in the loop Compare loop counter with target: < less than <= less than or equal to > greater than >= greater than or equal to Can increment, decrement, times, … Can be even empty

Outline  Admin and recap  for loop examples 7

Example for Loop: Print Result from  Print out the result of adding $1000 on Jan. 1 of each year, from 2016 to 2030, to a fund that returns 6% per year? 8

Example for Loop: Counting Down  Write a program generating output T-minus 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, blastoff!  Requirement: loop counter starts with 10 and counts down

Counting Down v1  The update uses -- to count down. System.out.print("T-minus "); for (int i = 10; i >= 1; i--) { System.out.print(i + ", "); } System.out.println("blastoff!");

Counting Down v2  Requirement: loop counter starts with 1 and counts up: T-minus 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, blastoff! System.out.print("T-minus "); for (int i = 1; i <= 10; i++) { // ??? } System.out.println("blastoff!");

Mapping Loop# to Target Pattern 12 i number to print …… y = 11 - x

Counting Down System.out.print("T-minus "); for (int i = 1; i <= 10; i++) { System.out.println( 11 – i + “, “); } System.out.println("blastoff!"); y = 11 - x

An “IQ Test” Format ?

An “IQ Test” Format ?

An “IQ Test” Format

An “IQ Test” Format Loop # i: slope Value at 0 y = 11 – 1 * x

Practice: Mapping loop# to numbers for (int count = 1; count <= 5; count++) { System.out.print(... ); }  What statement in the body would cause the loop to print:

Mapping loop# to numbers Loop# i: Target: 21–4*i for (int count = 1; count <= 5; count++) { System.out.print(-4 * count " "); }

(Offline) Practice: Mapping loop# to numbers for (int count = 1; count <= 5; count++) { System.out.print(... ); }  What statement in the body would cause the loop to print: for (int count = 1; count <= 5; count++) { System.out.print(3 * count " "); }

(Offline) Practice: Mapping loop# to numbers for (int count = 1; count <= 5; count++) { System.out.print(... ); }  What statement in the body would cause the loop to print: for (int count = 1; count <= 5; count++) { System.out.print(5 * count " "); }

Summary: Code Style 22 System.out.print("T-minus "); for (int i = 1; i <= 10; i++) { System.out.print(11-i + “, “); } System.out.println("blastoff!"); System.out.print("T-minus "); for (int i = 10; i >= 1; i--) { System.out.print(i + “, “); } System.out.println("blastoff!"); Counting down or up is mostly a personal style.

Counting Down: v3  If I want to count down from 12, what should we change?  T-minus 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, blastoff! System.out.print("T-minus "); for (int i = 1; i <= 10; i++) { System.out.print(11-i + “, “); } System.out.println("blastoff!"); Change to 12?

Counting Down: v3  Problem: The code has two “magic” numbers 11 and 10, but they are not independent 24 System.out.print("T-minus "); for (int i = 1; i <= 10; i++) { System.out.print(11-i + “, “); } System.out.println("blastoff!"); relation?

Good Code Style 25 int N = 10; System.out.print("T-minus "); for (int i = 1; i <= N; i++) { System.out.print(N+1-i + “, “); } System.out.println("blastoff!"); Good style: Minimize # magic numbers.

Counting Down: v4 Does the following program give the correct countdown?: 26 int N = 10; System.out.print("T-minus "); for (int i = 1; i <= N; N++) { System.out.print(N+1-i + “, “); } System.out.println("blastoff!");

Counting Down: v4 Does the following program gives the correct countdown?: 27 int N = 10; System.out.print("T-minus "); for (int i = 1; i <= N; N++) { System.out.print(N+1-i + “, “); } System.out.println("blastoff!"); Answer: No. There is a typo (N for i) Q: can the computer help me to find it (read my mind?)

Constant  Use keywords to tell computer your intension  If there is a final before a variable declaration, it is your promise to the computer that you will not modify it after declaration  If you break your promise, the compiler will catch you 28 CPSC112

Good Program Style 29 final int N = 10; System.out.print("T-minus "); for (int i = 1; i <= N; i++) { System.out.print(N+1-i + “, “); } System.out.println("blastoff!"); Convey your intention to the computer to help you.

Counting Down: Code Puzzle What if we want to print out the values of N and i after the loop: final int N = 10; System.out.print("T-minus "); for (int i = 1; i <= N; i++) { System.out.print(N+1-i + “, “); } System.out.println("blastoff!"); System.out.println(“N = “ + N); //? System.out.println(“Final i =“ + i); //?

Counting Down: Code Puzzle % javac CountDownValue.java CountDownValue.java:25: cannot find symbol symbol : variable i location: class CountDownValue System.out.println( "Final i = " + i ); ^ 1 error

Outline  Admin and recap  Loop examples  Variable scoping 32

Variable Scope  Scope: The part of a program where a variable exists.  Basic rule: from its declaration to the end of the enclosing { } braces  Examples  A variable declared in a for loop exists only in that loop. m A variable declared in a specific method exists only in that method. m A variable declared not inside any method but in a class is said to have class scope.

Variable Scope i's scope x's scope sum's scope public class CountSum { static int N = 10; public static void main(String[] args) { countSum(); } public static void countSum() { System.out.print("T-minus "); int sum = 0; for (int i = 1; i <= N; i++) { System.out.println( i ); sum += i; } System.out.println(“N: “ + N); System.out.println(“Sum: “ + sum); } // end of countSum } // end of class x's scope N’s scope

Why Scope?  Encapsulation m e.g., different methods can use the same variable name without the need for coordination m many analogies: folders allow same file name so long in different folders public static void aMethod() { int x = 1; … } public static void bMethod() { int x = 2; … }

Loop Example  Does the following code work? public static void main() { for (int i = 1; i <= 10; i++) { System.out.print( 11-i + “ “ ); } System.out.println(); for (int i = 1; i <= 10; i++) { System.out.print(11 – i + “ “); } Output:

Loop Example  Does the following code work? for (int set = 1; set <= 5; set++) { for (int rps = 1; rps <= set; rps++) { System.out.print("*"); } System.out.println(); } Output: * ** *** **** *****

Outline  Admin and recap  Loop examples  Variable scoping  Nested loops 38

Nested Loop for (int set = 1; set <= 5; set++) { for (int rps = 1; rps <= set; rps++) { System.out.print("*"); } System.out.println(); }  A loop inside another loop is said to form a nested loop  The #loop times of the inner loop can depend on the outer loop variable

Practice: Nested for loop example  What is the output of the following nested for loops? for (int i = 1; i <= 5; i++) { for (int j = 1; j <= i; j++) { System.out.print(i); } System.out.println(); }  Output:

Practice: Nested for loop example  What is the output of the following nested for loops? for (int i = 1; i <= 9; i++) { for (int j = 1; j <= i; j++) { System.out.print(i*j + “\t”); } System.out.println(); }

Nested Loop Design Example: Drawing A Complex Pattern  Use nested for loops to draw ASCII X  A size SIZE ASCII X has 2 * SIZE rows  Why draw ASCII art? m Real graphics will require some finesse (shortly) m ASCII art has complex patterns m Can focus on the algorithms == \ / /\ == \ / /\ == \ / /\ == SIZE 4 SIZE 3 SIZE 5