1 / 32

Chapter 3:Decision Structures

Chapter 3:Decision Structures. Chapter 3:Decision Structures. 3.1 The if Statement 3.2 The if-else Statement 3.3 The if-else-if Statement  3.4 Nested if Statements 3.5 Logical Operators 3.6 Comparing String Objects 3.7 More About Variable Declaration and Scope 3.9 The switch Statement

stefano
Télécharger la présentation

Chapter 3:Decision Structures

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:Decision Structures

  2. Chapter 3:Decision Structures • 3.1 The if Statement • 3.2 The if-else Statement • 3.3 The if-else-if Statement  • 3.4 Nested if Statements • 3.5 Logical Operators • 3.6 Comparing String Objects • 3.7 More About Variable Declaration and Scope • 3.9 The switch Statement • 3.10 Creating Objects with the DecimalFormat Class • 3.12 Common Errors to Avoid

  3. The if Statement • The code in methods executes sequentially. • The if statement allows the programmer to make decisions on whether a section of code executes or not. • The if statement uses a boolean expression as an argument to decide if the next statement or block of statements executes. if(boolean expression is true) execute next statement.

  4. Yes Is it cold outside? Wear a coat. Flowcharts • If statements can be modeled as a flow chart. if (coldOutside) wearCoat(); No

  5. Yes Is it cold outside? Flowcharts • A block if statement may be modeled as: if (coldOutside){ wearCoat(); wearHat(); wearGloves(); } Wear a coat. Wear a hat. Wear gloves. Note the use of curly braces to block several statements together.

  6. Relational Operators • In most cases, the boolean expression, used by the if statement, uses relational operators.

  7. Boolean Expressions • A boolean expression is any variable or calculation that results in a true or false condition.

  8. If Statements and Boolean Expressions if (x > y) System.out.println(“X is greater than Y”); if(x == y) System.out.println(“X is equal to Y”); if(x != y){ System.out.println(“X is not equal to Y”); x = y; System.out.println(“However, now it is.”); }

  9. AverageScore.java double score1; double score2; double score3; double average; String input; input = JOptionPane.showInputDialog(“Enter ” + “ score #1:”); score1 = Double.parseDouble(input);

  10. AverageScore.java input = JOptionPane.showInputDialog("Enter score #2:"); score2 = Double.parseDouble(input); input = JOptionPane.showInputDialog("Enter score #3:"); score3 = Double.parseDouble(input); average = (score1+score2+score3) / 3.0;

  11. AverageScore.java JOptionPane.showMessageDialog(null, "The average is " + average); if (average > 95) JOptionPane.showMessageDialog(null, "That's a great score!"); } }

  12. Programming Style and if Statements • If statements can span more than one line; however, they are still one statement. if(average > 95) System.out.println(“That’s a great score!”); • is functionally equivalent to if(average > 95) System.out.println(“That’s a” + “great score!”);

  13. Programming Style and if Statements • Rules of thumb: • The conditionally executed statement should be on the line after the if condition. • The conditionally executed statement should be indented one level from the if condition. • If an if statement does not have the block curly braces, it is ended by the first semicolon encountered after the if condition. if(expression) statement; No semicolon here.Semicolon ends statement here.

  14. Block if Statements • Conditionally executed statements can be grouped into a block by using curly braces {} to enclose them. • If curly braces are used to group conditionally executed statements, the if statement is ended by the closing curly brace. if(expression){ statement1; statement2; } Curly brace ends the statement.

  15. Block if Statements • Remember that if the curly braces are not used, then only the next statement after the if condition will be executed conditionally. if(expression) statement1; statement2; statement3; Only this statement is conditionally executed.

  16. Flags • A flag is a boolean variable that monitors some condition in a program. • When a condition is true, the flag is set to a true value. • The flag can be tested to see if the condition has changed. if(average > 95) highScore = true; • Later, this condition can be tested: if(highScore) System.out.println(“That’s a high score!);

  17. Comparing Characters • Characters can be tested using the relational operators. • Characters are stored in the computer using the unicode character format. • Unicode is stored as a sixteen (16) bit number. • Characters are ordinal, meaning they have an order in the unicode character set. • Since characters are ordinal, they can be compared to each other. char c = ‘A’; if(c < ‘Z’) System.out.println(“A is less than Z”);

  18. if-else Statements • The if-else statement adds the ability to conditionally execute code based if the expression of the if statement is false. if(expression) statementOrBlockIfTrue; else statementOrBlockIfFalse;

  19. Division.java double number1, number2; // Division operands double quotient; // Result of division number1 = Double.parseDouble( JOptionPane.showInputDialog( “Enter a number: “)); number2 = Double.parseDouble( JOptionPane.showInputDialog( “Enter another number: “)); if (number2 == 0){ System.out.println("Division by zero is not possible."); System.out.println("Please run the program again and "); System.out.println("enter a number other than zero."); }

  20. Division.java else{ quotient = number1 / number2; System.out.print("The quotient of " + number1); System.out.print(" divided by " + number2); System.out.println(" is " + quotient); }

  21. Yes Is it cold outside? Wear shorts. Wear a coat. if-else Statement Flowcharts No

  22. if-else-if Statements • if-else-if statements can become very complex. • Imagine the following decision set. if it is very cold, wear a heavy coat, else, if it is chilly, wear a light jacket, else, if it is windy wear a windbreaker, else, if it is hot, wear no jacket.

  23. if-else-if Statements if (expression) statement or block else if (expression) statement or block // Put as many else ifs as needed here else statement or block • Care must be used since else statements match up with the immediately preceding unmatched if statement.

  24. if-else-if Flowchart

  25. TestResults.java int testScore; // Numeric test score String input; // To hold the user's input = JOptionPane.showInputDialog( "Enter your numeric test score and I” +“ will tell you the grade: "); testScore = Integer.parseInt(input);

  26. TestResults.java if (testScore < 60) JOptionPane.showMessageDialog(null, "Your grade is F."); else if (testScore < 70) JOptionPane.showMessageDialog(null, "Your grade is D."); else if (testScore < 80) JOptionPane.showMessageDialog(null, "Your grade is C."); else if (testScore < 90) JOptionPane.showMessageDialog(null, "Your grade is B."); else if (testScore <= 100) JOptionPane.showMessageDialog(null, "Your grade is A."); else // Invalid score JOptionPane.showMessageDialog(null, "Invalid score.");

  27. Nested if Statements • If an if statement appears inside of another if statement (single or block) it is called a nested if statement. • The nested if is only executed if the if statement it is in results in a true condition. • Nested if statements can get very complex, very quickly.

  28. LoanQualifier.java double salary, yearsOnJob; String input; input = JOptionPane.showInputDialog("Enter your " + "annual salary."); salary = Double.parseDouble(input); input = JOptionPane.showInputDialog("Enter the number of " +"years at your current job."); yearsOnJob = Double.parseDouble(input);

  29. LoanQualifier.java if (salary >= 30000){ if (yearsOnJob >= 2){ JOptionPane.showMessageDialog(null, "You qualify " + "for the loan."); } else { JOptionPane.showMessageDialog(null, "You must have " + "been on your current job for at least " + "two years to qualify."); } } else { JOptionPane.showMessageDialog(null, "You must earn at” +“least $30,000 per year to qualify."); }

  30. No Yes Is it cold outside? Wear shorts. Is it snowing? Yes No Wear a jacket. Wear a parka. Nested if Statement Flowcharts

  31. if-else Matching • Curly brace use is not required if there is only one statement to be conditionally executed. • However, sometimes curly braces can help make the program more readable. • Additionally, proper indentation makes it much easier to match up else statements with their corresponding if statement.

  32. if-else Matching if (employed == 'y') { if (recentGrad == 'y') { System.out.println("You qualify for the special interest rate."); } else { System.out.println("You must be a recent college graduate to qualify."); } } else { System.out.println("You must be employed to qualify."); } This else matches with this if. This else matches with this if.

More Related