Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.

Similar presentations


Presentation on theme: "Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui."— Presentation transcript:

1 Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output
Prof. Salim Arfaoui

2 Boolean Expressions, Making Decisions, and Disk Input and Output
Alternatives to Sequential Execution Boolean Expressions The if Statement The if-else Statement Nested if Statements Console Input and the Scanner Class

3 Alternatives to Sequential Execution
Unless indicated otherwise, the order of statement execution is linear: one after the other in the order they are written. Some programming statements modify that order, allowing us to: Decide whether or not to execute a particular statement, or Perform a statement over and over repetitively. The order of statement execution is called the flow of control

4 Alternatives to Sequential Execution
A conditional statement lets us choose which statement will be executed next Therefore they are sometimes called selection statements Conditional statements give us the power to make basic decisions Java's conditional statements are the if statement, the if-else statement, and the switch statement

5 Boolean Expressions A simple Boolean expression evaluates to either true or false. In Java, these expressions consist of a relational or an equality operator surrounded by two operands. Note the difference between the equality operator (==) and the assignment operator (=) See page 143

6 Compound Boolean Expressions
Compound Boolean expressions also evaluate to either true or false. When used in a control-of-flow statement, compound Boolean expressions use the Java conditional binary logical operators AND and OR to combine the truth values of two or more operands. See page 145

7 The if Statement See page 149 if(control expression) { statements }
if is a Java reserved word The control expression must be a Boolean expression. It must evaluate to either true or false. if(control expression) { statements } If the condition is true, the statement is executed. If it is false, the statement is skipped. See page 149

8 If statement Example Output?

9 Compound Boolean Expressions - if statement
Output?

10 The if-else Statement See page 155 if(Boolean control expression){
An else clause can be added to an if statement to make it an if-else statement: If the condition is true, code block of if is executed; if the condition is false, code block of else is executed One or the other will be executed, but not both Syntax: if(Boolean control expression){ // One or more if clause statements } else{ // One or more else clause statements See page 155

11 The if-else Example Output?

12 If else if statement See page 157

13 NESTED if STATEMENTS See page 163
The statement executed as a result of an if statement or else clause could be another if statement. These are called nested if statements. See page 163

14 Practice 1 Write a program to:
1) ask a user to input two strings from the system console. 2) If the strings are identical, output Identical. Otherwise output Different.

15 Write a program to: 1) Ask a user to enter a scores for Exam1, Exam2 and Exam3 from the system console. 2) Calculate average score. 3) If the average score >= 92, output “Letter grade is A”. Else if average score >= 82, output “Letter grade is B”. Else if average score >= 72, output “Letter grade is C”. Otherwise output, “Letter grade is F”

16 Boolean Expressions, Making Decisions, and Disk Input and Output
Next Class The switch Statement Disk Input and Output: A First Look Catching Thrown Exceptions Chapter Summary


Download ppt "Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui."

Similar presentations


Ads by Google