0
Java Programming: From Problem Analysis to Program Design, 5e Chapter 4 Control Structures I: Selection
Chapter Objectives <ul><li>Learn about control structures </li></ul><ul><li>Examine relational and logical operators </li>...
Control Structures <ul><li>Three methods of processing a program </li></ul><ul><ul><li>In sequence </li></ul></ul><ul><ul>...
Flow of Execution Java Programming: From Problem Analysis to Program Design, 5e
Relational Operators <ul><li>Relational operator </li></ul><ul><ul><li>Allows you to make comparisons in a program </li></...
Relational Operators in Java Java Programming: From Problem Analysis to Program Design, 5e
Relational Operators and Primitive Data Types <ul><li>Can be used with integral and floating-point data types </li></ul><u...
Java Programming: From Problem Analysis to Program Design, 5e
Java Programming: From Problem Analysis to Program Design, 5e
Relational Operators and the Unicode Collating Sequence Java Programming: From Problem Analysis to Program Design, 5e
Logical (Boolean) Operators Java Programming: From Problem Analysis to Program Design, 5e
Logical (Boolean) Operators (continued) Java Programming: From Problem Analysis to Program Design, 5e
Logical (Boolean) Operators (continued) Java Programming: From Problem Analysis to Program Design, 5e
Logical (Boolean) Operators (continued) Java Programming: From Problem Analysis to Program Design, 5e
Precedence of Operators Java Programming: From Problem Analysis to Program Design, 5e
Precedence of Operators (continued) Java Programming: From Problem Analysis to Program Design, 5e
Java Programming: From Problem Analysis to Program Design, 5e
Java Programming: From Problem Analysis to Program Design, 5e
Java Programming: From Problem Analysis to Program Design, 5e
Selection <ul><li>One-way selection </li></ul><ul><li>Two-way selection </li></ul><ul><li>Compound (block of) statements <...
One-Way Selection <ul><li>Syntax </li></ul><ul><li>if  (expression) </li></ul><ul><li>  statement </li></ul><ul><li>Expres...
One-Way Selection (continued) Java Programming: From Problem Analysis to Program Design, 5e
<ul><li>Example 4-7 </li></ul><ul><li>//Program to determine the absolute value of an integer </li></ul><ul><li>import  ja...
<ul><li>if  (number < 0)    //Line 4 </li></ul><ul><li>number = -number;    //Line 5 </li></ul><ul><li>JOptionPane.showMes...
<ul><li>if  (number < 0)    //Line 4 </li></ul><ul><li>number = -number;    //Line 5 </li></ul><ul><li>JOptionPane.showMes...
Two-Way Selection <ul><li>Syntax </li></ul><ul><li>if  (expression) </li></ul><ul><li>  statement1 </li></ul><ul><li>else ...
Two-Way Selection (continued) Java Programming: From Problem Analysis to Program Design, 5e
Two-Way Selection (continued) Java Programming: From Problem Analysis to Program Design, 5e Example 4-10 if  (hours > 40.0...
Two-Way Selection (continued) Java Programming: From Problem Analysis to Program Design, 5e <ul><li>Example 4-11 </li></ul...
Two-Way Selection (continued) Java Programming: From Problem Analysis to Program Design, 5e
Compound (Block of) Statements <ul><li>Syntax </li></ul>Java Programming: From Problem Analysis to Program Design, 5e
Compound (Block of) Statements (continued) <ul><li>if  (age > 18) </li></ul><ul><li>{ </li></ul><ul><li>System.out.println...
Multiple Selection: Nested if <ul><li>Syntax </li></ul><ul><li>if  (expression1) </li></ul><ul><li>  statement1 </li></ul>...
Multiple Selection: Nested if (continued) Java Programming: From Problem Analysis to Program Design, 5e
Multiple Selection: Nested if (continued) Java Programming: From Problem Analysis to Program Design, 5e To avoid excessive...
Multiple Selection: Nested if (continued) Java Programming: From Problem Analysis to Program Design, 5e
Multiple Selection: Nested if (continued) Java Programming: From Problem Analysis to Program Design, 5e
Java Programming: From Problem Analysis to Program Design, 5e
Java Programming: From Problem Analysis to Program Design, 5e
<ul><li>Definition:  a process in which the computer evaluates a logical expression from left to right and stops as soon a...
Short-Circuit Evaluation (continued) Java Programming: From Problem Analysis to Program Design, 5e
Java Programming: From Problem Analysis to Program Design, 5e
Java Programming: From Problem Analysis to Program Design, 5e
Java Programming: From Problem Analysis to Program Design, 5e
Java Programming: From Problem Analysis to Program Design, 5e <ul><li>The preceding program and its output show that you s...
Conditional (? :) Operator <ul><li>Ternary operator </li></ul><ul><li>Syntax </li></ul><ul><li>expression1 ?  expression2 ...
Java Programming: From Problem Analysis to Program Design, 5e
Java Programming: From Problem Analysis to Program Design, 5e
Java Programming: From Problem Analysis to Program Design, 5e
Java Programming: From Problem Analysis to Program Design, 5e
switch  Structures Java Programming: From Problem Analysis to Program Design, 5e
switch  Structures (continued) Java Programming: From Problem Analysis to Program Design, 5e <ul><li>In Java,  switch ,  c...
switch  Structures (continued) Java Programming: From Problem Analysis to Program Design, 5e <ul><li>The expression is som...
switch  Structures (continued) Java Programming: From Problem Analysis to Program Design, 5e <ul><li>The  break  statement...
switch  Structures (continued) Java Programming: From Problem Analysis to Program Design, 5e
switch  Structures (continued) Java Programming: From Problem Analysis to Program Design, 5e
<ul><li>Example 4-20 </li></ul><ul><li>switch  (grade) </li></ul><ul><li>{ </li></ul><ul><li>case  'A':  </li></ul><ul><li...
<ul><li>case  'D':  </li></ul><ul><li>System.out.println(&quot;The grade is D.&quot;); </li></ul><ul><li>break ; </li></ul...
Java Programming: From Problem Analysis to Program Design, 5e
Java Programming: From Problem Analysis to Program Design, 5e
Java Programming: From Problem Analysis to Program Design, 5e
Java Programming: From Problem Analysis to Program Design, 5e To output results correctly, the  switch  structure must inc...
Programming Example:  Cable Company Billing <ul><li>Input: customer’s account number, customer code, number of premium cha...
Programming Example:  Cable Company Billing (continued) <ul><li>Solution  </li></ul><ul><ul><li>Prompt user for informatio...
Comparing Strings <ul><li>class   String   </li></ul><ul><ul><li>Method  compareTo </li></ul></ul><ul><ul><li>Method  equa...
Comparing Strings (continued) <ul><li>String str1 = &quot;Hello&quot;; </li></ul><ul><li>String str2 = &quot;Hi&quot;; </l...
Comparing Strings (continued) Java Programming: From Problem Analysis to Program Design, 5e
Comparing Strings (continued) Java Programming: From Problem Analysis to Program Design, 5e
Chapter Summary <ul><li>Control structures are used to process programs </li></ul><ul><li>Logical expressions and order of...
Upcoming SlideShare
Loading in...5
×

9781111530532 ppt ch04

518

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
518
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
23
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "9781111530532 ppt ch04"

  1. 1. Java Programming: From Problem Analysis to Program Design, 5e Chapter 4 Control Structures I: Selection
  2. 2. Chapter Objectives <ul><li>Learn about control structures </li></ul><ul><li>Examine relational and logical operators </li></ul><ul><li>Explore how to form and evaluate logical (Boolean) expressions </li></ul><ul><li>Learn how to use the selection control structures if , if … else , and switch in a program </li></ul>Java Programming: From Problem Analysis to Program Design, 5e
  3. 3. Control Structures <ul><li>Three methods of processing a program </li></ul><ul><ul><li>In sequence </li></ul></ul><ul><ul><li>Branching </li></ul></ul><ul><ul><li>Looping </li></ul></ul><ul><li>Branch: altering the flow of program execution by making a selection or choice </li></ul><ul><li>Loop: altering the flow of program execution by repetition of statement(s) </li></ul>Java Programming: From Problem Analysis to Program Design, 5e
  4. 4. Flow of Execution Java Programming: From Problem Analysis to Program Design, 5e
  5. 5. Relational Operators <ul><li>Relational operator </li></ul><ul><ul><li>Allows you to make comparisons in a program </li></ul></ul><ul><ul><li>Binary operator </li></ul></ul><ul><li>Condition is represented by a logical expression in Java </li></ul><ul><li>Logical expression: expression that has a value of either true or false </li></ul>Java Programming: From Problem Analysis to Program Design, 5e
  6. 6. Relational Operators in Java Java Programming: From Problem Analysis to Program Design, 5e
  7. 7. Relational Operators and Primitive Data Types <ul><li>Can be used with integral and floating-point data types </li></ul><ul><li>Can be used with the char data type </li></ul><ul><li>Unicode collating sequence </li></ul>Java Programming: From Problem Analysis to Program Design, 5e
  8. 8. Java Programming: From Problem Analysis to Program Design, 5e
  9. 9. Java Programming: From Problem Analysis to Program Design, 5e
  10. 10. Relational Operators and the Unicode Collating Sequence Java Programming: From Problem Analysis to Program Design, 5e
  11. 11. Logical (Boolean) Operators Java Programming: From Problem Analysis to Program Design, 5e
  12. 12. Logical (Boolean) Operators (continued) Java Programming: From Problem Analysis to Program Design, 5e
  13. 13. Logical (Boolean) Operators (continued) Java Programming: From Problem Analysis to Program Design, 5e
  14. 14. Logical (Boolean) Operators (continued) Java Programming: From Problem Analysis to Program Design, 5e
  15. 15. Precedence of Operators Java Programming: From Problem Analysis to Program Design, 5e
  16. 16. Precedence of Operators (continued) Java Programming: From Problem Analysis to Program Design, 5e
  17. 17. Java Programming: From Problem Analysis to Program Design, 5e
  18. 18. Java Programming: From Problem Analysis to Program Design, 5e
  19. 19. Java Programming: From Problem Analysis to Program Design, 5e
  20. 20. Selection <ul><li>One-way selection </li></ul><ul><li>Two-way selection </li></ul><ul><li>Compound (block of) statements </li></ul><ul><li>Multiple selections (nested if) </li></ul><ul><li>Conditional operator </li></ul><ul><li>switch structures </li></ul>Java Programming: From Problem Analysis to Program Design, 5e
  21. 21. One-Way Selection <ul><li>Syntax </li></ul><ul><li>if (expression) </li></ul><ul><li> statement </li></ul><ul><li>Expression referred to as decision maker </li></ul><ul><li>Statement referred to as action statement </li></ul>Java Programming: From Problem Analysis to Program Design, 5e
  22. 22. One-Way Selection (continued) Java Programming: From Problem Analysis to Program Design, 5e
  23. 23. <ul><li>Example 4-7 </li></ul><ul><li>//Program to determine the absolute value of an integer </li></ul><ul><li>import javax.swing.JOptionPane; </li></ul><ul><li>public class AbsoluteValue </li></ul><ul><li>{ </li></ul><ul><li>public static void main(String[] args) </li></ul><ul><li>{ </li></ul><ul><li>int number; </li></ul><ul><li>int temp; </li></ul><ul><li>String numString; </li></ul><ul><li>numString = </li></ul><ul><li>JOptionPane.showInputDialog </li></ul><ul><li>(&quot;Enter an integer:&quot;); //Line 1 </li></ul><ul><li>number = Integer.parseInt(numString); //Line 2 </li></ul><ul><li>temp = number; //Line 3 </li></ul>One-Way Selection (continued) Java Programming: From Problem Analysis to Program Design, 5e
  24. 24. <ul><li>if (number < 0) //Line 4 </li></ul><ul><li>number = -number; //Line 5 </li></ul><ul><li>JOptionPane.showMessageDialog(null, </li></ul><ul><li>&quot;The absolute value of &quot; + temp </li></ul><ul><li>+ &quot; is &quot; + number, </li></ul><ul><li>&quot;Absolute Value&quot;, </li></ul><ul><li>JOptionPane.INFORMATION_MESSAGE); //Line 6 </li></ul><ul><li>System.exit(0); </li></ul><ul><li>} </li></ul>One-Way Selection (continued) Java Programming: From Problem Analysis to Program Design, 5e
  25. 25. <ul><li>if (number < 0) //Line 4 </li></ul><ul><li>number = -number; //Line 5 </li></ul><ul><li>JOptionPane.showMessageDialog(null, </li></ul><ul><li>&quot;The absolute value of &quot; + temp </li></ul><ul><li>+ &quot; is &quot; + number, </li></ul><ul><li>&quot;Absolute Value&quot;, </li></ul><ul><li>JOptionPane.INFORMATION_MESSAGE); //Line 6 </li></ul><ul><li>System.exit(0); </li></ul><ul><li>} </li></ul>Java Programming: From Problem Analysis to Program Design, 5e
  26. 26. Two-Way Selection <ul><li>Syntax </li></ul><ul><li>if (expression) </li></ul><ul><li> statement1 </li></ul><ul><li>else </li></ul><ul><li> statement2 </li></ul><ul><li>else statement must be paired with an if </li></ul>Java Programming: From Problem Analysis to Program Design, 5e
  27. 27. Two-Way Selection (continued) Java Programming: From Problem Analysis to Program Design, 5e
  28. 28. Two-Way Selection (continued) Java Programming: From Problem Analysis to Program Design, 5e Example 4-10 if (hours > 40.0) wages = 40.0 * rate + 1.5 * rate * (hours - 40.0); else wages = hours * rate;
  29. 29. Two-Way Selection (continued) Java Programming: From Problem Analysis to Program Design, 5e <ul><li>Example 4-11 </li></ul><ul><li>if (hours > 40.0); //Line 1 </li></ul><ul><li>wages = 40.0 * rate + </li></ul><ul><li>1.5 * rate * (hours - 40.0); //Line 2 </li></ul><ul><li>else //Line 3 </li></ul><ul><li>wages = hours * rate; //Line 4 </li></ul><ul><li>Because a semicolon follows the closing parenthesis of the if statement (Line 1), the else statement stands alone </li></ul><ul><li>The semicolon at the end of the if statement (see Line 1) ends the if statement, so the statement at Line 2 separates the else clause from the if statement; that is, else is by itself </li></ul><ul><li>Since there is no separate else statement in Java, this code generates a syntax error </li></ul>
  30. 30. Two-Way Selection (continued) Java Programming: From Problem Analysis to Program Design, 5e
  31. 31. Compound (Block of) Statements <ul><li>Syntax </li></ul>Java Programming: From Problem Analysis to Program Design, 5e
  32. 32. Compound (Block of) Statements (continued) <ul><li>if (age > 18) </li></ul><ul><li>{ </li></ul><ul><li>System.out.println(&quot;Eligible to vote.&quot;); </li></ul><ul><li>System.out.println(&quot;No longer a minor.&quot;); </li></ul><ul><li>} </li></ul><ul><li>else </li></ul><ul><li>{ </li></ul><ul><li>System.out.println(&quot;Not eligible to vote.&quot;); </li></ul><ul><li>System.out.println(&quot;Still a minor.&quot;); </li></ul><ul><li>} </li></ul>Java Programming: From Problem Analysis to Program Design, 5e
  33. 33. Multiple Selection: Nested if <ul><li>Syntax </li></ul><ul><li>if (expression1) </li></ul><ul><li> statement1 </li></ul><ul><li>else if (expression2) </li></ul><ul><li> statement2 </li></ul><ul><li>else </li></ul><ul><li> statement3 </li></ul><ul><li>Else associated with most recent incomplete if </li></ul><ul><li>Multiple if statements can be used in place of if…else statements </li></ul><ul><li>May take longer to evaluate </li></ul>Java Programming: From Problem Analysis to Program Design, 5e
  34. 34. Multiple Selection: Nested if (continued) Java Programming: From Problem Analysis to Program Design, 5e
  35. 35. Multiple Selection: Nested if (continued) Java Programming: From Problem Analysis to Program Design, 5e To avoid excessive indentation, the code in Example 4-15 can be rewritten as follows:
  36. 36. Multiple Selection: Nested if (continued) Java Programming: From Problem Analysis to Program Design, 5e
  37. 37. Multiple Selection: Nested if (continued) Java Programming: From Problem Analysis to Program Design, 5e
  38. 38. Java Programming: From Problem Analysis to Program Design, 5e
  39. 39. Java Programming: From Problem Analysis to Program Design, 5e
  40. 40. <ul><li>Definition: a process in which the computer evaluates a logical expression from left to right and stops as soon as the value of the expression is known </li></ul>Short-Circuit Evaluation Java Programming: From Problem Analysis to Program Design, 5e
  41. 41. Short-Circuit Evaluation (continued) Java Programming: From Problem Analysis to Program Design, 5e
  42. 42. Java Programming: From Problem Analysis to Program Design, 5e
  43. 43. Java Programming: From Problem Analysis to Program Design, 5e
  44. 44. Java Programming: From Problem Analysis to Program Design, 5e
  45. 45. Java Programming: From Problem Analysis to Program Design, 5e <ul><li>The preceding program and its output show that you should be careful when comparing floating-point numbers for equality. </li></ul><ul><li>One way to check whether two floating-point numbers are equal is to check whether the absolute value of their difference is less than a certain tolerance. For example, suppose the tolerance is .000001 . </li></ul><ul><li>Then x and y are equal if the absolute value of (x – y) is less than 0.000001 . To find the absolute value, you can use the function Math.abs of the class Math , as shown in the program. </li></ul><ul><li>Therefore, the expression Math.abs(x – y) < 0.000001 determines whether the absolute value of (x – y) is less than 0.000001. </li></ul>
  46. 46. Conditional (? :) Operator <ul><li>Ternary operator </li></ul><ul><li>Syntax </li></ul><ul><li>expression1 ? expression2 : expression3 </li></ul><ul><li>If expression1 = true , then the result of the condition is expression 2; otherwise, the result of the condition is expression 3 </li></ul>Java Programming: From Problem Analysis to Program Design, 5e
  47. 47. Java Programming: From Problem Analysis to Program Design, 5e
  48. 48. Java Programming: From Problem Analysis to Program Design, 5e
  49. 49. Java Programming: From Problem Analysis to Program Design, 5e
  50. 50. Java Programming: From Problem Analysis to Program Design, 5e
  51. 51. switch Structures Java Programming: From Problem Analysis to Program Design, 5e
  52. 52. switch Structures (continued) Java Programming: From Problem Analysis to Program Design, 5e <ul><li>In Java, switch , case , break , and default are reserved words </li></ul><ul><li>In a switch structure, the expression is evaluated first </li></ul><ul><li>The value of the expression is then used to perform the actions specified in the statements that follow the reserved word case </li></ul><ul><li>The expression is usually an identifier </li></ul><ul><li>The value of the identifier or the expression can be only of type int , byte , short , or char </li></ul>
  53. 53. switch Structures (continued) Java Programming: From Problem Analysis to Program Design, 5e <ul><li>The expression is sometimes called the selector; its value determines which statements are selected for execution </li></ul><ul><li>A particular case value must appear only once </li></ul><ul><li>One or more statements may follow a case label, so you do not need to use braces to turn multiple statements into a single compound statement </li></ul>
  54. 54. switch Structures (continued) Java Programming: From Problem Analysis to Program Design, 5e <ul><li>The break statement may or may not appear after each statements1 , statements2 , ..., statementsn </li></ul><ul><li>A switch structure may or may not have the default label </li></ul>
  55. 55. switch Structures (continued) Java Programming: From Problem Analysis to Program Design, 5e
  56. 56. switch Structures (continued) Java Programming: From Problem Analysis to Program Design, 5e
  57. 57. <ul><li>Example 4-20 </li></ul><ul><li>switch (grade) </li></ul><ul><li>{ </li></ul><ul><li>case 'A': </li></ul><ul><li>System.out.println(&quot;The grade is A.&quot;); </li></ul><ul><li>break ; </li></ul><ul><li>case 'B': </li></ul><ul><li>System.out.println(&quot;The grade is B.&quot;); </li></ul><ul><li>break ; </li></ul><ul><li>case 'C': </li></ul><ul><li>System.out.println(&quot;The grade is C.&quot;); </li></ul><ul><li>break ; </li></ul>switch Structures (continued) Java Programming: From Problem Analysis to Program Design, 5e
  58. 58. <ul><li>case 'D': </li></ul><ul><li>System.out.println(&quot;The grade is D.&quot;); </li></ul><ul><li>break ; </li></ul><ul><li>case 'F': </li></ul><ul><li>System.out.println(&quot;The grade is F.&quot;); </li></ul><ul><li>break ; </li></ul><ul><li>default : </li></ul><ul><li>System.out.println(&quot;The grade is invalid.&quot;); </li></ul><ul><li>} </li></ul>switch Structures (continued) Java Programming: From Problem Analysis to Program Design, 5e
  59. 59. Java Programming: From Problem Analysis to Program Design, 5e
  60. 60. Java Programming: From Problem Analysis to Program Design, 5e
  61. 61. Java Programming: From Problem Analysis to Program Design, 5e
  62. 62. Java Programming: From Problem Analysis to Program Design, 5e To output results correctly, the switch structure must include a break statement after each println statement, except the last println statement.
  63. 63. Programming Example: Cable Company Billing <ul><li>Input: customer’s account number, customer code, number of premium channels to which customer subscribes, number of basic service connections (in case of business customers) </li></ul><ul><li>Output: customer’s account number and the billing amount </li></ul>Java Programming: From Problem Analysis to Program Design, 5e
  64. 64. Programming Example: Cable Company Billing (continued) <ul><li>Solution </li></ul><ul><ul><li>Prompt user for information </li></ul></ul><ul><ul><li>Use switch statements based on customer’s type </li></ul></ul><ul><ul><li>Use an if statement nested within a switch statement to determine amount due by each customer </li></ul></ul>Java Programming: From Problem Analysis to Program Design, 5e
  65. 65. Comparing Strings <ul><li>class String </li></ul><ul><ul><li>Method compareTo </li></ul></ul><ul><ul><li>Method equals </li></ul></ul><ul><li>Given string str1 and str2 </li></ul>Java Programming: From Problem Analysis to Program Design, 5e
  66. 66. Comparing Strings (continued) <ul><li>String str1 = &quot;Hello&quot;; </li></ul><ul><li>String str2 = &quot;Hi&quot;; </li></ul><ul><li>String str3 = &quot;Air&quot;; </li></ul><ul><li>String str4 = &quot;Bill&quot;; </li></ul><ul><li>String str5 = &quot;Bigger&quot;; </li></ul>Java Programming: From Problem Analysis to Program Design, 5e
  67. 67. Comparing Strings (continued) Java Programming: From Problem Analysis to Program Design, 5e
  68. 68. Comparing Strings (continued) Java Programming: From Problem Analysis to Program Design, 5e
  69. 69. Chapter Summary <ul><li>Control structures are used to process programs </li></ul><ul><li>Logical expressions and order of precedence of operators are used in expressions </li></ul><ul><li>If statements </li></ul><ul><li>if … else statements </li></ul><ul><li>switch structures </li></ul><ul><li>Proper syntax for using control statements </li></ul><ul><li>Compare strings </li></ul>Java Programming: From Problem Analysis to Program Design, 5e
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×