Decisions in Python elif. A new keyword elif A contraction of “else if” Used to tie two if statements (or more) together into one structure Syntax – elif,

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

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.
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Python Programming Language
Overview Program flow Decision / branching / selection structures True & False in Python Comparison operators & Boolean expressions if … if … else if …
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.
Introduction to Computers and Programming Lecture 6 Professor: Evan Korth New York University.
Jay Summet CS 1 with Robots IPRE Python Review 1.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
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,
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
Making Decisions In Python
The If/Else Statement, Boolean Flags, and Menus Page 180
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
Decision Making George Mason University. Today’s topics 2 Review of Chapter 2: Decision Making Go over exercises Decision making in Python.
TODAY’S LECTURE Review Chapter 2 Go over exercises.
Lecture 11 – if … else, if... elif statements, nested ifs COMPSCI 1 1 Principles of Programming.
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
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.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
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.
Rational Expressions and selection structures Relational operators Logical operators Selection structures.
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Controlling Execution Dong Shao, Nanjing Unviersity.
Decisions in Python Bools and simple if statements.
Chapter 4 Making Decision Csc 125 C++ programming language Fall 2005.
Copyright 2003 Scott/Jones Publishing Making Decisions.
Conditional Expression One of the most useful tools for processing information in an event procedure is a conditional expression. A conditional expression.
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 7 Conditionals and Loops 4/18/09 Python Mini-Course: Day 2 - Lesson 7.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Decision Making CMSC 201. Overview Today we will learn about: Boolean expressions Decision making.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Selection Statements. Introduction Today we learn more about learn to make decisions in Turing ▫Nested if statements, ▫case statements.
Flow of Control Chapter 3. Outline Branching Statements Java Loop Statements Programming with Loops The Type boolean.
Java Programming Fifth Edition Chapter 5 Making Decisions.
Chapter 5: Making Decisions. Objectives Plan decision-making logic Make decisions with the if and if…else structures Use multiple statements in if and.
Decisions in Python Boolean functions. A Boolean function This is a function which returns a bool result (True or False). The function can certainly work.
Variables and Expressions CMSC 201. Today we start Python! Two ways to use python: You can write a program, as a series of instructions in a file, and.
8. DECISION STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Decision Making CMSC 201 Chang (rev ).
The If Statement There are no switch statements in Python. You need to use just if statements. There are no switch statements in Python. You need to use.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Python Flow of Control CS 4320, SPRING Iteration The ‘for’ loop is good for stepping through lists The code below will print each element in the.
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
Today… Operators, Cont. Operator Precedence Conditional Statement Syntax. Winter 2016CISC101 - Prof. McLeod1.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Silberschatz and Galvin  C Programming Language Decision making in C Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University.
CS 115 Lecture 8 Conditionals, if statements Taken from notes by Dr. Neil Moore.
Control Flow (Python) Dr. José M. Reyes Álamo.
CS 115 Lecture Conditionals and if statements
Chapter 4: Making Decisions.
Upsorn Praphamontripong CS 1110 Introduction to Programming Fall 2016
Topics The if Statement The if-else Statement Comparing Strings
If, else, elif.
Topics The if Statement The if-else Statement Comparing Strings
Selection CIS 40 – Introduction to Programming in Python
Bools and simple if statements
Types, Truth, and Expressions (Part 2)
Pages:51-59 Section: Control1 : decisions
CS 1111 Introduction to Programming Spring 2019
Thinking about if’s.
CHAPTER 5: Control Flow Tools (if statement)
Pages:51-59 Section: Control1 : decisions
Presentation transcript:

Decisions in Python elif

A new keyword elif A contraction of “else if” Used to tie two if statements (or more) together into one structure Syntax – elif, followed by a bool expression, ended with colon elif will always be part of an if statement – cannot stand on its own if a > b: print(‘a’) elif a > b+c: print(“c”) else: print(“b”)

Semantics of elif When you have an if structure that contains an elif 1.Evaluate the first Boolean expression, Test1 2.If Test1 comes out True, do the statements after the if and skip the rest of the structure 3.If Test1 comes out False, go to the elif and do the Boolean expression there (Test2). If Test2 is True, do the statements after the elif line, then skip the rest of the structure. If Test2 is False, go to the next elif or else statement and do the Boolean expression there. 4.If all the tests are False, eventually all the tests in the structure will be done. If the structure ends with a plain “else”, the statements after the else will be executed. If it ends with an elif, the statements are skipped. 5.Execution always picks up on the next statement after the if structure

Semantics of elif

A chain of decisions Sometimes you have a series of possible values for a variable You could write the tests as separate if statements if x == “A”: print(“do A stuff”) if x == “C”: print(“do C stuff”) if x == “K”: print(“do K stuff”) But this is pretty inefficient. Every test has to be done every time, regardless of which value is in x. And people make the mistake of putting an else on only the LAST if, to “catch everything else”. It does not do that. That else goes only with the last if, not with all the if’s.

Chaining if’s together You can combine several if statements into one statement using elif if x == “A”: print(“do A stuff”) elif x == “C”: print(“do C stuff”) elif x == “K”: print(“do K stuff”) This is more efficient because the tests are executed only until one is found to be True. That branch’s statements are done and then the entire structure is exited. No more tests are done. This is also more flexible. If you choose to put a last “else:” at the end, to “catch everything else”, it does exactly that.

Caution – too many conditions People tend to put in conditions which are not required if x > 50: print(“big”) elif x <= 50: # this test is NOT required # if this elif is executed, you KNOW x must be less than # or equal to 50, you do not have to test for it # A reason it is not good code: what if you make a mistake # on second condition and leave out a branch? Example: elif x < 50: # what happened to x == 50? Summary: If your situation only has two mutually exclusive cases, use a plain if/else, not an if/elif.

If you don’t use elif You do not HAVE to use elif. It is possible to write the structure as nested if statements, but the indentations required will cause the code to be clumsy to read. elif is aligned directly under the original if Example: these two pieces of code are equivalent if x > 5: print(“big”) else: if x > 0: print(“medium”) else: print(“small”) if x > 5: print(“big”) elif x > 0: print(“medium”) else: print(“small”)