1 / 26

COMP 110-001 Flow of Control: Branching 1

Learn about the if-else statement, boolean expressions, and the order of actions in a program. Explore branching and simple order concepts using real-life examples.

pringlem
Download Presentation

COMP 110-001 Flow of Control: Branching 1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COMP 110-001Flow of Control: Branching 1 Yi Hong May 19, 2015

  2. Today • The if-else statement • Boolean expressions

  3. Flow of Control • The order in which a program performs actions • Continuation (unconditional) • Until now, actions are taken sequentially • More complicated flow of control: • A branching statement chooses an action from a list of two or more possible actions • A loop statement repeats an action again and again until some stopping condition is met

  4. Simple order Leave Home • Continuation (unconditional) • Perform actions sequentially • A single path in the flow chart Check Time Take Bus Reach School

  5. Branching Leave Home • Choose a path in the flow chart by checking conditions Check Time Before 7am? Yes No Take Bus Take Subway Reach School

  6. Branching Leave Home • Choose a path in the flow chart by checking conditions Check Time Before 7am? Yes No Take Bus Take Subway Reach School

  7. Branching Leave Home • Choose a path in the flow chart by checking conditions Check Time Before 7am? Yes No Take Bus Take Subway Reach School

  8. Another Example Compute #days • In homework 1 Print #days #days==1 Yes No Print “day” Print “days” Compute #hours

  9. The if-else Statement • A branching statement that chooses between two possible actions • Syntax if (Boolean_Expression) Statement_1 else statement_2 Example: if (days == 1) system.out.print(“ day”); else System.out.print(“ days”);

  10. The if-else Statement • What if you have multiple statements in each branch? if (Boolean_Expression){ Statement_1.1 Statement_1.2... } else { Statement_2.1 Statement_2.2 ... }

  11. Java Example importjava.util.*;publicclassFlowChart{publicstaticvoid main(String[] args){System.out.println("Give me an integer:");Scanner keyboard = new Scanner(System.in);intinputInt = keyboard.nextInt();if( inputInt > 5){System.out.println("Big number");}else{System.out.println("Small number");}}} Start Prompt User for int Is user input greater than 5? NO YES Print: “small number” Print: “big number”

  12. Java Comparison Operators

  13. Boolean Expressions • if (boolean_expression) { statements } • True or false • “atomic” expressions 5 == 3 // always false myInt <= 6 // depends on the value of myInt // depends on the value of myInt, anotherInt myInt!= anotherInt

  14. Review of Boolean Operators • The effect of the boolean operators && (and), || (or), and ! (not) on boolean values

  15. Use && for and • Form a larger boolean expression out of many smaller boolean expressions • Syntax • (Sub_Expression_1) && (Sub_Expression_2) && … • Will be true if and only if ALL statements are true

  16. Use || for or • Also form a larger boolean expression out of many boolean expressions • Syntax • (Sub_Expression_1) || (Sub_Expression_2) || … • Will be true if ONE expression is true

  17. Example in Homework 1 • Check if #days is not 1 • days ! = 1 • !(days == 1) • days < 1 || days > 1 • !(days >= 1 && days <= 1)

  18. Comparison for Objects • Call object’s method equals() method • E.g.: Compare two strings • str1 = str2; ( ✗ assignment statement) • str1 == str2; (Do NOT, == tests whether they are stores in the same memory location) • str1.equals(str2); (GOOD)

  19. Compare Strings • Syntax • String.equals(Other_String) • String.equalsIgnoreCase(Other_String) • Example String name1 = “COMP110”; String name 2 = new String(“COMP110”); if (name1.equals(name2)){ System.out.println(“The same”); } else { System.out.println(“Different”); }

  20. If Without Else Wake up • You can use just an if statement if (Boolean_Expression) { Statement_1.1 Statement_1.2... } the rest of your code Check Time Before 7am? Yes No Have Breakfast Leave Home

  21. A Different Implement for Homework 1 Print #days dayText = “ days” Print #days #days==1 #days==1 Yes No Yes No Print “day” Print “days” dayText=“ day” Print dayText

  22. A Different Implement for Homework 1 Print #days System.out.print( days ); String dayText = “ days”; if (days==1){ dayText= “ day”; } System.out.print( dayText ); dayText = “ days” #days==1 Yes No dayText=“ day” Print dayText

  23. Nested if-else Statements • An if-else statement can contain any sort of statements within it • You can nest an if-else statement within another if-else statement if (boolean expression) { … } else if (boolean expression){ stuff goes here } else { more stuff } if (boolean expression) { if (booleanexpression){ stuff goes here } else { more stuff } } else { … }

  24. Pseudocode in Flowchart Format Start Prompt User for int What is the integer? inputInt == 0 inputInt > 1 inputInt == 1 Print: “how may I help you” Print: “hello” Print: “how are you”

  25. importjava.util.*;publicclassFlowChart {publicstaticvoid main(String[] args) {System.out.println("Give me an integer:"); Scanner keyboard = new Scanner(System.in);intinputInt = keyboard.nextInt();if ( inputInt == 0)System.out.println(”hello"); else if ( inputInt == 1) System.out.println(”how are you"); else if (inputInt > 1) System.out.println(”how may I help you"); else System.out.println(“Negative”);} }

  26. Next Class • More if / else statements • The switch statements

More Related