COMP 110 Branching Statements and Boolean Expressions Luv
COMP 110 Branching Statements and Boolean Expressions Luv Kohli September 8, 2008 MWF 2 -2: 50 pm Sitterson 014 1
Announcements Lab 1 will be graded by Wednesday Program 1 due Wednesday, 2 pm Follow the assignment submission instructions! 2
Questions? 3
Today in COMP 110 if/else statements Boolean expressions Review
Flow chart Schematic representation of an algorithm Example: going to school in the morning Leave home Check weather Yes Sunny? Walk to school No Take the bus Reach school
Flow chart Leave home Rectangle: action or statement Arrow: flow of control, next step Check weather Yes Sunny? Walk to school Diamond: decision, conditional No Take the bus Reach school
Flow chart to pseudocode Leave home Check weather Yes Sunny? Walk to school Check weather No Take the bus Reach school If it is sunny, Walk to school Otherwise Take the bus Reach school
Flow chart, pseudocode, if/else Leave home Check weather Yes Sunny? Walk to school No Take the bus Reach school Leave home Check weather If it is sunny walk to school Otherwise take the bus Reach school Leave home Check weather if (sunny) walk to school else take the bus Reach school
Branching statement: if/else if (boolean expression) action 1 else action 2 If boolean expression is true, perform action 1, else perform action 2
Multiple statements per action if (boolean expression) { action 1 statements //. . . } else { action 2 statements //. . . }
Boolean Expressions An expression that is either true or false Examples: ◦ It is sunny today (true) ◦ 10 is larger than 5 (true) ◦ Today is Saturday (false) ◦ I am a secret agent (? ? ? ) Just for fun: Liar Paradox ◦ This sentence is false.
Boolean Expressions When programming, Boolean expressions are often used for comparisons
Java Comparison Operators == != > >= < <= Equal to Not equal to Greater than or equal to Less than or equal to Example expressions: variable <= 6 my. Int > 5 5 == 3
Java Example import java. util. *; Prompt user for integer Yes Print: “big number” Is input greater than 10? public class Flow. Chart { public static void main(String[] args) { System. out. println("Give me an integer: "); Scanner keyboard = new Scanner(System. in); int input. Int = keyboard. next. Int(); No Print: “small number” if (input. Int > 10) { System. out. println("big number"); } else { System. out. println("small number"); } } }
The && (AND) operator Only walk to school if it is between 50 AND 75 degrees true if ALL expressions are true if ((temperature > 50) && (temperature < 75)) { // walk to school } // note: you can use an if without an else
The || (OR) operator Walk to school if it is sunny OR cloudy true if AT LEAST ONE expression is true if (sunny || cloudy) { // walk to school }
boolean type Can be either true or false boolean sunny = true; boolean cloudy = false; if (sunny || cloudy) { // walk to school }
The ! (NOT) operator !true is false !false is true Example: walk to school if it is NOT cloudy if (!cloudy) { // walk to school }
The ! (NOT) operator if (input. Int > 10) { // walk to school } // is the same as if (!(input. Int <= 10)) { // walk to school }
Gotcha: == var 1 = var 2 (assignment statement) var 1 == var 2 (boolean expression) ◦ Error!!!!!!! Do NOT use == to compare Strings ◦ string 1 == string 2 // BAD ◦ string 1. equals(string 2); // GOOD
More than 2 branches Prompt user for size of chair size. Of. Chair > 10 What is the size of the chair? (size. Of. Chair >= 5) && (size. Of. Chair <= 10) size. Of. Chair < 5 Print: “just right” Print: “too big” Print: “too small”
More than 2 branches if (size. Of. Chair > 10) { System. out. println(“Too big”); } else if (size. Of. Chair < 5) { System. out. println(“Too small”); } else { System. out. println(“Just right”); }
Nested if/else if (size. Of. Chair > 10) { if (size. Of. Chair > 100) { System. out. println(“REALLY big!”); } else { System. out. println(“Somewhat big”); } } else { System. out. println(“small”); }
Chapters 1 and 2 Review In-class worksheet
Wednesday Program 1 due, 2 pm 25
- Slides: 25