Chapter 9 IF Statement Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012.

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

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 8P. 1Winter Quarter Control Statements Lecture.
Exercise (1).
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
ECS 15 if and random. Topic  Testing user input using if statements  Truth and falsehood in Python  Getting random numbers.
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.
Algorithms CSC1310 Fall What Is Programming? Programming Programming means writing down a series of instructions that tell a computer what to do.
Introduction to Computing Science and Programming I
Pascal Programming Today Chapter 4 1 »Conditional statements allow the execution of one of a number of possible operations. »Conditional statements include:
Conditional Statements Introduction to Computing Science and Programming I.
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 & Relational Operators Programming.
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.
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
Intro to Robots Conditionals and Recursion. Intro to Robots Modulus Two integer division operators - / and %. When dividing an integer by an integer we.
INTRODUCTION TO PYTHON PART 3 - LOOPS AND CONDITIONAL LOGIC CSC482 Introduction to Text Analytics Thomas Tiahrt, MA, PhD.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/33 Conditionals and Loops Now we will examine programming statements.
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.
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
ECS 10 10/8. Outline Announcements Homework 2 questions Boolean expressions If/else statements State variables and avoiding sys.exit(…) Example: Coin.
PYTHON CONDITIONALS AND RECURSION : CHAPTER 5 FROM THINK PYTHON HOW TO THINK LIKE A COMPUTER SCIENTIST.
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.
1 Lecture 5: Selection Structures. Outline 2  Control Structures  Conditions  Relational Operators  Logical Operators  if statements  Two-Alternatives.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
Course A201: Introduction to Programming 09/16/2010.
Decision Making CMSC 201. Overview Today we will learn about: Boolean expressions Decision making.
Chapter 3: Branching and Program Flow CSCI-UA 0002 – Introduction to Computer Programming Mr. Joel Kemp.
Python Conditionals chapter 5
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Python 101 Dr. Bernard Chen University of Central Arkansas PyArkansas.
Decision Structures and Boolean Variables. Sequence Structures Thus far, we’ve been programming “sequence structures” Thus far, we’ve been programming.
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers.
September 7, 2004ICP: Chapter 3: Control Structures1 Introduction to Computer Programming Chapter 3: Control Structures Michael Scherger Department of.
8. DECISION STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Decision Making CMSC 201 Chang (rev ).
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 3 Decision Trees Conditionals.
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.
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)
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.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #003 (February 14, 2015)
Multi-Selection If-elsif Statement.  In fact, it’s everything in between  Yesterday we learned how to add a control statement that gave us two path.
Types, Truth, and Expressions Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Types, Truth, and Expressions Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
 Type Called bool  Bool has only two possible values: True and False.
Control Flow (Python) Dr. José M. Reyes Álamo.
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Topics The if Statement The if-else Statement Comparing Strings
If, else, elif.
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
Topics The if Statement The if-else Statement Comparing Strings
Learning to Program in Python
Chapter 4: Decision Structures and Boolean Logic
Visual Basic – Decision Statements
Introduction to Programming Using Python PART 2
Chapter 3: Selection Structures: Making Decisions
CHAPTER 5: Control Flow Tools (if statement)
Chapter 3: Selection Structures: Making Decisions
Conditionals and Loops
Python Conditionals: The if statement
Chapter 4: Decision Structures and Boolean Logic
Control Structures.
Types, Truth, and Expressions
Control 9 / 25 / 19.
Presentation transcript:

Chapter 9 IF Statement Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012

Outline Logic expression General concept behind the Python statement syntax IF statement

Some Logic Expression Computer only understand ‘1’ and ‘0’ In computer logic: 1: true 0: false

Some Logic Expression Equal: “==” NOT Equal: “!=” Greater: “>”, “>=” Less than: “<”, “<=”

Some Logic Expression example >>> a=10 >>> b=10 >>> a==b >>> a=10 >>> b=5 >>> a==b

Some Logic Expression example >>> a=10 >>> b=10 >>> a!=b >>> a=10 >>> b=5 >>> a!=b

Some Logic Expression example >>> a=10 >>> b=10 >>> a>=b >>> a=10 >>> b=5 >>> a<b

Outline Logic expression General concept behind the Python statement syntax IF statement

Python’s Syntax In general, Python has a simple syntax: Block and statement boundaries are detected automatically: python use indention of statement under a header to group the statement Header --- ‘:’

Outline Logic expression General concept behind the Python statement syntax IF statement Simple If statement If... else statement If… else if … else statement

If Statement The main statement used for selecting from alternative actions based on test results It’s the primary selection tool in Python and represents the Logic process

Simple If statement It takes the form of an if test if :

Simple If statement if 4>3: print “it is true that 4>3” if 1: print “true” a=10 if a==10: print “a=10”

Truth Statement In Python: True means any nonzero number or nonempty object False means not true: a zero number of empty object

If Statement “login” example

If... else statement It takes the form of an if test, and ends with an optional else block if : else:

If Statement “login” example

If... else statement >>> a=10 >>> b=5 >>> if a>b: print “a is larger” else: print “b is larger”

If... else statement a=10 if a==10: print “a=10” else: print “a is not equal to 10” (also try >, < )

If… else if … else statement It takes the form of an if test, followed by one or more optional elif tests, and ends with an optional else block if : elif : else:

If… else if … else statement a=10 if a>10: print “a > 10” elif a==10: print “a = 10” else: print “a < 10”

If… else if … else statement example number = 23 guess = int(raw_input('Enter an integer : ')) if guess == number: print 'Congratulations, you guessed it.' print "(but you do not win any prizes!)" elif guess < number: print 'No, it is a little higher than that' else: print 'No, it is a little lower than that'

In class practice import random number=random.randint(1,5) guess = int(raw_input('Enter an integer : ')) if guess == number: print 'Congratulations, you guessed it.' print "(but you do not win any prizes!)" elif guess < number: print 'No, it is a little higher than that' else: print 'No, it is a little lower than that' print 'The answer is:',number

Some More Logic Expression And (True if both are true) Examples: >>> 5>4 and 8>7 True >>> 5>4 and 8>9 False ABA and B

Example temp=int(raw_input("Please enter today's tempeture:")) rain=int(raw_input("Is is raining? 1:yes, 0:no")) if temp>86 and rain==0: print "Today is a hot sunny day" elif temp>86 and rain==1: print "Today is a hot raining day" elif temp<32 and rain==0: print "Today is a cold sunny day" elif temp<32 and rain==1: print "Today is a cold raining day" else: print "Today's weather is not special"

Some More Logic Expression Or (True if either one is true) Examples: >>> 5>4 or 8>7 True >>> 5>4 or 8>9 True ABA or B

Example a=10 b=20 if a%2==0 and b%2==0: print "Both a and b are even numbers" elif a%2==0 or b%2==0: print "Either a or b is an even number" else: print "Both a and b are odd numbers"

Nested IF statement Sometimes you may want to check one condition after another condition. In such situation, we call it “Nested IF Statement” The Code would looks something like:

Nested IF statement

Example: deposit=int(raw_input('Clerk: How much do you want to deposit? ')) if deposit > : print "Wow, that's a lot of money" if deposit < : print "I need to call my manager" elif deposit < : print "Let me open an VIP room for you" else: print "Can you merry me?" elif deposit > 1000: print "Sure, let me do it right now, do you want to consider a CD" elif deposit > 1: print "Sure, let me do it right now" else: print "Are you kidding me, I am busy right now..."