1 / 19

CSC 1051 – Algorithms and Data Structures I

CSC 1051 – Algorithms and Data Structures I. Conditional Statements. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051 /f13/

kin
Download Presentation

CSC 1051 – Algorithms and Data Structures I

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. CSC 1051 – Algorithms and Data Structures I Conditional Statements Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/f13/ Some slides in this presentation are adapted from the slides accompanying Java Software Solutions by Lewis & Loftus CSC 1051 M.A. Papalaskari, Villanova University

  2. Review Conditional statementsalter the linear flow of control. They use boolean expressions to determine what to do next.Example: if (credits == 0) System.out.println("GPA: None"); else {gpa = qp / credits;System.out.println ("\n\tGPA: " + gpa); } A boolean expression if true, do this if false, do these if more than one statement, enclose in braces CSC 1051 M.A. Papalaskari, Villanova University

  3. false false condition evaluated condition evaluated true true statement 1 statement2 statement Conditional statements if ( condition) statement; // no else clause if ( condition) statement1; else statement2; CSC 1051 M.A. Papalaskari, Villanova University

  4. Another example: Create an application called Vacation that prompts for and inputs an integer representing someone’s age and then suggests an appropriate vacation destination. One of three destinations should be suggested depending on whether the answer is less than 20, between 20 and 50, or over 50. For example, a run of the program might look like this: How old is the traveler > 15 You should consider Hershey Park. CSC 1051 M.A. Papalaskari, Villanova University

  5. Review Boolean Expressions • The reserved wordstrueandfalseare the only valid values for a boolean type • Example booleandeclarations: booleanaboveAgeLimit = false; booleanusePlural = hours > 1; A boolean expression using a relational operator CSC 1051 M.A. Papalaskari, Villanova University

  6. Review Java relational operators • relational operators can be used with numeric types and producebooleanresults: ==equal to !=not equal to <less than >greater than <=less than or equal to >=greater than or equal to • Note the difference between the equality operator (==) and the assignment operator (=) CSC 1051 M.A. Papalaskari, Villanova University

  7. Java Logical Operators • logical operators can be used with boolean operands to express more complexbooleanconditions: !Logical NOT &&Logical AND ||Logical OR CSC 1051 M.A. Papalaskari, Villanova University

  8. Example 103 total if (total < MAX+5 && !found) System.out.println("Processing…"); 100 MAX false found • All logical operators have lower precedence than the relational operators • The ! operator has higher precedence than && and || CSC 1051 M.A. Papalaskari, Villanova University

  9. Logical NOT • The logical NOT operation is also called logical negation or logical complement • If some boolean condition a is true, then !a is false; if a is false, then !a is true • Logical expressions can be shown using a truth table: CSC 1051 M.A. Papalaskari, Villanova University

  10. Logical AND and Logical OR • The logical AND expression a && b is true if both a and b are true, and false otherwise • The logical OR expression a || b is true if a or b or both are true, and false otherwise CSC 1051 M.A. Papalaskari, Villanova University

  11. Logical AND and Logical OR • A truth table shows all possible true-false combinations of the terms • Since && and || each have two operands, there are four possible combinations of conditions a and b CSC 1051 M.A. Papalaskari, Villanova University

  12. Quick Check 1 20 total What do the following statements do? 8 stock if (total != stock + warehouse) inventoryError = true; 12 warehouse false inventoryerror if (found || !done) System.out.println("Ok"); false found true done CSC 1051 M.A. Papalaskari, Villanova University

  13. Quick Check 2 20 total Try again with different values 7 stock if (total != stock + warehouse) inventoryError = true; 12 warehouse true inventoryerror if (found || !done) System.out.println("Ok"); false found false done CSC 1051 M.A. Papalaskari, Villanova University

  14. Quick Check 3 20 total Try again with different values 8 stock if (total != stock + warehouse) inventoryError = true; 12 warehouse true inventoryerror if (found || !done) System.out.println("Ok"); true found false done CSC 1051 M.A. Papalaskari, Villanova University

  15. Boolean Expressions • using truth tables – let’s try this one: CSC 1051 M.A. Papalaskari, Villanova University

  16. Boolean Expressions • using truth tables – another example: CSC 1051 M.A. Papalaskari, Villanova University

  17. How much of a boolean expression do we need to evaluate before determining its value?*** Short-Circuited Operators • The processing of && and || is “short-circuited” in cases where the left operand is sufficient to determine the result (the right operand is not evaluated at all) • This can be both useful and dangerous! if (count != 0 && total/count > MAX) System.out.println ("Testing."); CSC 1051 M.A. Papalaskari, Villanova University

  18. Indentation Revisited • Remember that indentation is for the human reader, and is ignored by the computer if (total > MAX) System.out.println ("Error!!"); errorCount = errorcount + 1;; Despite what is implied by the indentation, the increment will occur whether the condition is true or not CSC 1051 M.A. Papalaskari, Villanova University

  19. More examples in textbook, Section 5.2 • Age.java • Wages.java • Guessing.java • MinOfThree.java CSC 1051 M.A. Papalaskari, Villanova University

More Related