SSEA Computer Science: Track A

Slides:



Advertisements
Similar presentations
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:
Advertisements

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.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 4: Conditional Execution.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
1 for Loops Computer Science is a science of abstraction - creating the right model for a problem and devising the appropriate mechanizable techniques.
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
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);
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:
In the last lesson we discussed about: Casting Precedence The “=“ used as an assignment operator Made a calculate average program.
Building Java Programs Chapter 2 Primitive Data and Definite Loops Copyright (c) Pearson All rights reserved.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Autumn 2017 Lecture 13: Cumulative Sum and Boolean Logic
Building Java Programs
Loop Structures.
Lecture 4: Program Control Flow
Primitive Data, Variables, Loops (Maybe)
Lecture 4: Conditionals
Building Java Programs
CS 106A, Lecture 6 Control Flow and Parameters
Building Java Programs Chapter 4
Outline Altering flow of control Boolean expressions
CSc 110, Spring 2017 Lecture 9: Advanced if/else; Cumulative sum
Building Java Programs
Executes a block of statements only if a test is true
Algorithm Discovery and Design
CSc 110, Spring 2017 Lecture 4: Nested Loops and Loop Figures
CSc 110, Autumn 2016 Lecture 10: Advanced if/else; Cumulative sum
Building Java Programs
Building Java Programs
CSc 110, Spring 2018 Lecture 13: Cumulative Sum and Boolean Logic
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.
Lecture 5: For Loops Building Java Programs: A Back to Basics Approach
Building Java Programs
Building Java Programs
Building Java Programs
Lecture 8:The For Loop AP Computer Science Principles.
Building Java Programs
Building Java Programs
Building Java Programs
Lecture 6: Conditionals AP Computer Science Principles
Building Java Programs
Chapter 2 Programming Basics.
SSEA Computer Science: Track A
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
Chapter 2 Lecture 2-2: The for Loop reading: 2.3
Review of Previous Lesson
Building Java Programs
Common pattern/tool: Accumulator loops
Presentation transcript:

SSEA Computer Science: Track A Dr. Cynthia Lee Lecturer in Computer Science Stanford

Topics for today Java programming language: new tools Two new kinds of operators: RELATIONAL operators (>, <, ==, !=, >=, <=) LOGICAL operators (&&, ||, !) A new data type: boolean

Relational and Logical Operators The operators we learned previously were primarily arithmetic operators

