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

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

3-1 Chapter 3 Flow of Control (part a - branching)
The If Then Else Statement Using JavaScript TIP The If Statement is the fundamental control structure that allows branches in the flow of control in a.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Decisions (Conditional Programming) Chapter 5 (Sec. 5.1 & 5.2)
Flow of Control (1) : Logic Clark Savage Turner, J.D., Ph.D. Some lecture slides have been adapted from those developed.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Logical Operators and Conditional statements
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
Visual C++ Programming: Concepts and Projects
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/33 Conditionals and Loops Now we will examine programming statements.
Flow of Control Java Programming Mrs. C. Furman January 5, 2009.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Decision Structures and Boolean Logic
Computer Science Selection Structures.
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
1. We’ve learned that our programs are read by the compiler in order, from top to bottom, just as they are written The order of statement execution is.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
1 Interactive Applications (CLI) and Math Interactive Applications Command Line Interfaces The Math Class Flow of Control / Conditional Statements The.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
Flow of Control Unless indicated otherwise, the order of statement execution through a method is linear: one after the other in the order they are written.
Control statements Mostafa Abdallah
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Design A software design specifies how a program will accomplish its requirements A design includes one or more algorithms to accomplish its goal.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CNG 140 C Programming (Lecture set 3)
Operator Precedence Operators Precedence Parentheses () unary
Boolean Expressions and If
Topics The if Statement The if-else Statement Comparing Strings
Boolean Expressions & the ‘if’ Statement
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
The order in which statements are executed is called the flow of control. Most of the time, a running program starts at the first programming statement,
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Topics The if Statement The if-else Statement Comparing Strings
Chapter 3: Program Statements
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
CS139 October 11, 2004.
Visual Basic – Decision Statements
Selection Statements.
Chapter 4: Control Structures I (Selection)
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
CS2011 Introduction to Programming I Selections (I)
CSC 1051 – Data Structures and Algorithms I
The Selection Structure
Comparing Data & the ‘switch’ Statement
Comparing Data & the ‘switch’ Statement
CprE 185: Intro to Problem Solving (using C)
Conditionals and Loops
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
Flow of Control Flow of control is the order in which a program performs actions. Up to this point, the order has been sequential. A branching statement.
Boolean Expressions & the ‘if’ Statement
CprE 185: Intro to Problem Solving (using C)
3.0 - Design A software design specifies how a program will accomplish its requirements A design includes one or more algorithms to accomplish its goal.
Presentation transcript:

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

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

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

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

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

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

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

If statement Example Output?

Compound Boolean Expressions - if statement Output?

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

The if-else Example Output?

If else if statement See page 157

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

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.

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”

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