Содержание
- 2. Motivations If you assigned a negative value for radius in ComputeAreaWithConsoleInput.java, the program would print an
- 3. Objectives To declare boolean variables and write Boolean expressions using relational operators (§3.2). To implement selection
- 4. The boolean Type and Operators The boolean data type declares a variable with the value either
- 5. Relational Operators
- 6. Problem: Addition Question AdditionQuiz This example creates a program to let a first grader practice additions.
- 7. if Statements (One Condition) if (boolean-expression) { statement(s); } if (radius >= 0) { area =
- 8. Note
- 9. Simple if Demo SimpleIfDemo Write a program that prompts the user to enter an integer. If
- 10. İf-else Statement (2 conditions) if (boolean-expression) { statement(s)-for-the-true-case; } else { statement(s)-for-the-false-case; } An if-else statement
- 11. if-else Example if (radius >= 0) { area = radius * radius * 3.14159; System.out.println("The area
- 12. if Statements (more than 2 conditions)
- 13. if Statement (more than 2 conditions)
- 14. Trace if-else statement if (score >= 90.0) System.out.print("A"); else if (score >= 80.0) System.out.print("B"); else if
- 15. Trace if-else statement if (score >= 90.0) System.out.print("A"); else if (score >= 80.0) System.out.print("B"); else if
- 16. Trace if-else statement if (score >= 90.0) System.out.print("A"); else if (score >= 80.0) System.out.print("B"); else if
- 17. Trace if-else statement if (score >= 90.0) System.out.print("A"); else if (score >= 80.0) System.out.print("B"); else if
- 18. Trace if-else statement if (score >= 90.0) System.out.print("A"); else if (score >= 80.0) System.out.print("B"); else if
- 19. Note The else clause matches the most recent if clause in the same block.
- 20. Note, cont. Nothing is printed from the preceding statement. To force the else clause to match
- 21. Common Errors Adding a semicolon at the end of an if clause is a common mistake.
- 22. TIP
- 23. CAUTION
- 24. Problem: Subtraction Question This example creates a program to teach a first grade child how to
- 25. Problem: Body Mass Index Body Mass Index (BMI) is a measure of health on weight. It
- 26. Problem: Computing Taxes The US federal personal income tax is calculated based on the filing status
- 27. Problem: Computing Taxes, cont. if (status == 0) { // Compute tax for single filers }
- 28. Logical Operators The logical operators !, &&, ||, and ^ can be used to create a
- 29. Truth Table for Operator !
- 30. Truth Table for Operator &&
- 31. Truth Table for Operator ||
- 32. Truth Table for Operator ^
- 33. Examples Here is a program that checks whether a number is divisible by 2 and 3,
- 34. Examples System.out.println("Is " + number + " divisible by 2 and 3? " + ((number %
- 35. The & and | Operators If x is 1, what is x after this expression? (x
- 36. Problem: Determining Leap Year? LeapYear This program first prompts the user to enter a year as
- 37. Problem: Lottery Write a program that randomly generates a lottery of a two-digit number, prompts the
- 38. switch Statements switch (status) { case 0: compute taxes for single filers; break; case 1: compute
- 39. switch Statement Flow Chart
- 40. switch Statement Rules switch (switch-expression) { case value1: statement(s)1; break; case value2: statement(s)2; break; … case
- 41. switch Statement Rules The keyword break is optional, but it should be used at the end
- 42. Trace switch statement switch (day) { case 1: case 2: case 3: case 4: case 5:
- 43. Trace switch statement switch (day) { case 1: case 2: case 3: case 4: case 5:
- 44. Trace switch statement switch (day) { case 1: case 2: case 3: case 4: case 5:
- 45. Trace switch statement switch (day) { case 1: case 2: case 3: case 4: case 5:
- 46. Trace switch statement switch (day) { case 1: case 2: case 3: case 4: case 5:
- 47. Trace switch statement switch (day) { case 1: case 2: case 3: case 4: case 5:
- 48. Trace switch statement switch (day) { case 1: case 2: case 3: case 4: case 5:
- 49. Problem: Chinese Zodiac Write a program that prompts the user to enter a year and displays
- 50. Conditional Operator (?) Syntax of conditional operator: (boolean-expression) ? exp1 : exp2 if (num % 2
- 51. Conditional Expressions if (x > 0) y = 1 else y = -1; is equivalent to
- 52. Operator Precedence var++, var-- +, - (Unary plus and minus), ++var,--var (type) Casting ! (Not) *,
- 53. Operator Precedence and Associativity The expression in the parentheses is evaluated first. (Parentheses can be nested,
- 54. Operator Associativity When two operators with the same precedence are evaluated, the associativity of the operators
- 55. Example Applying the operator precedence and associativity rule, the expression 3 + 4 * 4 >
- 56. Debugging The process of finding and correcting errors is called debugging. A common approach to debugging
- 57. Debugger Debugger is a program that facilitates debugging. You can use a debugger to Execute a
- 59. Скачать презентацию