Class 12.

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

CSC 110 Decision structures [Reading: chapter 7] CSC 110 H1.
1 Decision Structures Chapter 7 (Skip 7.1.3, 7.4) Adapted from the online slides provided by John Zelle (
Conditional Statements Introduction to Computing Science and Programming I.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
James Tam Making Decisions In Python In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
If Statements Sections 1.25, Control Structures o All code thus far executes every line of code sequentially o We want to be able to repeat,
C++ for Engineers and Scientists Third Edition
IS 1181 IS 118 Introduction to Development Tools VB Chapter 03.
Geography 465 Assignments, Conditionals, and Loops.
Chapter 7 Decision Structures
Python Control of Flow.
Python Control Flow statements There are three control flow statements in Python - if, for and while.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
Topics: IF If statements Else clauses. IF Statement For the conditional expression, evaluating to True or False, the simple IF statement is if : x = 7.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Decision Making CMSC 201. Overview Today we will learn about: Boolean expressions Decision making.
Xiaojuan Cai Computational Thinking 1 Lecture 7 Decision Structure Xiaojuan Cai (蔡小娟) Fall, 2015.
8. DECISION STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
CS 127 Exceptions and Decision Structures. Exception Handling This concept was created to allow a programmer to write code that catches and deals with.
Decision Making CMSC 201 Chang (rev ).
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Chapter 5 – Decision Making. Structured Programming Sequence Selection Repetition yesno yes no.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Python Programming Module 4 Sensors and Loops Python Programming, 2/e1.
 Type Called bool  Bool has only two possible values: True and False.
Python Programming, 3/e1 Python Programming: An Introduction to Computer Science Chapter 7 Decision Structures.
Python Boot Camp Booleans While loops If statements Else clauses
Control Flow (Python) Dr. José M. Reyes Álamo.
If/Else Statements.
Line Continuation, Output Formatting, and Decision Structures
Chapter 4: Making Decisions.
Selections Java.
Upsorn Praphamontripong CS 1110 Introduction to Programming Fall 2016
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
CMSC201 Computer Science I for Majors Lecture 06 – Decision Structures
Chapter 4: Making Decisions.
Python: Control Structures
Python Programming: An Introduction to Computer Science
EGR 2261 Unit 4 Control Structures I: Selection
Topics The if Statement The if-else Statement Comparing Strings
Chapter 4: Making Decisions.
CMSC201 Computer Science I for Majors Lecture 04 – Decision Structures
Logical Operators, Boolean Data Types
Worksheet Key 9 11/14/2018 8:58 PM Quadratic Formula.
Topics The if Statement The if-else Statement Comparing Strings
Line Continuation, Output Formatting, and Decision Structures
Bools and simple if statements
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions (Part 2)
Control Structures: Selection Statement
3. Decision Structures Rocky K. C. Chang 19 September 2018
Introduction to Decision Structures and Boolean Variables
Conditional and iterative statements
CS 1111 Introduction to Programming Spring 2019
15-110: Principles of Computing
Types, Truth, and Expressions (Part 2)
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Class 13 function example unstring if if else if elif else
Winter 2019 CISC101 4/16/2019 CISC101 Reminders
CHAPTER 5: Control Flow Tools (if statement)
Chapter 3: Selection Structures: Making Decisions
Control Structures: Selection Statement
Jurassic Park Python Programming, 2/e.
Combining conditions with and, or, and not.
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
Presentation transcript:

Class 12

if example: Temperature Warnings if fahrenheit >= 90: print("It's really hot out there, be careful!") if fahrenheit <= 30: print("Brrrrr. Be sure to dress warmly") Python Programming, 3/e

if The Python if statement is used to implement the decision. if <condition>: <body> The body is a sequence of one or more statements indented under the if heading. Python Programming, 3/e

if The semantics of the if should be clear. First, the condition in the heading is evaluated. If the condition is true, the sequence of statements in the body is executed, and then control passes to the next statement in the program. If the condition is false, the statements in the body are skipped, and control passes to the next statement in the program. Python Programming, 3/e

Forming Simple Conditions Simple comparison: <expr> <relop> <expr> <relop> is short for relational operator Python Programming, 3/e

Forming Simple Conditions Python Mathematics Meaning < Less than <= ≤ Less than or equal to == = Equal to >= ≥ Greater than or equal to > Greater than != ≠ Not equal to Python Programming, 3/e

Forming Simple Conditions Notice the use of == for equality. Since Python uses = to indicate assignment, a different symbol is required for the concept of equality. A common mistake is using = in conditions! Python Programming, 3/e

Forming Simple Conditions Conditions may compare either numbers or strings. When comparing strings, the ordering is lexigraphic, meaning that the strings are sorted based on the underlying Unicode. All upper-case Latin letters come before lower-case letters: (“Bbbb” comes before “aaaa”) Python Programming, 3/e

Boolean Expression When a Boolean expression is evaluated, it produces either a value of True (meaning the condition holds), or it produces False (it does not hold). (Some computer languages use 1 and 0 to represent True and False.) Python Programming, 3/e

Examples: Simple Conditions Boolean conditions are of type bool and the Boolean values of true and false are represented by the literals True and False. >>> 3 < 4 True >>> 3 * 4 < 3 + 4 False >>> "hello" == "hello" >>> "goodbye" < "hello" Python Programming, 3/e

Two-Way Decisions In Python, a two-way decision can be implemented by attaching an else clause onto an if clause. This is called an if-else statement: if <condition>: <statements> else: <statements> Python Programming, 3/e

Aside: Quadratic equation ax2 + bx + c roots=(-b±sqrt(b2-4ac)) / 2a if the sqrt exists. b2-4ac is called the discriminant. Python Programming, 3/e

Example: Two-Way Decisions discrim = b * b - 4 * a * c if discrim < 0: print("\nThe equation has no real roots!") else: discRoot = math.sqrt(b * b - 4 * a * c) root1 = (-b + discRoot) / (2 * a) root2 = (-b - discRoot) / (2 * a) print ("\nThe solutions are:", root1, root2 ) Python Programming, 3/e

Multi-Way Decisions Check the value of discrim when < 0: handle the case of no roots when = 0: handle the case of a double root when > 0: handle the case of two distinct roots We can do this with two if-else statements, one inside the other. Putting one compound statement inside of another is called nesting. Python Programming, 3/e

Multi-Way Decisions if discrim < 0: print("Equation has no real roots") else: if discrim == 0: root = -b / (2 * a) print("There is a double root at", root) # Do stuff for two roots Python Programming, 3/e

Multi-Way Decisions Imagine if we needed to make a five-way decision using nesting. The if-else statements would be nested four levels deep! There is a construct in Python that achieves this, combining an else followed immediately by an if into a single elif. Python Programming, 3/e

Multi-Way Decisions if <condition1>: <case1 statements> elif <condition2>: <case2 statements> elif <condition3>: <case3 statements> … else: <default statements> Python Programming, 3/e

Multi-Way Decisions This form sets off any number of mutually exclusive code blocks. Python evaluates each condition in turn looking for the first one that is true. If a true condition is found, the statements indented under that condition are executed, and control passes to the next statement after the entire if-elif-else. If none are true, the statements under else are performed. Python Programming, 3/e

Multi-Way Decisions The else is optional. If there is no else, it’s possible no indented block would be executed. Python Programming, 3/e

Example: Multi-Way Decisions discrim = b * b - 4 * a * c if discrim < 0: print("\nThe equation has no real roots!") elif discrim == 0: root = -b / (2 * a) print("\nThere is a double root at", root) else: discRoot = math.sqrt(b * b - 4 * a * c) root1 = (-b + discRoot) / (2 * a) root2 = (-b - discRoot) / (2 * a) print("\nThe solutions are:", root1, root2 ) Python Programming, 3/e

and if grade >= 40 and grade < 80: print("Go to tutoring!") 15 25 35 45 55 65 75 85 95 MAY write if 40 <= grade and grade < 80: In Python, but NOT other languages, may write if 40 <= grade < 80:

or AaBbCcDdEe if choice == 'A' or choice == 'a' : print("You want the tuna salad.") elif choice== 'B' or choice == 'b' : print ("You want pizza.") else print ("Invalid selection.") MAY NOT write if choice == 'A' or 'a': (get an answer, not what you intended! Always true!) MAY write if choice.upper() == 'A': BbCcDdEe

not if not (x == 0) : print ("point not on the y axis") MAY also write if x != 0 : if not (width > 0 and width < 15): print ("Rug won't fit this room.") 15