csci 144 introduction to
play

CSCI 144 - Introduction to Computer Science Instructor: John - PowerPoint PPT Presentation

1 CSCI 144 - Introduction to Computer Science Instructor: John Goettsche Computer Science Department Pacific Lutheran University Spring 2018 More Boolean Expressions and If Statements Flags Flags boolean onGreen = false; Monitor some


  1. 1 CSCI 144 - Introduction to Computer Science Instructor: John Goettsche Computer Science Department Pacific Lutheran University Spring 2018

  2. More Boolean Expressions and If Statements

  3. Flags

  4. Flags boolean onGreen = false; Monitor some condition Boolean variable if (distanceToHole < 30) onGreen = true; Can be set And later tested if (onGreen) System.out.println("Get out your putter!");

  5. Comparing Characters

  6. Comparing Characters < > <= >= == != Characters can be compared using relational operators char ch = ‘C’; 0000000001000011 Unicode is stored as a 16 bit number

  7. Comparing Characters < > <= >= == != Characters can be compared using relational operators char ch = ‘C’; 0000000001000011 Unicode is stored as a 16 bit number ‘0’ … ‘9’ ‘?’ ‘A’ … ‘Z’ ‘a’ … ‘z’ Characters are ordinal -- they have an order char c = ′A′; if(c < ′Z′) System.out.println("A is less than Z");

  8. Character Unicode Character Unicode 'A' 'a' 65 97 'B' 'b' 66 98 'C' 'c' 67 99 'D' 'd' 68 100 ... ... ... ... 'Z' 'z' 90 122

  9. Comparing Strings

  10. String name1, name2; name1 name2 “Sally” “Sally” if (name1 == name2) System.out.println(“The names are equal”); Can’t compare strings with ==

  11. String name1, name2; name1 name2 “Sally” “Sally” if (name1.equals(name2)) System.out.println (“The names are equal”); Must use equals method

  12. String name1, name2; name1 name2 “Sally” “SALLY” if (name1.equalsIgnoreCase(name2)) System.out.println (“The names are equal”);

  13. Logical Operators

  14. Logical Operators Operator Meaning Effect Both expressions must be true for the overall && AND expression to be true. One or both expressions must be true for the overall || OR expression to be true. The ! operator reverses the truth of a boolean ! NOT expression.

  15. AND && Expression 1 Expression 2 Expression1 && Expression2 true false false true false false true true

  16. OR || Expression 1 Expression 2 Expression1 || Expression2 true false false true false false true true

  17. NOT ! Expression 1 !Expression1 true false

  18. Precedence

  19. Order of Operators Description Precedence (unary negation) ! Unary negation, logical NOT 1 * / % 2 Multiplication, Division, Modulus + - 3 Addition, Subtraction Less-than, Greater-than, Less-than or < > <= >= 4 equal to, Greater-than or equal to == != 5 Is equal to, Is not equal to && 6 Logical AND || 7 Logical OR

  20. Practice

  21. Logical Operators Evaluate the following expressions given these values for Boolean variables x, y, and z: boolean x = true, y = false, z = true; Expression Result x && y || x && z __________ (x || !y) && (!x || z) __________ x || y && z __________ !(x || y) && z __________

  22. Boolean Expressions Evaluate the following expressions given these variables: int count = 0, limit = 10, x = 5, y = 7; char midtermGrade = ‘A’; Expression Result count == 0 && limit < 20 __________ limit > 20 || count < 5 __________ !(midtermGrade != ‘C’) __________ (count < 10) || (x < y) __________

  23. Boolean Expressions (cont.) Evaluate the following expressions given these variables: int count = 0, limit = 10, x = 5, y = 7; char midtermGrade = ‘A’; Expression Result !(((count < 10) || (x < y)) && (count >= 0)) __________ ((limit/count) > 7) || (limit < 20) __________ ((midtermGrade == ‘A’) || ((limit/count) < 20) __________ (limit < 0) && ((limit/count) > 7) __________

  24. Write an if statement to calculate tax (totalSales times TAXRATE) if the user input forProfit is ‘Y’ or ‘y’. if (forProfit == ‘Y’ || forProfit == ‘y’ ) { tax = totalsales * TAXRATE; }

  25. Write an if statement to calculate tax (totalSales times TAXRATE) if the user input response is “yes” (be sure to ignore case). if (response.equalsIgnoreCase( “yes” )) { tax = totalsales * TAXRATE; }

  26. Write an JAVA program, scan user’s input as temperature and mood, write if statement to print “Play golf!” if the temperature is between 65 and 80 , and mood is “happy”. For all other cases, print ‘Programming at home’. (hint: consider ‘flags’)

  27. Reference: Tables and flowcharts adapted from Starting Out with Java by T. Gaddis Chapter 3 Slides.

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend