Conditional Statements Introduction to Computing Science and Programming I.

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

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.
5.04 Apply Decision Making Structures
Python Programming Language
CS0007: Introduction to Computer Programming
Introduction to Computing Science and Programming I
Conditional statements and Boolean expressions. The if-statement in Java (1) The if-statement is a conditional statement The statement is executed only.
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 Sections 1.25, Control Structures o All code thus far executes every line of code sequentially o We want to be able to repeat,
Complexity (Running Time)
The If/Else Statement, Boolean Flags, and Menus Page 180
INTRODUCTION TO PYTHON PART 3 - LOOPS AND CONDITIONAL LOGIC CSC482 Introduction to Text Analytics Thomas Tiahrt, MA, PhD.
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.
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
Branching and Conditions CSIS 1595: Fundamentals of Programming and Problem Solving 1.
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.
CPS120: Introduction to Computer Science Decision Making in Programs.
Lecture 10 – Boolean expressions, if statements COMPSCI 101 Principles of Programming.
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
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.
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Chapter 3: Branching and Program Flow CSCI-UA 0002 – Introduction to Computer Programming Mr. Joel Kemp.
Python Conditionals chapter 5
Decision Structures, String Comparison, Nested Structures
CONDITIONALS. Boolean values Boolean value is either true or false It is name after the British mathemetician, George Boole who first formulated Boolean.
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)
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 3 Decision Trees Conditionals.
CPS120: Introduction to Computer Science Decision Making in Programs.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Program Flow Control Addis Ababa Institute of Technology Yared Semu April 2012.
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.
PH2150 Scientific Computing Skills Control Structures in Python In general, statements are executed sequentially, top to bottom. There are many instances.
 Type Called bool  Bool has only two possible values: True and False.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
Introduction to Decision Structures and Boolean Variables
Control Flow (Python) Dr. José M. Reyes Álamo.
Introduction to Python
Selection and Python Syntax
Python: Control Structures
IF statements.
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,
Topics The if Statement The if-else Statement Comparing Strings
Conditions and Ifs BIS1523 – Lecture 8.
Types, Truth, and Expressions (Part 2)
Class 12.
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions (Part 2)
Logical Operations In Matlab.
3. Decision Structures Rocky K. C. Chang 19 September 2018
Introduction to Programming Using Python PART 2
Selection Statements.
Introduction to Decision Structures and Boolean Variables
Types, Truth, and Expressions (Part 2)
PYTHON: BUILDING BLOCKS Sequencing & Selection
Programming Concepts and Database
Understanding Conditions
CHAPTER 5: Control Flow Tools (if statement)
Types, Truth, and Expressions
Types, Truth, and Expressions (Part 2)
Presentation transcript:

Conditional Statements Introduction to Computing Science and Programming I

Conditional Statements So far we have only talked about writing statements that are executed one after another from top to bottom So far we have only talked about writing statements that are executed one after another from top to bottom This simply isn’t sufficient to write very many useful programs This simply isn’t sufficient to write very many useful programs

Simple Guessing Game Here is an algorithm for a very simple guessing game. Here is an algorithm for a very simple guessing game. write “Think of a number between 1 and 10.” set guess to 6. write “Is your number equal to guess?” read answer if answer is “yes”, then write “I got it right!” if answer isn’t “yes”, then write “Nuts.” write “That’s the end of the game.”

Simple Guessing Game Looking at this algorithm we see that there are statements that will be run under certain conditions, but not others. Looking at this algorithm we see that there are statements that will be run under certain conditions, but not others. if answer is “yes”, then write “I got it right!” if answer isn’t “yes”, then write “Nuts.”

Simple Guessing Game Depending on the value of answer we will do one of two different things. Depending on the value of answer we will do one of two different things. If you want to write code like this that will execute if a certain condition is true, you use an if structure If you want to write code like this that will execute if a certain condition is true, you use an if structure

If Structure if condition: statement1statement2statement3 If the condition is satisfied when execution reaches the if statement, then all statements that are indented after it will be executed. If the condition is not satisfied then they will be skipped and execution will continue after the indented statements If the condition is satisfied when execution reaches the if statement, then all statements that are indented after it will be executed. If the condition is not satisfied then they will be skipped and execution will continue after the indented statements

Blocks of Code Python uses indentation to indicate blocks of code. Python uses indentation to indicate blocks of code. For an if statement we indent every statement in the block of code that will be executed based on the result of the if For an if statement we indent every statement in the block of code that will be executed based on the result of the if if x==1: print “first line of code block” print “last line of code block” print “first line outside of code block”

Blocks of Code Blocks of Code can be within other blocks of code Blocks of Code can be within other blocks of code 1 if x > 5: 2 print “x is greater than 5” 3 if x > 10: 4 print “x is also greater than 10” Lines 2, 3, and 4 are the block of code for the first if statement. Line 4 is the block of code for the second if statement Lines 2, 3, and 4 are the block of code for the first if statement. Line 4 is the block of code for the second if statement

Guessing Game Here is how the guessing game is translated into Python Here is how the guessing game is translated into Python print "Think of a number between 1 and 10.“ guess = 6 answer = raw_input("Is your number equal to “ + \ str(guess) + "? ") if answer == "yes": print "I got it right!" if answer != "yes": print "Nuts." print "That's the end of the game."

