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.

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

Chapter 4 - Control Statements
Chapter 4 Decision Making Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E. Reingold.
Logical Operators and While Loops CS303E: Elements of Computers and Programming.
Selection Victor Norman CS104 Calvin College. Reading Quiz Counts toward your grade.
ECS 15 if and random. Topic  Testing user input using if statements  Truth and falsehood in Python  Getting random numbers.
Python Programming Language
DECISION MAKING STRUCTURES Computer Programming 2.
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.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
Python (yay!) November 16, Unit 7. Recap We can store values in variables using an assignment statement >>>x = We can get input from the user using.
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.
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,
Geography 465 Assignments, Conditionals, and Loops.
Python Control of Flow.
Chapter 9 IF Statement Bernard Chen. If Statement The main statement used for selecting from alternative actions based on test results It’s the primary.
Chapter 9 IF Statement Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012.
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
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.
Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout
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.
CSE1222: Lecture 6The Ohio State University1. Common Mistakes with Conditions (1)  Consider the following code: int age(26); if (age = 18) { cout
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
Conditionals CS 103 February 16, Blast from the Past: C14 Dating Problem Statement: Calculate the age of a fossil from its C-14 radioactivity Problem.
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.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Chapter 8: MuPAD Programming I Conditional Control and Loops MATLAB for Scientist and Engineers Using Symbolic Toolbox.
CONDITIONALS. Boolean values Boolean value is either true or false It is name after the British mathemetician, George Boole who first formulated Boolean.
Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 11 Using strings and sequences 5/02/09 Python Mini-Course: Day 3 – Lesson.
Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 10 Iteration: Loops 05/02/09 Python Mini-Course: Day 3 - Lesson 10 1.
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
Student Q and As from 5.1 – 5.7, 5.11 Students of CS104, Fall 2013 (and me)
Shell script – part 2 CS 302. Special shell variable $0.. $9  Positional parameters or command line arguments  For example, a script myscript take 2.
If statement.  It is made up of three main components:  The keyword itself,  an expression that is tested for its truth value,  and a code suite to.
Python Basics  Values, Types, Variables, Expressions  Assignments  I/O  Control Structures.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
USING CONDITIONAL CODE AMIR KHANZADA. Conditional Statement  Conditional statements are the set of commands used to perform different actions based on.
Chapter 3. Control Structure C++ Programing. Conditional structure C++ programming language provides following types of decision making statements. Click.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Today… Operators, Cont. Operator Precedence Conditional Statement Syntax. Winter 2016CISC101 - Prof. McLeod1.
Silberschatz and Galvin  C Programming Language Decision making in C Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University.
 Type Called bool  Bool has only two possible values: True and False.
Python Basics.
Control Flow (Python) Dr. José M. Reyes Álamo.
Making Choices with if Statements
C-Language Lecture By B.S.S.Tejesh, S.Neeraja
Basic operators - strings
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions (Part 2)
Program Flow Control Selection & repetition
Selection Statements.
Computer Science Core Concepts
Nate Brunelle Today: Conditional Decision Statements
Adding Intelligence Check for the presence or absence of a condition in the environment Take the appropriate actions Involves making a choice (to do or.
Types, Truth, and Expressions (Part 2)
Python Programming Language
Nate Brunelle Today: Conditional Decision Statements
ECS15 boolean.
Winter 2019 CISC101 4/16/2019 CISC101 Reminders
CHAPTER 5: Control Flow Tools (if statement)
Control Statements.
Types, Truth, and Expressions
Nate Brunelle Today: Conditional Decision Statements
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.
Types, Truth, and Expressions (Part 2)
Presentation transcript:

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 just if statements. There are 3 conditional commands: There are 3 conditional commands: –if –elif –else

Syntax if expression: statement elif expression: statement statementelse:statement

Example x = 5 if x < 0: print "x is negative" print "x is negative" elif x % 2: print " x is positive and odd" print " x is positive and odd"else: print "x is even and non-negative" print "x is even and non-negative"

To check if something is true the only thing that needs to be done is To check if something is true the only thing that needs to be done is if x: Nothing else is needed Nothing else is needed

True and False can be used to define boolean variables True and False can be used to define boolean variables All of the normal conditional operators are present: “ ”, “==“, “!=“, “ =“ All of the normal conditional operators are present: “ ”, “==“, “!=“, “ =“ And, Or and XOr comparisons are used as well: And is a single &, Or is a single |, and XOr is a single ^. And, Or and XOr comparisons are used as well: And is a single &, Or is a single |, and XOr is a single ^. In a chain of & statements, if the first comparison is false then the second comparison is not conducted. In a chain of & statements, if the first comparison is false then the second comparison is not conducted.