Presentation is loading. Please wait.

Presentation is loading. Please wait.

SSEA Computer Science: Track A

Similar presentations


Presentation on theme: "SSEA Computer Science: Track A"— Presentation transcript:

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

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

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

4 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.

5 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

6 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!"); ...

7 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.");

8 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 {

9 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

10 “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

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

12 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

13 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

14 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!”); }

15 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?

16 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 blastoff! The end.

17 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).

18 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. ***** ***** * ***** **** ** ***** *** *** ***** ** **** ***** * ***** (Bonus: How would you modify the code to produce each output above?) answer: C

19 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!

20 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.


Download ppt "SSEA Computer Science: Track A"

Similar presentations


Ads by Google