9781439035665 ppt ch04

1,053 views
934 views

Published on

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

  • Be the first to like this

No Downloads
Views
Total views
1,053
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
37
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

9781439035665 ppt ch04

  1. 1. Java Programming: From Problem Analysis to Program Design, 4e 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, 4e
  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, 4e
  4. 4. Flow of Execution Java Programming: From Problem Analysis to Program Design, 4e
  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, 4e
  6. 6. Relational Operators in Java Java Programming: From Problem Analysis to Program Design, 4e
  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, 4e
  8. 8. Java Programming: From Problem Analysis to Program Design, 4e
  9. 9. Java Programming: From Problem Analysis to Program Design, 4e
  10. 10. Relational Operators and the Unicode Collating Sequence Java Programming: From Problem Analysis to Program Design, 4e
  11. 11. Logical (Boolean) Operators Java Programming: From Problem Analysis to Program Design, 4e
  12. 12. Logical (Boolean) Operators (continued) Java Programming: From Problem Analysis to Program Design, 4e
  13. 13. Logical (Boolean) Operators (continued) Java Programming: From Problem Analysis to Program Design, 4e
  14. 14. Logical (Boolean) Operators (continued) Java Programming: From Problem Analysis to Program Design, 4e
  15. 15. Precedence of Operators Java Programming: From Problem Analysis to Program Design, 4e
  16. 16. Precedence of Operators (continued) Java Programming: From Problem Analysis to Program Design, 4e
  17. 17. Short-Circuit Evaluation <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>Java Programming: From Problem Analysis to Program Design, 4e
  18. 18. 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, 4e
  19. 19. 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, 4e
  20. 20. One-Way Selection (continued) Java Programming: From Problem Analysis to Program Design, 4e
  21. 21. <ul><li>Example 4-10 </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, 4e
  22. 22. <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, 4e
  23. 23. 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, 4e
  24. 24. Two-Way Selection (continued) Java Programming: From Problem Analysis to Program Design, 4e
  25. 25. Two-Way Selection (continued) Java Programming: From Problem Analysis to Program Design, 4e Example 4-14 if (hours > 40.0) wages = 40.0 * rate + 1.5 * rate * (hours - 40.0); else wages = hours * rate;
  26. 26. Two-Way Selection (continued) Java Programming: From Problem Analysis to Program Design, 4e <ul><li>Example 4-14 (continued) </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>
  27. 27. Compound (Block of) Statements <ul><li>Syntax </li></ul>Java Programming: From Problem Analysis to Program Design, 4e
  28. 28. 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, 4e
  29. 29. 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, 4e
  30. 30. 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, 4e
  31. 31. switch Structures Java Programming: From Problem Analysis to Program Design, 4e
  32. 32. switch Structures (continued) Java Programming: From Problem Analysis to Program Design, 4e <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>
  33. 33. switch Structures (continued) Java Programming: From Problem Analysis to Program Design, 4e <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>
  34. 34. switch Structures (continued) Java Programming: From Problem Analysis to Program Design, 4e <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>
  35. 35. switch Structures (continued) Java Programming: From Problem Analysis to Program Design, 4e
  36. 36. switch Structures (continued) Java Programming: From Problem Analysis to Program Design, 4e
  37. 37. <ul><li>Example 4-23 </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, 4e
  38. 38. <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, 4e
  39. 39. 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, 4e
  40. 40. 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, 4e
  41. 41. 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, 4e
  42. 42. 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, 4e
  43. 43. Comparing Strings (continued) Java Programming: From Problem Analysis to Program Design, 4e
  44. 44. Comparing Strings (continued) Java Programming: From Problem Analysis to Program Design, 4e
  45. 45. 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, 4e

×