1 / 37

Chapter 3

Chapter 3. Selection in Java. b oolean. The boolean data type has only two possible values: true and false . Declaration: boolean varname ; Boolean operations generally compare values and determine whether the asserted relationship is true or false.

xuxa
Télécharger la présentation

Chapter 3

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. Chapter 3 Selection in Java

  2. boolean • The boolean data type has only two possible values: true and false. • Declaration: booleanvarname; • Boolean operations generally compare values and determine whether the asserted relationship is true or false. • Boolean operations are stricter than C/C++ where false is zero and true is non-zero!

  3. Boolean operators • < // 4 < 5 is true, 5 < 4 is false • <= • == // 4 == 2+2 is true, 5 == 3+3 is false • > // 3 > 1 is true, 2 > 4 is false • >= • != // ‘A’ != ‘Z’ is true, 0 != 4-3+1 is false

  4. The if statement

  5. The if statement • Keyword is lower case • Boolean expression inside parentheses () • Can be followed by a single statement or a block of statements enclosed in curly braces {} • The statement or block will only be executed if the booleanexpression evaluates to true • Do not put a ‘;’ after the boolean expression

  6. The else statement • The else statement is processed if the boolean expression evaluates to false. • It can be a single statement or a block of statements enclose in curly braces {} • Be careful about which if statement an else statement matches with in nested if statements

  7. The else in action

  8. Logical operators • ! // “not” !true == false, !false == true • && // “and” x && y is true only if both x and y are true, otherwise it is false • || // “or” x || y is true if either x or y is true, it can only be false if both are false • ^ // “exclusive or” x ^ y is true if x and y are opposite values. If both are true or both are false, it is false.

  9. Combining boolean expressions:The case of an historic software error

  10. The switch and case statements • The switch(expression) uses the value of the expression to select among choices represented by the values associated with the list of case value statements • The list of statements after the selected case statement is executed until a break statement is encountered • The default statement is executed if no case value matches the expression

  11. The conditional ? : expression • ( test-exp)? true-exp: false-exp • The test expression is enclosed with () • If the test expression is true, the expression after the ‘?’ is returned • If the test expression is false, the expression after the ‘:’ is returned • Short for if(test-exp) true-expelse false-exp

  12. Using the ? : conditional statement

  13. The printf method • System.out.printf(format, item1, item2, ….); • format is a string expression with embedded specifiers that control formatting • Specifiers consist of ‘%’ with formatting codes • Each item must have a matching specifier that is compatible with the item type • Each item specifier must have a corresponding item in the list

  14. The printf() specifiers • %b // boolean • %c // character • %d // decimal integer • %f // floating point number • %e // number in scientific notation • %s // string • %% // print the ‘%’ character

  15. Examples of formatted output

  16. Tips for printf() • Works like fprintf() in C/C++ language • Escape characters such as /n, /”, etc. work • Format string can be a string expression composed of concatenated expressions and method calls • Items can be expressions that evaluate to the correct type for the print specifier • Specifiers must match items in order

  17. Padding and field sizing • For all specifiers, a number after the ‘%’ specifies the width of the field; for example, %8c will print a character with 7 spaces padding in front • %8.3e specifies a total field width of 8 spaces, including the ‘.’ and ‘e’ with 3 digits to the right of the decimal place.

  18. Operator Precedence (highest to lowest) • var++, var— • ++var, --var, !, (typecast) , unary +, unary – • *, /, % • +, - • <, <=, >, >= • ==, != • ^ • && • || • ? : • =, +=, -=, *=, /=, %=

  19. http://www.cis.upenn.edu/~palsetia/java/precedenceTable.html

  20. Confirmation Dialogs

  21. Output Dialog

  22. Chapter 4 Looping Constructs

  23. Comparing looping constructs • Java has while() {} and do {} while();loops • The do {} while();loop executes at least once before testing the exit condition • The while() {} loop may not execute at all if the exit condition is already met at the start

  24. Break; will exit the inner-most loop immediately and unconditionally • Continue; will jump to the exit condition test and begin a new iteration if it is still true

  25. What will be the output if the input value is 11?

  26. What will be the output if the input value is 11?

  27. The for loop • Syntax is just like C/C++ for loop format: for (initialization ; exit test; post loop action) • Initialization can be multiple comma-separated assignments. • Exit test is a single boolean statement. If it is false to begin with, the for loop does not run. • Post loop action can be multiple comma-separated statements.

  28. Typical for loop

  29. Complicated for loop

  30. for loop <-> while loop equivalence

  31. Sum of 0.01, 0.02, … 0.99, 1.0

  32. Can double do better?

More Related