CS106A, Stanford University

Slides:



Advertisements
Similar presentations
Chapter 7 - Iteration. Chapter Goals Program repitiation statements – or loops – with the for, while, and do-while statements Program repitiation statements.
Advertisements

Chapter 2 Programming by Example. A Holistic Perspective Three sections separated by blank lines. Program comment File: FileName.java
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
CS324e - Elements of Graphics and Visualization A Little Java A Little Python.
IAT 800 Lab 1: Loops, Animation, and Simple User Interaction.
1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
Chapter 7: User-Defined Methods
Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.
Python quick start guide
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Continuous February 16, Test Review What expression represents the zip car eligibility rules of at least 18 years old and no incidents?
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Methods We write methods in our programs for many reasons:
Often being different. Control flow By default Java (and therefore Processing) executes lines of a program one after the other –Doesn’t matter what happened.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Methods Awesomeness!!!. Methods Methods give a name to a section of code Methods give a name to a section of code Methods have a number of important uses.
Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Review Expressions and operators Iteration – while-loop – for-loop.
Test Review. General Info. All tests will be comprehensive. You will be tested more on your understanding of code as opposed to your ability to write.
Nested For Loops. First, the rules to these loops do not change. We are sticking one loop inside another. while(condition) { // loop body } do { // loop.
1 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
Functions. What is a Function?  We have already used a few functions. Can you give some examples?  Some functions take a comma-separated list of arguments.
Variable Scope. When you declare a variable, that name and value is only “alive” for some parts of the program  We must declare variables before we use.
CS 106A, Lecture 27 Final Exam Review 1
CS 106A, Lecture 4 Introduction to Java
Core Java Statements in Java.
Functions + Overloading + Scope
Chapter 7 User-Defined Methods.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 7 User-Defined Methods.
Chapter 4 Repetition Statements (loops)
The Lifetime of a Variable
AKA the birth, life, and death of variables.
Review.
CS106A, Stanford University
Java Programming: Guided Learning with Early Objects
Beyond Console Programs
SSEA Computer Science: CS106A
reading: Art & Science of Java,
CS106A, Stanford University
CS 106A, Lecture 15 Events and Memory
Computation as an Expressive Medium
CS 106A, Lecture 12 More Graphics
CS106A, Stanford University
CS 106A, Lecture 7 Parameters and Return
CS 106A, Lecture 6 Control Flow and Parameters
CS106A, Stanford University
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
CS 106A, Lecture 22 More Classes
Arrays, For loop While loop Do while loop
CS 106A, Lecture 5 Booleans, Control Flow and Scope
Code Elements and Processing Coordinate System
CS106A, Stanford University
SSEA Computer Science: Track A
CHAPTER 6 GENERAL-PURPOSE METHODS
Graphics.
CS106A, Stanford University
Variables Title slide variables.
Code Elements and Processing Coordinate System
CS106A, Stanford University
CS106A, Stanford University
SSEA Computer Science: Track A
CS106A, Stanford University
CS106A, Stanford University
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
Corresponds with Chapter 5
Looping and Repetition
Week 7 - Monday CS 121.
Presentation transcript:

CS106A, Stanford University Nested Loops Chris Piech CS106A, Stanford University

By Chris

Once upon a time…

X was looking for love! int x = 5; if(lookingForLove()) { int y = 5; } println(x + y); 5 x

X was looking for love! int x = 5; if(lookingForLove()) { int y = 5; } println(x + y); 5 x

X was looking for love! int x = 5; if(lookingForLove()) { int y = 5; } x was definitely looking for love int x = 5; if(lookingForLove()) { int y = 5; } println(x + y); 5 x

And met y int x = 5; if(lookingForLove()) { int y = 5; } println(x + y); 5 5 x y

And met y int x = 5; if(lookingForLove()) { int y = 5; } println(x + y); 5 5 Hi, I’m y x y

“Wow!”

And met y int x = 5; if(lookingForLove()) { int y = 5; } println(x + y); Wow 5 5 x y

And met y int x = 5; if(lookingForLove()) { int y = 5; } println(x + y); 5 5 We have so much in common x y

And met y int x = 5; if(lookingForLove()) { int y = 5; } println(x + y); 5 5 We both have value 5! x y

And met y int x = 5; if(lookingForLove()) { int y = 5; } println(x + y); 5 5 Maybe one day we can… x y

And met y int x = 5; if(lookingForLove()) { int y = 5; } println(x + y); 5 5 println together? x y

They got along int x = 5; if(lookingForLove()) { int y = 5; } println(x + y); 5 5 x y

It was a beautiful match…

But then tragedy struck.

Tragedy Struck int x = 5; if(lookingForLove()) { int y = 5; } println(x + y); 5 5 x y

Tragedy Struck int x = 5; if(lookingForLove()) { int y = 5; } println(x + y); 5 5 x y

Tragedy Struck int x = 5; if(lookingForLove()) { int y = 5; } println(x + y); 5 x

Noooooooooooooooo!

You see…

When a program exits a code block… int x = 5; if(lookingForLove()) { int y = 5; } println(x + y); 5 x

All variables declared inside that block.. int x = 5; if(lookingForLove()) { int y = 5; } println(x + y); 5 x

Get deleted from memory! int x = 5; if(lookingForLove()) { int y = 5; } println(x + y); 5 x

Since y was declared in the if-block int x = 5; if(lookingForLove()) { int y = 5; } println(x + y); 5 x

It gets deleted from memory here int x = 5; if(lookingForLove()) { int y = 5; } println(x + y); 5 x

And doesn’t exist here int x = 5; if(lookingForLove()) { int y = 5; } println(x + y); 5 x

Error. Undefined variable y. And doesn’t exist here Error. Undefined variable y. int x = 5; if(lookingForLove()) { int y = 5; } println(x + y); 5 x

The End

Sad times 

Variables have a lifetime (called scope) public void run(){ double v = 8; if(condition){ v = 4; … some code } … some other code

Variables have a lifetime (called scope) public void run(){ double v = 8; if(condition){ v = 4; … some code } … some other code

Vars come to existence when declared public void run(){ double v = 8; if(condition){ v = 4; … some code } … some other code Comes to life here 8 v

Live until end of their code block public void run(){ double v = 8; if(condition){ v = 4; … some code } … some other code This is the inner most code block in which it was declared…. 4 v

Live until end of their code block public void run(){ double v = 8; if(condition){ v = 4; … some code } … some other code Still alive here… 4 v

Live until end of their code block public void run(){ double v = 8; if(condition){ v = 4; … some code } … some other code 4 v It dies here (at the end of its code block)

Live until end of their code block public void run(){ double v = 8; if(condition){ v = 4; … some code } … some other code It dies here (at the end of its code block)

Example 2 public void run(){ … some code if(condition){ int w = 4; } … some other code This is the scope of w

Example 2 public void run(){ … some code if(condition){ int w = 4; } … some other code w comes to life here w dies here (at the end of its code block)

Chapter 2

The programmer fixed her bug

x was looking for love! int x = 5; if(lookingForLove()) { int y = 5; println(x + y); } 5 x

x was looking for love… int x = 5; if(lookingForLove()) { int y = 5; x was definitely looking for love int x = 5; if(lookingForLove()) { int y = 5; println(x + y); } 5 x

x met y int x = 5; if(lookingForLove()) { int y = 5; println(x + y); }

Since they were both “in scope” int x = 5; if(lookingForLove()) { int y = 5; println(x + y); } 5 5 x y

The story had a happy ending!

Scope Formally The scope of a variable refers to the section of code where a variable can be accessed. Scope starts where the variable is declared. Scope ends at the termination of the inner-most code block in which the variable was defined. A code block is a chunk of code between { } brackets

Back to our regularly scheduled program…

How would you println “Stanford rocks socks” 100 times

For Loop Redux public void run() { for(int i = 0; i < 100; i++) { println(“Stanford rocks socks!”); }

For Loop Redux for(int i = 0; i < 100; i++) { Enters the loop if this condition passes This line is run each time the code gets to the end of the ‘body’ This line is run once, just before the for loop starts for(int i = 0; i < 100; i++) { println(“Stanford rocks socks!”); }

For Loop Redux for(int i = 0; i < 3; i++) { println(“Stanford rocks socks!”); } For Loop Redux

For Loop Redux for(int i = 0; i < 3; i++) { println(“Stanford rocks socks!”); } For Loop Redux

For Loop Redux i for(int i = 0; i < 3; i++) { for(int i = 0; i < 3; i++) { println(“Stanford rocks socks!”); } For Loop Redux

For Loop Redux i for(int i = 0; i < 3; i++) { for(int i = 0; i < 3; i++) { println(“Stanford rocks socks!”); } For Loop Redux

For Loop Redux i for(int i = 0; i < 3; i++) { for(int i = 0; i < 3; i++) { println(“Stanford rocks socks!”); } For Loop Redux Stanford rocks socks

For Loop Redux i 1 for(int i = 0; i < 3; i++) { println(“Stanford rocks socks!”); } For Loop Redux Stanford rocks socks

For Loop Redux i 1 for(int i = 0; i < 3; i++) { println(“Stanford rocks socks!”); } For Loop Redux Stanford rocks socks

For Loop Redux i 1 for(int i = 0; i < 3; i++) { println(“Stanford rocks socks!”); } For Loop Redux Stanford rocks socks Stanford rocks socks

For Loop Redux i 2 for(int i = 0; i < 3; i++) { println(“Stanford rocks socks!”); } For Loop Redux Stanford rocks socks Stanford rocks socks

For Loop Redux i 2 for(int i = 0; i < 3; i++) { println(“Stanford rocks socks!”); } For Loop Redux Stanford rocks socks Stanford rocks socks

For Loop Redux i 2 for(int i = 0; i < 3; i++) { println(“Stanford rocks socks!”); } For Loop Redux Stanford rocks socks Stanford rocks socks Stanford rocks socks

For Loop Redux i 3 for(int i = 0; i < 3; i++) { println(“Stanford rocks socks!”); } For Loop Redux Stanford rocks socks Stanford rocks socks Stanford rocks socks

For Loop Redux i 3 for(int i = 0; i < 3; i++) { println(“Stanford rocks socks!”); } For Loop Redux Stanford rocks socks Stanford rocks socks Stanford rocks socks

For Loop Redux for(int i = 0; i < 3; i++) { println(“Stanford rocks socks!”); } For Loop Redux Stanford rocks socks Stanford rocks socks Stanford rocks socks

For Loop Redux for(int i = 0; i < 3; i++) { println(“Stanford rocks socks!”); } For Loop Redux Stanford rocks socks Stanford rocks socks Stanford rocks socks

You can use the for loop variable

How would you println the first 100 even numbers?

Printing Even Numbers

Printing Even Numbers for(int i = 0; i < NUM_NUMS; i++) { println(i * 2); }

Printing Even Numbers for(int i = 0; i < 3; i++) { println(i * 2); } For Loop Redux

Printing Even Numbers for(int i = 0; i < 3; i++) { println(i * 2); } For Loop Redux

Printing Even Numbers i for(int i = 0; i < 3; i++) { for(int i = 0; i < 3; i++) { println(i * 2); } For Loop Redux

Printing Even Numbers i for(int i = 0; i < 3; i++) { for(int i = 0; i < 3; i++) { println(i * 2); } For Loop Redux

Printing Even Numbers i for(int i = 0; i < 3; i++) { for(int i = 0; i < 3; i++) { println(i * 2); } For Loop Redux

Printing Even Numbers i 1 for(int i = 0; i < 3; i++) { println(i * 2); } For Loop Redux

Printing Even Numbers i 1 for(int i = 0; i < 3; i++) { println(i * 2); } For Loop Redux

Printing Even Numbers i 1 for(int i = 0; i < 3; i++) { println(i * 2); } For Loop Redux 2

Printing Even Numbers i 2 for(int i = 0; i < 3; i++) { println(i * 2); } For Loop Redux 2

Printing Even Numbers i 2 for(int i = 0; i < 3; i++) { println(i * 2); } For Loop Redux 2

Printing Even Numbers i 2 for(int i = 0; i < 3; i++) { println(i * 2); } For Loop Redux 2 4

Printing Even Numbers i 3 for(int i = 0; i < 3; i++) { println(i * 2); } For Loop Redux 2 4

Printing Even Numbers i 3 for(int i = 0; i < 3; i++) { println(i * 2); } For Loop Redux 2 4

Printing Even Numbers i 3 for(int i = 0; i < 3; i++) { println(i * 2); } For Loop Redux 2 4

Printing Even Numbers for(int i = 0; i < 3; i++) { println(i * 2); } For Loop Redux 2 4

Types of Programs Program Console Program Graphics Program Karel Program SuperKarel Program

Graphics Programs

GRect GRect is a variable type that stores a rectangle. As an example, the following run method displays a blue square public void run() { GRect rect = new GRect(50, 50, 200, 200); rect.setFilled(true); rect.setColor(Color.BLUE); add(rect); } LargestOval

Graphics Coordinates 0,0 40,20 120,40 getHeight(); 40,120 getWidth();

GOval The GOval class represents an elliptical shape defined by the boundaries of its enclosing rectangle. As an example, the following run method creates the largest oval that fits within the canvas: public void run() { GOval oval = new GOval(0, 0, getWidth(), getHeight()); oval.setFilled(true); oval.setColor(Color.GREEN); add(oval); } LargestOval

Graphics Variable Types GObject GLabel GRect GOval others… GRect myRect = new GRect(350, 270);

Primitives vs Classes Primitive Variable Types Class Variable Types int double char boolean GRect GOval GLine … Class variables: Have upper camel case types You can call methods on them Are constructed using new Are stored in a special way

Goal

Milestone 1

Milestone 2

Milestone 3