The if Statement Control structure: logical design that controls order in which set of statements execute Sequence structure: set of statements that execute.

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. S/E with Control Structures 3.1 Relational Operators and Expressions 3.2 If and if-else Statements 3.3 The Type Double 3.4 Program Design with the While.
Making Decisions in Python Sec 9-10 Web Design. Objectives The student will: Understand how to make a decision in Python Understand the structure of an.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Basic Elements of Programming A VB program is built from statements, statements from expressions, expressions from operators and operands, and operands.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Chapter 4: Control Structures: 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.
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
Python – Making Decisions Lecture 02. Control Structures A program that only has one flow is useful but limited. We can use if statements to make these.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.
PAGES:51-59 SECTION: CONTROL1 : DECISIONS Decisions.
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.
INLS 560 – C ONDITIONALS Instructor: Jason Carter.
Selection Control Structures. Simple Program Design, Fourth Edition Chapter 4 2 Objectives In this chapter you will be able to: Elaborate on the uses.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
Computer Science 111 Fundamentals of Programming I Making Choices with if Statements.
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
Conditional Expression One of the most useful tools for processing information in an event procedure is a conditional expression. A conditional expression.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
Decision Structures and Boolean Variables. Sequence Structures Thus far, we’ve been programming “sequence structures” Thus far, we’ve been programming.
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.
Decision Making CMSC 201 Chang (rev ).
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
Introduction to Decision Structures and Boolean Variables
The if…else Selection Statement
More on the Selection Structure
Basics programming concepts-2
Sequence, Selection, Iteration The IF Statement
Chapter 4: Making Decisions.
Upsorn Praphamontripong CS 1110 Introduction to Programming Fall 2016
Chapter 4: Making Decisions.
Lecture 3- Decision Structures
Operator Precedence Operators Precedence Parentheses () unary
The Selection Structure
Topics The if Statement The if-else Statement Comparing Strings
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: Decision Structures and Boolean Logic
Chapter 4: Making Decisions.
Lecture 3 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz
Javascript Conditionals.
Making Decisions in a Program
Topics The if Statement The if-else Statement Comparing Strings
Chapter 4: Decision Structures and Boolean Logic
3 Control Statements:.
Pages:51-59 Section: Control1 : decisions
3. Decision Structures Rocky K. C. Chang 19 September 2018
Selection Statements.
Comparing Strings Strings can be compared using the == and != operators String comparisons are case sensitive Strings can be compared using >, =, and.
Introduction to Decision Structures and Boolean Variables
Topics The if Statement The if-else Statement Comparing Strings
Understanding Conditions
CS2011 Introduction to Programming I Selections (I)
Lecture Notes – Week 2 Lecture-2
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Pages:51-59 Section: Control1 : decisions
Introduction to Flowcharting
Chapter 4: Decision Structures and Boolean Logic
Control Structures.
Presentation transcript:

The if Statement Control structure: logical design that controls order in which set of statements execute Sequence structure: set of statements that execute in the order they appear Decision structure: specific action(s) performed only if a condition exists Also known as selection structure

The if Statement (cont’d.) In flowchart, diamond represents true/false condition that must be tested Actions can be conditionally executed Performed only when a condition is true Single alternative decision structure: provides only one alternative path of execution If condition is not true, exit the structure

The if Statement (cont’d.)

The if Statement (cont’d.) Python syntax: if condition: Statement First line known as the if clause Includes the keyword if followed by condition The condition can be true or false When the if statement executes, the condition is tested, and if it is true the block statements are executed. Otherwise, block statements are skipped

Boolean Expressions and Relational Operators Boolean expression: expression tested by if statement to determine if it is true or false Example: a > b true if a is greater than b; false otherwise Relational operator: determines whether a specific relationship exists between two values Example: greater than (>)

Boolean Expressions and Relational Operators (cont’d.) >= and <= operators test more than one relationship It is enough for one of the relationships to exist for the expression to be true == operator determines whether the two operands are equal to one another Do not confuse with assignment operator (=) != operator determines whether the two operands are not equal

Boolean Expressions and Relational Operators (cont’d.)

Boolean Expressions and Relational Operators (cont’d.) Using a Boolean expression with the > relational operator

Boolean Expressions and Relational Operators (cont’d.) Any relational operator can be used in a decision block Example: if balance == 0 Example: if payment != balance It is possible to have a block inside another block Example: if statement inside a function Statements in inner block must be indented with respect to the outer block

Example # This program gets three test scores and displays their average. It congratulates the user if the average is high. high_score = 95 # Get the three test scores. test1 = int(input('Enter the score for test 1: ')) test2 = int(input('Enter the score for test 2: ')) test3 = int(input('Enter the score for test 3: ')) # Calculate the average test score. average = (test1 + test2 + test3) / 3 # Print the average. print('The average score is', average) # If the average is a high score, congratulate the user. if average >= high_score: print('Congratulations!') print('That is a great average!')

The if-else Statement Dual alternative decision structure: two possible paths of execution One is taken if the condition is true, and the other if the condition is false Syntax: if condition: statements else: other statements if clause and else clause must be aligned Statements must be consistently indented

The if-else Statement (cont’d.)

The if-else Statement (cont’d.)

# Variables for base hours and the overtime multiplier. base_hours = 40 # Base hours per week ot_multiplier = 1.5 # Overtime multiplier # Get the hours worked and the hourly pay rate. hours = float(input('Enter the number of hours worked: ')) pay_rate = float(input('Enter the hourly pay rate: ')) # Calculate and display the gross pay. if hours > base_hours: # First, get the number of overtime hours worked. overtime_hours = hours - base_hours # Calculate the amount of overtime pay. overtime_pay = overtime_hours * pay_rate * ot_multiplier # Calculate the gross pay. gross_pay = base_hours * pay_rate + overtime_pay else: # Calculate the gross pay without overtime. gross_pay = hours * pay_rate # Display the gross pay. print('The gross pay is $', gross_pay)