Lecture 6: Conditionals AP Computer Science Principles

Slides:



Advertisements
Similar presentations
Variables Conditionals Boolean Expressions Conditional Statements How a program produces different results based on varying circumstances if, else if,
Advertisements

BUILDING JAVA PROGRAMS CHAPTER 4 Conditional Execution.
Copyright 2010 by Pearson Education Building Java Programs Chapter 4 Lecture 4-2: Advanced if/else ; Cumulative sum reading: 4.1, 4.3, 4.5; "Procedural.
1 BUILDING JAVA PROGRAMS CHAPTER 4 CONDITIONAL EXECUTION.
Copyright 2008 by Pearson Education Building Java Programs Chapter 4 Lecture 4-1: if and if/else Statements reading: 4.2 self-check: #4-5, 7, 10, 11 exercises:
Copyright 2008 by Pearson Education Building Java Programs Chapter 4 Lecture 4-1: Scanner ; if/else reading: , 4.2, 4.6.
Running JavaScript Chapter 18.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 4: Conditional Execution.
CS305j Introduction to Computing Conditional Execution 1 Topic 12 Conditional Execution "We flew down weekly to meet with IBM, but they thought the way.
Building Java Programs Chapter 4 Conditional Execution Copyright (c) Pearson All rights reserved.
CS 112 Introduction to Programming Conditional Statements Boolean Expressions and Methods Yang (Richard) Yang Computer Science Department Yale University.
Topic 12 more if/else, cumulative algorithms, printf Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
Copyright 2008 by Pearson Education Building Java Programs Chapter 4 Lecture 4-1: if and if/else Statements reading: 4.2 self-check: #4-5, 7, 10, 11 exercises:
Decision Making - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 10/27/20151.
Building Java Programs
1 if / else statements. 2 Conditionals “If you eat your vegetables, then you can have dessert.” “If you do your homework, then you may go outside to play,
Lesson Two: Everything You Need to Know
Building Java Programs Chapter 4 Conditional Execution Copyright (c) Pearson All rights reserved.
1 CSE 142 Lecture Notes Conditional Execution with if Statements; Methods that Return Values (briefly) Chapters 3 and 4 Suggested reading: ;
Copyright 2008 by Pearson Education Building Java Programs Chapter 4 Lecture 4-1: if and if/else Statements reading: 4.2 self-check: #4-5, 7, 10, 11 exercises:
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Copyright 2008 by Pearson Education 1 The if statement Executes a block of statements only if a test is true if ( test ) { statement ;... statement ; }
Copyright 2010 by Pearson Education The if/else statement reading: 4.1, 4.6.
CSc 110, Autumn 2016 Lecture 9: input ; if/else Adapted from slides by Marty Stepp and Stuart Reges.
1 Building Java Programs Chapter 4: Conditional Execution These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted,
CSc 110, Autumn 2017 Lecture 13: Cumulative Sum and Boolean Logic
Building Java Programs
CprE 185: Intro to Problem Solving (using C)
Building Java Programs
Building Java Programs Chapter 4
Lecture 4: Program Control Flow
Factoring if/else code
Building Java Programs
Adapted from slides by Marty Stepp and Stuart Reges
Lecture 4: Conditionals
Building Java Programs
CSc 110, Spring 2017 Lecture 8: input; if/else
Building Java Programs
Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs Chapter 4
Building Java Programs
Executes a block of statements only if a test is true
CSc 110, Autumn 2016 Lecture 9: input; if/else
Building Java Programs
CSc 110, Autumn 2016 Lecture 10: Advanced if/else; Cumulative sum
Building Java Programs
Building Java Programs
Building Java Programs
Lecture 5: Basic Java Syntax AP Computer Science Principles
Lecture 8:The For Loop AP Computer Science Principles.
Building Java Programs
Building Java Programs
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
Building Java Programs
Building Java Programs
Building Java Programs
Chapter 4 Lecture 4-1: Scanner; if/else reading: 3.3 – 3.4, 4.1, 4.5
Building Java Programs Chapter 4
Building Java Programs
Factoring if/else code
Building Java Programs
Building Java Programs
Presentation transcript:

Lecture 6: Conditionals AP Computer Science Principles

Type boolean

Type boolean boolean: A logical type whose values are true and false. int age = 18; boolean minor = age < 21; //true boolean lovesCS = true;

