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

Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
Control Structures. Decision Making Structures The if and if…else are all selection control structures that introduce decision-making ability into a program.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
Computer Science 1620 Loops.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
Loops – While, Do, For Repetition Statements Introduction to Arrays
The If/Else Statement, Boolean Flags, and Menus Page 180
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Lecture Review (If-else Statement) if-else statement has the following syntax: if ( condition ) { statement1; } else { statement2; } The condition.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
COMP More About Classes Yi Hong May 22, 2015.
Python quick start guide
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
Chapter 5 Loops.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Computer Science Department LOOPS. Computer Science Department Loops Loops Cause a section of your program to be repeated a certain number of times. The.
More on Logic Today we look at the for loop and then put all of this together to look at some more complex forms of logic that a program will need The.
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.
Iteration & Loop Statements 1 Iteration or Loop Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
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.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
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.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
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.
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.
Loops Review. How to generate random numbers Math.random() will return a random decimal value in the form of a double. double num1 = Math.random(); num1.
Information and Computer Sciences University of Hawaii, Manoa
Lecture 4b Repeating With Loops
Lecture 2 D&D Chapter 2 & Intro to Eclipse IDE Date.
Chapter 4 Repetition Statements (loops)
Unit 3 Lesson 9 Repetition Statements (Loops)
Review.
ECS10 10/10
IF statements.
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
CS106A, Stanford University
Beyond Console Programs
Chapter 6: Conditional Statements and Loops
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
SSEA Computer Science: CS106A
Engineering Innovation Center
CS106A, Stanford University
CS 106A, Lecture 15 Events and Memory
CS 106A, Lecture 7 Parameters and Return
CS 106A, Lecture 6 Control Flow and Parameters
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Arrays, For loop While loop Do while loop
CS 106A, Lecture 5 Booleans, Control Flow and Scope
CS106A, Stanford University
Outline Altering flow of control Boolean expressions
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
CS106A, Stanford University
T. Jumana Abu Shmais – AOU - Riyadh
Graphics.
CS106A, Stanford University
Module 4 Loops.
Lec 4: while loop and do-while loop
Chapter 6: Repetition Statements
Building Java Programs
CPS120: Introduction to Computer Science
SSEA Computer Science: Track A
Chapter 2 Programming Basics.
Week 7 - Monday CS 121.
Presentation transcript:

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

Boolean Variables // Store expressions that evaluate to true/false boolean x = 1 < 2; // true boolean y = 5.0 == 4.0; // false

Boolean Variables // Store expressions that evaluate to true/false boolean x = 1 < 2; // true boolean y = 5.0 == 4.0; // false // Directly set to true/false boolean isFamilyVisiting = true; boolean isRaining = false;

Boolean Variables // Store expressions that evaluate to true/false boolean x = 1 < 2; // true boolean y = 5.0 == 4.0; // false // Directly set to true/false boolean isFamilyVisiting = true; boolean isRaining = false; // Ask the user a true/false (yes/no) question boolean playAgain = readBoolean("Play again?”, "y", "n"); if (playAgain) { ... Note: does not store the whole expression – just the value. Note – we don’t need to check if it’s true

*know your logical precedence

Game Show

|| or && and Choose a Door int door = readInt("Door: "); // while the input is invalid while(door < 1 || door > 3) { // tell the user the input was invalid println("Invalid door!"); // ask for a new input door = readInt("Door: "); } || or && and

The Door Logic int prize = 4; if(door == 1) { } else if(door == 2) { boolean locked = prize % 2 != 0; if(!locked) { prize += 6; } } else if(door == 3) { prize++;

The Door Logic int prize = 4; if(door == 1) { } else if(door == 2) { boolean locked = prize % 2 != 0; if(!locked) { prize += 6; } } else if(door == 3) { prize++;

The Door Logic int prize = 4; if(door == 1) { } else if(door == 2) { boolean locked = prize % 2 != 0; if(!locked) { prize += 6; } } else if(door == 3) { prize++;

The Door Logic int prize = 4; if(door == 1) { } else if(door == 2) { boolean locked = prize % 2 != 0; if(!locked) { prize += 6; } } else if(door == 3) { prize++;

The Door Logic int prize = 4; if(door == 1) { } else if(door == 2) { boolean locked = prize % 2 != 0; if(!locked) { prize += 6; } } else if(door == 3) { prize++;

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

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…

Types of Programs Program Console Program Graphics Program Karel 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(200, 200); rect.setFilled(true); rect.setColor(Color.BLUE); add(rect, 50, 50); } Hello Rect rect

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(200, 200); rect.setFilled(true); rect.setColor(Color.BLUE); add(rect, 50, 50); } Hello Rect rect

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(200, 200); rect.setFilled(true); rect.setColor(Color.BLUE); add(rect, 50, 50); } Hello Rect rect

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(200, 200); rect.setFilled(true); rect.setColor(Color.BLUE); add(rect, 50, 50); } Hello Rect rect

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(200, 200); rect.setFilled(true); rect.setColor(Color.BLUE); add(rect, 50, 50); } Hello Rect rect

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

Goal

Milestone 1

Milestone 2

Milestone 3