Relational operators Operator Meaning Example Value == equals 1 + 1 == 2 true != does not equal 3.2 != 2.5 < less than 10 < 5 false > greater than 10 > 5 <= less than or equal to 126 <= 100 >= greater than or equal to 5.0 >= 5.0 if statements and loops use logical tests. for (int i = 0; i < 10; i++) { ... if (age >= 40) { ... These are boolean expressions. Boolean is a logical data type.

New data type: boolean Only two possible values: true and false Actually write “true” and “false” (without the quotes) in your code: Named after George Boole, a pioneer in the area of formal logic I’m assuming not actually an angry person, just had resting angry face

Misuse of if What's wrong with the following code? int percent = readInt("What percentage did you earn? "); if (percent >= 90) { println("You got an A!"); } if (percent >= 80) { println("You got a B!"); if (percent >= 70) { println("You got a C!"); if (percent >= 60) { println("You got a D!"); if (percent < 60) { println("You got an NP!"); ...

Ends with else: exactly 1 path taken. Ends with if: 0-1 paths taken. Solution: if/else-if Ends with else: exactly 1 path taken. Ends with if: 0-1 paths taken. if (test) { statements; } else if (test) { } Example: if (place == 1) { println("Gold medal!!"); } else if (place == 2) { println("Silver medal!"); } else if (place == 3) { println("Bronze medal.");

Unnecessary if The following code is unnecessarily verbose and redundant: if (x < 0) { println("x is negative"); } else if (x >= 0) { println("x is non-negative"); } The second test is unnecessary and can be removed: } else {

Logical operators Operator Description Example Result && and Tests can be combined using logical operators: "Truth tables" for each, used with logical tests p and q: Operator Description Example Result && and (2 == 3) && (-1 < 5) false || or (2 == 3) || (-1 < 5) true ! not !(2 == 3) p q p && q p || q true false p !p true false

“Or”/|| in code p q p && q p || q true false “Or” in code means “one or the other or both” In English, sometimes we use “or” to include the both case, and sometimes we don’t mean it to include the both case: Inclusive “or” includes the both case: “You will be in great shape if you go running or biking daily”—of course you will also be in great shape if you do both! Exclusive “or” excludes the both case: “Your lunch special comes with soup or salad.”—usually restaurants mean you have to choose only one or other. p q p && q p || q true false

Revisiting for Let’s take another look at the for loop, now that we know the tools that describe each part!

Revisiting for loops for (initialization; test; update) { header statements; } for (int i=0; i<10; i++) { println(i); Perform initialization once. Repeat the following: Check if the test is true. If not, stop. Execute the statements. Perform the update. Fun fact! If any variables are declared in the initialization, they can be used in the body of the loop. header body

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

Nested loop question Q: How many of these do the same thing as this one? 1 2 3 4 for (int i = 0; i < 5; i++) { println(“Happy!”); } for (int i = 1; i < 6; i++) { println(“Happy!”); } for (int i = 1; i < 5; i++) { println(“Happy!”); } answer: C for (int i = 1; i <= 5; i++) { println(“Happy!”); } for (int i = 0; i <= 5; i++) { println(“Happy!”); }

Using the loop variable println("Let's count!"); for (int i = 0; i < 5; i++) { println(i + "..."); } println(“Blastoff!"); Output: Let's count! 0... 1... 2... 3... 4... Blastoff! Would be cooler if it counted DOWN, right? How could we do that?

Decrementing loops The update can use -- to make the loop count down. The test should say > instead of < Note: The print method displays output without going to the next line. print("T-minus "); for (int i = 10; i >= 1; i--) { print(i + " "); } println(“Blastoff!"); println("The end."); Output: T-minus 10 9 8 7 6 5 4 3 2 1 blastoff! The end.

Nested loops for (int i = 1; i <= 5; i++) { nested loop: A loop placed inside another loop. for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 10; j++) { print("*"); } println(); // to end the line Output: ********** The outer loop repeats 5 times; the inner one 10 times per outer loop repeat (50 times in all).

Nested loop question Q: What output is produced by the following code? HINT: Rocket drawing homework problem!! Nested loop question Q: What output is produced by the following code? for (int i = 1; i <= 5; i++) { for (int j = 1; j <= i; j++) { print("*"); } println(); A. B. C. D. E. ***** ***** * 1 12345 ***** **** ** 22 ***** *** *** 333 ***** ** **** 4444 ***** * ***** 55555 (Bonus: How would you modify the code to produce each output above?) answer: C

Nested loop question HINT: Rocket drawing homework problem!! (see solution code provided in class web directory) How would we produce the following output? ....1 ...2. ..3.. .4... 5.... This should help with your rocketship ASCII art homework problem!

Common pattern/tool: Accumulator loops HINT: Exam scores homework problem!! int sum = 0; // initialize these OUTSIDE loop int count = 0; int biggest = -1; for (int i = 1; i <= 1000; i++) { sum = sum + i; // update each of these INSIDE loop count++; if (i > biggest) { // unlike sum and count, we only update biggest = examscore; // biggest some of the time (i.e., when // we see a number that is bigger than previous record } double average = sum/count; println("The sum is " + sum); Accumulator variable: A variable that keeps some progress and is updated repeatedly until loop is finished. The sum in the above code is a cumulative sum. Accumulator variables must be declared outside the loops that update them, so that they will still exist after the loop.