1 / 19

Lecture 3 Decisions (Conditionals)

Lecture 3 Decisions (Conditionals). One of the essential features of computer programs is their ability to make decisions. Like a train that changes tracks depending on how the switches are set, a program can take different actions depending on inputs and other circumstances .

Télécharger la présentation

Lecture 3 Decisions (Conditionals)

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. Lecture 3 Decisions (Conditionals)

  2. One of the essential features of computer programs is their ability to make decisions. Like a train that changes tracks depending on how the switches are set, a program can take different actions depending on inputs and other circumstances. In this chapter, you will learn how to program simple and complex decisions. You will apply what you learn to the task of checking user input.

  3. Alternative Paths of Execution false radius < 0 true area = radius * radius * PI "Area is" + area "Incorrect input"

  4. Comparison Operators

  5. One-Way if Statements false conditional if (conditional) do something; do next thing; true do something do next thing

  6. Two-Way if Statements false conditional if (conditional) do something; do next thing; true do something do other thing do next thing

  7. if-else if-else false false conditional 1 conditional 2 true true yet another thing do something do other thing if (condition 1) do something; else if (condition 2) do other thing; else yet another thing; do next thing; do next thing

  8. Example Program

  9. import java.util.Scanner;publicclass LetterGradeDemo{publicstaticvoid main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter final average (0-100)... ");double avg = input.nextDouble();char grade;if (avg >= 90.0) { grade = 'A'; }elseif (avg >= 80.0) { grade = 'B'; }elseif (avg >= 70.0) { grade = 'C'; }elseif (avg >= 60.0) { grade = 'D'; }else { grade = 'F'; }if(grade == 'A') System.out.println("An average of " + avg + " is an " + grade);else System.out.println("An average of " + avg + " is a " + grade); }}

  10. Boolean Operators

  11. Example Program

  12. Switch Statements case 0 stuff break case 0: case 1 stuff break case 1: case 2 stuff break case 2: default stuff default:

  13. Switch Statement Rules (andStringtypes starting with JDK 7)

  14. Alternative Form for Conditional Statements For both version of the conditional constructs below, if x is greater than 0 then y is assigned the value 1, otherwise y is assign the value -1. This alternative version of the two-way conditional statement will not be used in this course. It is provided here for purposes of comparison and the understanding of java programs written by others.

  15. Formatting Console Output specifying display data types

  16. Formatting Console Output specifying width and precision

  17. Operator Precedence

  18. Confirmation Dialogs

  19. Summary

More Related