The bool Data Type Before this point all of the expressions we have used evaluate to either a string, integer, or float. Before this point all of the expressions we have used evaluate to either a string, integer, or float. For the if statement we have a condition that Python will evaluate to be either True or False. These conditions are boolean expressions for which we have the bool data type For the if statement we have a condition that Python will evaluate to be either True or False. These conditions are boolean expressions for which we have the bool data type

The bool Data Type For the guessing game we have two conditional statements that contain simple boolean expressions For the guessing game we have two conditional statements that contain simple boolean expressions answer == “yes” answer == “yes” Evaluates to True if answer is equal to “yes” Evaluates to True if answer is equal to “yes” answer != “yes” answer != “yes” Evaluates to True if answer is not equal to “yes” Evaluates to True if answer is not equal to “yes”

Boolean Operators Comparison Operators Comparison Operators ==, is equal to ==, is equal to This is different from = This is different from = x=5, assigns x the value 5 x=5, assigns x the value 5 x==5, a boolean expression that evaluates to True if x is equal to 5, False if is not x==5, a boolean expression that evaluates to True if x is equal to 5, False if is not !=, is not equal to !=, is not equal to Returns the opposite of == Returns the opposite of == x!=5 is true if x does not equal 5 x!=5 is true if x does not equal 5, = all act as you’d expect, = all act as you’d expect

Boolean Operators Logical Operators Logical Operators and, & and, & A and B is true if both A and B are True A and B is true if both A and B are Truex=5 (x > 1 and x 1 and x < 4) is False or, | or, | A or B is true if either A or B is True A or B is true if either A or B is Truex=5 (x > 1 or x 1 or x < 4) is True not, ! not, ! not A is true if A is False not A is true if A is Falsex=5 not x < 4 is True

Boolean Variables Since bool is a data type, you can store a bool value into a variable as with any other data type Since bool is a data type, you can store a bool value into a variable as with any other data type x=int(raw_input(“Enter an integer between 0 and 10: ”)) xIsInCorrectRange = (x >= 0 and x = 0 and x <= 10) if not xIsInCorrectRange: print “You didn’t enter an integer between 0 and 10” print “You didn’t enter an integer between 0 and 10” print “I’m giving you the number 5” print “I’m giving you the number 5” x=5 x=5 print “Your number is “ + str(x) + “.”

else Clause if condition: code block1 else: code block2 If the condition is satisfied when execution reaches the if statement, then code block1 will be executed. Otherwise, code block2 will be executed If the condition is satisfied when execution reaches the if statement, then code block1 will be executed. Otherwise, code block2 will be executed

else Clause In our guessing game example we can simplify the code using an else clause instead of a second if statement In our guessing game example we can simplify the code using an else clause instead of a second if statement We have this, but we know that if the first condition is false the second is true We have this, but we know that if the first condition is false the second is true if answer == “yes”: print “I got it right.” if answer != “yes”: print “Nuts.” We can use an else clause. If the condition in the if statement is false execution will move to the associated else clause. We can use an else clause. If the condition in the if statement is false execution will move to the associated else clause. if answer == “yes”: print “I got it right.” else: print “Nuts.”

elif Clauses elif is how Python shortens else-if elif is how Python shortens else-if elif clauses can be used if there are multiple related conditions you want to check for and execute different code depending on which one is true elif clauses can be used if there are multiple related conditions you want to check for and execute different code depending on which one is true

elif Clauses if condition1: code block1 elif condition2: code block2..else: code block If condition1 is true, code block1 will be executed and the other clauses will be skipped, otherwise if condition2 is true, code block2 will be executed, and so on. If none of the conditions are true and there is an else clause, the code block for that clause will be executed. You can use as many elif clauses as needed. If condition1 is true, code block1 will be executed and the other clauses will be skipped, otherwise if condition2 is true, code block2 will be executed, and so on. If none of the conditions are true and there is an else clause, the code block for that clause will be executed. You can use as many elif clauses as needed.

elif Clauses One more extension of the guessing game using an elif clause. One more extension of the guessing game using an elif clause. if answer == "yes": print "I got it right!" elif answer == "no": print "Nuts." else: print "You must answer ’yes’ or ’no’.“

elif Clauses Remember that only the first elif clause whose condition is satisfied will have its block of code executed. Remember that only the first elif clause whose condition is satisfied will have its block of code executed. weight = int(raw_input("How many grams does the object weight? ")) if weight >1000: print "It weighs " + str(weight/1000.0) + " kilograms." print "It weighs " + str(weight/1000.0) + " kilograms." elif weight > 100: print "It weighs " + str(weight/100.0) + " hectograms." print "It weighs " + str(weight/100.0) + " hectograms." elif weight > 10: print "It weighs " + str(weight/10.0) + " dekagrams." print "It weighs " + str(weight/10.0) + " dekagrams." elif weight >= 0: print "It weighs " + str(weight) + " grams." print "It weighs " + str(weight) + " grams."else: print "You entered an invalid number." print "You entered an invalid number."

elif Clauses Easy way to set up a simple menu. Easy way to set up a simple menu. print “1. Create a new file” print “2. Open a file” print ”3. Exit” x = int(raw_input(“Select an option: “)) if x==1:.. elif x==2:.. elif x==3:..else: print “You didn’t enter a valid option.”