Executes a block of statements only if the test is true The if statement Executes a block of statements only if the test is true if (test) { statement; ... } Example: float gpa = 2.5; if (gpa >= 2.0) { println("Application accepted.");

Executes one block if a test is true, another if false The if/else statement Executes one block if a test is true, another if false if (test) { statement(s); } else { // no test after else } Example: float gpa = 1.9; if (gpa >= 2.0) { println("Welcome to Mars University!"); } else { println("Application denied.");

Relational expressions Tests use 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 Note that == tests equality, not = . The = is used for the assignment operator!

Misuse of if What's wrong with the following code? 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 F!");

Chooses between outcomes using many tests Nested if/else Chooses between outcomes using many tests if (test) { statement(s); } else if (test) { } else { } Example: if (x > 0) { println("Positive"); } else if (x < 0) { println("Negative"); println("Zero");

Nested if/else/if Example: If it ends with else, exactly one path must be taken. If it ends with if, the code might not execute any path. if (test) { statement(s); } else if (test) { } Example: if (place == 1) { println("Gold medal!"); } else if (place == 2) { println("Silver medal!"); } else if (place == 3) { println("Bronze medal.");

Nested if structures exactly 1 path (mutually exclusive) if (test) { statement(s); } else if (test) { } else { } 0 or 1 path (mutually exclusive) 0, 1, or many paths (independent tests; not exclusive)

Which nested if/else? (1) if/if/if (2) nested if/else (3) nested if/else/if Whether a user is lower, middle, or upper-class based on income. (2) nested if / else if / else Whether you made the dean's list (GPA ≥ 3.8) or honor roll (3.5-3.8). (3) nested if / else if Whether a number is divisible by 2, 3, and/or 5. (1) sequential if / if / if Computing a grade of A, B, C, D, or F based on a percentage. (2) nested if / else if / else if / else if / else

Logical operators Tests can be combined using logical operators: "Truth tables" for each, used with logical values 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

Using boolean boolean goodAge = age >= 18 && age < 29; boolean goodHeight = height >= 70 && height < 76; boolean rich = salary >= 100000.0; if ((goodAge && goodHeight) || rich) { println("Okay, let's go out!"); } else { println("It's not you, it's me..."); }

Evaluating logic expressions Relational operators have lower precedence than math. 5 * 7 >= 3 + 5 * (7 - 1) 5 * 7 >= 3 + 5 * 6 35 >= 3 + 30 35 >= 33 True Relational operators cannot be "chained" as in algebra. 2 <= x <= 10 Instead, combine multiple tests with && or || 2 <= x && x <= 10 true && false false

Evaluating logic expressions ! is evaluated first, then AND is evaluated before OR. int x = 2; int y = 4; int z = 5; z > 2 || x > 3 && y < 3 ; // true if evaluate && before || // false if evaluate || before && // the correct answer is true: && // must be evaluated before ||

Logical questions What is the result of each of the following expressions? int x = 42; int y = 17; int z = 25; y < x && y <= z x % 2 == y % 2 || x % 2 == z % 2 x <= y + z && x >= y + z !(x < y && x < z) (x + y) % 2 == 0 || !((z - y) % 2 == 0) Answers: true, false, true, true, false

Scope scope: The part of a program where a variable exists. From its declaration to the end of the { } braces A variable declared in a if block exists only in that block. A variable declared in a method exists only in that method. void setup() { int x = 3; if(x == 3){ println(x); // x exists here } // x exists here // x ceases to exist here

If Block if(x <= 3) { int y = 2; } y = 5; // error since y does not exist outside // the if block 18

Global vs Local Variables Any variable declared BEFORE setup() is global variable and has a scope that spanned the entire program. A variable declared inside of a method or a block is a local variable that exists only for that method or block. int x = 5; //global variabe void setup(){ if(x > 10){ int y = 2; //local variable println(y); //y exists here, as does x. } println(x); //ok println(y); //Error! Outside of y’s scope. 19

Global vs Local Variables int x = 5; // global variable void setup(){ println(x); //x exists here int y = 10; // local variable printMyNumber(x); } void draw() { println(x); // x exists here also. println(y); // Error! y does not exist here. void printMyNumber(int x){//declaring a local variable x println(x); // x exists here // however, this x is a local variable // not the same as the global x above 20

Lab 1: Mouse Collision I Write a program that that display a circle whose fill is red if the mouse is inside the circle and blue if it is outside. Use the built in dist(x1,y1,x2,y2) to compute the distance between (x1,y1) and (x2,y2).

Lab 2: Mouse Collision II Modify the previous program to display a circle and a rectangle that is initially filled with the color black. If the mouse is inside of the circle, the circle turns red. If it is inside of the rectangle, the rectangle turns blue. If the mouse is not in either shape, both remains black.

Lab 2: Mouse Collision II

Lab 3: Quadrants Create a program that has a square window size, for example, size(600,600). Draw a horizontal line and a vertical line that divides the window into four equal quadrants. Modify the program so that the quadrant containing the current mouse position will light up red and stays the same background color otherwise.

Lab 4: Quadrants II This program is similar to the previous program except that the quadrant will light up red if the mouse position has been there before. Create a copy of the previous program instead of modifying it.

Homework Read and reread these lecture notes. Complete the problem set. Complete the labs.

References Part of this lecture is taken from the following book. 1) Stuart Reges and Marty Stepp. Building Java Programs: A Back to Basics Approach. Pearson Education. 2008.