CS106A, Stanford University

Slides:



Advertisements
Similar presentations
Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
Advertisements

Computer Science 1620 Loops.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Loops – While, Do, For Repetition Statements Introduction to Arrays
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
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.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Python quick start guide
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Agenda Exam #1 Review Modulus Conditionals Boolean Algebra Reading: Chapter Homework #5.
Flow of Control Part 1: Selection
CPS120: Introduction to Computer Science Decision Making in Programs.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Operators Precedence - Operators with the highest precedence will be executed first. Page 54 of the book and Appendix B list C's operator precedence. Parenthesis.
Chapter Making Decisions 4. Relational Operators 4.1.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
In the last lesson we discussed about: Casting Precedence The “=“ used as an assignment operator Made a calculate average program.
Conditionals Opening Discussion zWhat did we talk about last class? zDo you have any questions about the assignment? zPass by value limitations.
1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
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.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Programming in Java (COP 2250) Lecture 12 & 13 Chengyong Yang Fall, 2005.
Inside Class Methods Chapter 4. 4 What are variables? Variables store values within methods and may change value as the method processes data.
Information and Computer Sciences University of Hawaii, Manoa
Chapter 4 Repetition Statements (loops)
Review.
Building Java Programs
Chapter 4: Making Decisions.
Primitive Data, Variables, Loops (Maybe)
Chapter 6: Conditional Statements and Loops
Computation as an Expressive Medium
CS 106A, Lecture 7 Parameters and Return
CS 106A, Lecture 6 Control Flow and Parameters
CS106A, Stanford University
CS 106A, Lecture 5 Booleans, Control Flow and Scope
Building Java Programs Chapter 2
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Building Java Programs
Building Java Programs
CS106A, Stanford University
Building Java Programs
Selection Statements.
Building Java Programs Chapter 2
Building Java Programs
SSEA Computer Science: Track A
Chapter 2 Programming Basics.
SSEA Computer Science: Track A
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Relational Operators.
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs Chapter 2
Building Java Programs
Building Java Programs
Building Java Programs
Problem 1 Given n, calculate 2n
Building Java Programs
Building Java Programs
Presentation transcript:

CS106A, Stanford University Simple Java Chris Piech CS106A, Stanford University

Let’s fix an old program

Review: Operations Operations on numerical types Operations: + “addition” - “subtraction” * “multiplication” / “division” (different for int vs. double) % “remainder” Precedence (in order): () highest *, /, % +, - lowest Operators in same precedence category evaluated left to right

Expressions Short Hand int x = 3; x = x + 1; x += 1; x++; x = x + 5; x += 5; x = x – 1; x -= 1; x--; x = x * 3; x *= 3; x = x / 2; x /= 2;

Review: Boolean Expressions Boolean expression is just a test for a condition Essentially, evaluates to true or false Value comparisons: == “equals” (note: not single =) != “not equals” (cannot say <>) > “greater than” < “less than” >= “greater than or equal to” <= “less than or equal to”

Basics of boolean variables Today’s Goal How to use constants Basics of boolean variables Understand For loops Know variable scope

Today’s Route Simple Java For Loops Game Show Scope The River of Java Booleans The River of Java

Boolean variable type

Boolean Expressions Value comparisons (in order of precidence): ! “not” !p If p is true then !p is false (and vice versa)

Boolean Expressions Value comparisons (in order of precidence): ! “not” !p && “and” p && q If p is true then !p is false (and vice versa) Evaluates to true if both sides are true

Boolean Expressions Value comparisons (in order of precidence): ! “not” !p && “and” p && q || “or” p || q If p is true then !p is false (and vice versa) Evaluates to true if both sides are true Evaluates to true if either p or q (or both) are true

Boolean Expressions Value comparisons (in order of precidence): ! “not” !p && “and” p && q || “or” p || q If p is true then !p is false (and vice versa) Evaluates to true if both sides are true Evaluates to true if either p or q (or both) are true boolean p = (x != 1) || (x != 2);

Boolean Expressions Value comparisons (in order of precidence): ! “not” !p && “and” p && q || “or” p || q If p is true then !p is false (and vice versa) Evaluates to true if both sides are true Evaluates to true if either p or q (or both) are true boolean p = (x != 1) || (x != 2); boolean p = (x != 1) && (X != 2);

*know your logical precedence

Today’s Route Simple Java For Loops Game Show Scope The River of Java Booleans The River of Java

Today’s Route Simple Java For Loops Game Show Scope The River of Java Booleans The River of Java

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

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++;

Today’s Route Simple Java For Loops Game Show Scope The River of Java Booleans The River of Java

Today’s Route Simple Java For Loops Game Show Scope The River of Java Booleans The River of Java

How would you println “Nick rocks socks” 100 times

println(“Nick rocks socks!”);

For Loop Redux public void run() { for(int i = 0; i < 100; i++) { println(“Nick 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(“Nick rocks socks!”); }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

For Loop Redux for(int i = 0; i < 3; i++) { println(“Nick rocks socks!”); } For Loop Redux Nick rocks socks Nick rocks socks Nick 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

Milestone 1

Milestone 2

Milestone 3

Today’s Route You are here Simple Java For Loops Conditions Graphics Review The River of Java

Today’s Route You are here Simple Java For Loops Conditions Graphics Review The River of Java

Basics of boolean variables Today’s Goal How to use constants Basics of boolean variables Understand For loops Know variable scope