Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP 110 Branching Statements and Boolean Expressions Luv Kohli September 8, 2008 MWF 2-2:50 pm Sitterson 014 1.

Similar presentations


Presentation on theme: "COMP 110 Branching Statements and Boolean Expressions Luv Kohli September 8, 2008 MWF 2-2:50 pm Sitterson 014 1."— Presentation transcript:

1 COMP 110 Branching Statements and Boolean Expressions Luv Kohli September 8, 2008 MWF 2-2:50 pm Sitterson 014 1

2 Announcements Lab 1 will be graded by Wednesday Program 1 due Wednesday, 2pm Follow the assignment submission instructions! 2

3 Questions? 3

4 Today in COMP 110 if/else statements Boolean expressions Review

5 Flow chart Schematic representation of an algorithm Example: going to school in the morning Sunny? Leave home Check weather Walk to school Take the bus Reach school YesNo

6 Flow chart Rectangle: action or statement Arrow: flow of control, next step Diamond: decision, conditional Sunny? Leave home Check weather Walk to school Take the bus Reach school YesNo

7 Flow chart to pseudocode Sunny? Leave home Check weather Walk to school Take the bus Reach school YesNo Leave home Check weather If it is sunny, Walk to school Otherwise Take the bus Reach school

8 Flow chart, pseudocode, if/else Sunny? Leave home Check weather Walk to school Take the bus Reach school YesNo Leave home Check weather if (sunny) walk to school else take the bus Reach school Leave home Check weather If it is sunny walk to school Otherwise take the bus Reach school

9 Branching statement: if/else if (boolean expression) action 1 else action 2 If boolean expression is true, perform action 1, else perform action 2

10 Multiple statements per action if (boolean expression) { action 1 statements //... } else { action 2 statements //... }

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

12 Boolean Expressions When programming, Boolean expressions are often used for comparisons

13 Java Comparison Operators == Equal to != Not equal to > Greater than >= Greater than or equal to < Less than <= Less than or equal to Example expressions: variable <= 6 myInt > 5 5 == 3

14 Java Example Is input greater than 10? YesNo Prompt user for integer Print: “big number” Print: “small number” import java.util.*; public class FlowChart { public static void main(String[] args) { System.out.println("Give me an integer:"); Scanner keyboard = new Scanner(System.in); int inputInt = keyboard.nextInt(); if (inputInt > 10) { System.out.println("big number"); } else { System.out.println("small number"); } } }

15 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

16 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 }

17 boolean type Can be either true or false boolean sunny = true; boolean cloudy = false; if (sunny || cloudy) { // walk to school }

18 The ! (NOT) operator !true is false !false is true Example: walk to school if it is NOT cloudy if (!cloudy) { // walk to school }

19 The ! (NOT) operator if (inputInt > 10) { // walk to school } // is the same as if (!(inputInt <= 10)) { // walk to school }

20 Gotcha: == var1 = var2 (assignment statement) ◦ Error!!!!!!! var1 == var2 (boolean expression) Do NOT use == to compare Strings ◦ string1 == string2 // BAD ◦ string1.equals(string2); // GOOD

21 More than 2 branches What is the size of the chair? sizeOfChair > 10 Prompt user for size of chair Print: “too big” Print: “too small” sizeOfChair < 5 (sizeOfChair >= 5) && (sizeOfChair <= 10) Print: “just right”

22 More than 2 branches if (sizeOfChair > 10) { System.out.println(“Too big”); } else if (sizeOfChair < 5) { System.out.println(“Too small”); } else { System.out.println(“Just right”); }

23 Nested if/else if (sizeOfChair > 10) { if (sizeOfChair > 100) { System.out.println(“REALLY big!”); } else { System.out.println(“Somewhat big”); } else { System.out.println(“small”); }

24 Chapters 1 and 2 Review In-class worksheet

25 Wednesday Program 1 due, 2pm 25


Download ppt "COMP 110 Branching Statements and Boolean Expressions Luv Kohli September 8, 2008 MWF 2-2:50 pm Sitterson 014 1."

Similar presentations


Ads by Google