lecture 3 decisions (conditionals)
DESCRIPTION
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 . - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Lecture 3 Decisions (Conditionals)](https://reader035.vdocuments.us/reader035/viewer/2022062422/568135fe550346895d9d70a9/html5/thumbnails/1.jpg)
Lecture 3
Decisions (Conditionals)
![Page 2: Lecture 3 Decisions (Conditionals)](https://reader035.vdocuments.us/reader035/viewer/2022062422/568135fe550346895d9d70a9/html5/thumbnails/2.jpg)
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.
![Page 3: Lecture 3 Decisions (Conditionals)](https://reader035.vdocuments.us/reader035/viewer/2022062422/568135fe550346895d9d70a9/html5/thumbnails/3.jpg)
radius < 0
"Incorrect input" area = radius * radius * PI "Area is" + area
Alternative Paths of Execution
true
false
![Page 4: Lecture 3 Decisions (Conditionals)](https://reader035.vdocuments.us/reader035/viewer/2022062422/568135fe550346895d9d70a9/html5/thumbnails/4.jpg)
Comparison Operators
![Page 5: Lecture 3 Decisions (Conditionals)](https://reader035.vdocuments.us/reader035/viewer/2022062422/568135fe550346895d9d70a9/html5/thumbnails/5.jpg)
conditional
do something
true
false
do next thing
if (conditional) do something;do next thing;
One-Way if Statements
![Page 6: Lecture 3 Decisions (Conditionals)](https://reader035.vdocuments.us/reader035/viewer/2022062422/568135fe550346895d9d70a9/html5/thumbnails/6.jpg)
if (conditional) do something;do next thing;
conditional
do something
true
false
do next thing
do other thing
Two-Way if Statements
![Page 7: Lecture 3 Decisions (Conditionals)](https://reader035.vdocuments.us/reader035/viewer/2022062422/568135fe550346895d9d70a9/html5/thumbnails/7.jpg)
conditional1
do something
true
false
do next thing
do other thing
conditional2
true
false
yet another thing
if (condition 1) do something;else if (condition 2) do other thing;else yet another thing;do next thing;
if-else if-else
![Page 8: Lecture 3 Decisions (Conditionals)](https://reader035.vdocuments.us/reader035/viewer/2022062422/568135fe550346895d9d70a9/html5/thumbnails/8.jpg)
Example Program
![Page 9: Lecture 3 Decisions (Conditionals)](https://reader035.vdocuments.us/reader035/viewer/2022062422/568135fe550346895d9d70a9/html5/thumbnails/9.jpg)
import java.util.Scanner;public class LetterGradeDemo{ public static void 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'; } else if (avg >= 80.0) { grade = 'B'; } else if (avg >= 70.0) { grade = 'C'; } else if (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); }}
![Page 10: Lecture 3 Decisions (Conditionals)](https://reader035.vdocuments.us/reader035/viewer/2022062422/568135fe550346895d9d70a9/html5/thumbnails/10.jpg)
Boolean Operators
![Page 11: Lecture 3 Decisions (Conditionals)](https://reader035.vdocuments.us/reader035/viewer/2022062422/568135fe550346895d9d70a9/html5/thumbnails/11.jpg)
Example Program
![Page 12: Lecture 3 Decisions (Conditionals)](https://reader035.vdocuments.us/reader035/viewer/2022062422/568135fe550346895d9d70a9/html5/thumbnails/12.jpg)
case 0: case 0 stuff break
case 1:
case 2:
default:
case 1 stuff
case 2 stuff
default stuff
break
break
Switch Statements
![Page 13: Lecture 3 Decisions (Conditionals)](https://reader035.vdocuments.us/reader035/viewer/2022062422/568135fe550346895d9d70a9/html5/thumbnails/13.jpg)
Switch Statement Rules
(and String types starting with JDK 7)
![Page 14: Lecture 3 Decisions (Conditionals)](https://reader035.vdocuments.us/reader035/viewer/2022062422/568135fe550346895d9d70a9/html5/thumbnails/14.jpg)
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.
![Page 15: Lecture 3 Decisions (Conditionals)](https://reader035.vdocuments.us/reader035/viewer/2022062422/568135fe550346895d9d70a9/html5/thumbnails/15.jpg)
Formatting Console Outputspecifying display data types
![Page 16: Lecture 3 Decisions (Conditionals)](https://reader035.vdocuments.us/reader035/viewer/2022062422/568135fe550346895d9d70a9/html5/thumbnails/16.jpg)
Formatting Console Outputspecifying width and precision
![Page 17: Lecture 3 Decisions (Conditionals)](https://reader035.vdocuments.us/reader035/viewer/2022062422/568135fe550346895d9d70a9/html5/thumbnails/17.jpg)
Operator Precedence
![Page 18: Lecture 3 Decisions (Conditionals)](https://reader035.vdocuments.us/reader035/viewer/2022062422/568135fe550346895d9d70a9/html5/thumbnails/18.jpg)
Confirmation Dialogs
![Page 19: Lecture 3 Decisions (Conditionals)](https://reader035.vdocuments.us/reader035/viewer/2022062422/568135fe550346895d9d70a9/html5/thumbnails/19.jpg)
Summary