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.

Slides:



Advertisements
Similar presentations
INTEC CS160 - Jeanine Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 3 Control Structures and Data Files.
Advertisements

IF statement (i) Single statement. IF ( logical expression ) statement Example: read(*,*) a if (a. lt. 0) a = -a write(*,*) a Or read(*,*) a if (a < 0)
If Statements, Try Catch and Validation. The main statement used in C# for making decisions depending on different conditions is called the If statement.
Chapter 13 Control Structures. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Control Structures Conditional.
CSC 142 G 1 CSC 142 Conditionals [Reading: chapter 5]
3. S/E with Control Structures 3.1 Relational Operators and Expressions 3.2 If and if-else Statements 3.3 The Type Double 3.4 Program Design with the While.
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.
Python Programming Language
Algorithms CSC1310 Fall What Is Programming? Programming Programming means writing down a series of instructions that tell a computer what to do.
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.
Chapter 6 Horstmann Programs that make decisions: the IF command.
Decisions (Conditional Programming) Chapter 5 (Sec. 5.1 & 5.2)
Geography 465 Assignments, Conditionals, and Loops.
INTRODUCTION TO PYTHON PART 3 - LOOPS AND CONDITIONAL LOGIC CSC482 Introduction to Text Analytics Thomas Tiahrt, MA, PhD.
Chapter 9 IF Statement Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls printf(”Hello.\n”);
Python Lab: Control Statements Conditionals, Loops, Iterations Proteomics Informatics, Spring 2014 Week 4 18 th Feb, 2014
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.
PAGES:51-59 SECTION: CONTROL1 : DECISIONS Decisions.
Python Jim Eng Vote on class policy In lecture we discussed several ways in which you might show credits for content in the web pages.
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.
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.
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
Python 101 Dr. Bernard Chen University of Central Arkansas PyArkansas.
More Python!. Lists, Variables with more than one value Variables can point to more than one value at a time. The simplest way to do this is with a List.
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.
3.1.3 Program Flow control Structured programming – SELECTION in greater detail.
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.
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.
IF STATEMENTS AND BOOLEAN EXPRESSIONS. BOOLEAN EXPRESSIONS Evaluate to a value of true or false Use relational or equivalence operators Boolean operators.
 Type Called bool  Bool has only two possible values: True and False.
Control Flow (Python) Dr. José M. Reyes Álamo.
Selection: Non-linear programs
Making Choices with if Statements
Sequence, Selection, Iteration The IF Statement
Chapter 6 Conditionals.
Selection and Python Syntax
Chapter 4: Control Structures
Factoring if/else code
Topics The if Statement The if-else Statement Comparing Strings
Types, Truth, and Expressions (Part 2)
Control Structures: Selection Statement
Program Flow Control Selection & repetition
Pages:51-59 Section: Control1 : decisions
1 WRITE YOUR PRIZE HERE 2 WRITE YOUR PRIZE HERE WRITE YOUR PRIZE HERE
Visual Basic – Decision Statements
Introduction to Programming Using Python PART 2
Types, Truth, and Expressions (Part 2)
The if Statement Control structure: logical design that controls order in which set of statements execute Sequence structure: set of statements that execute.
Inequalities and Their Graphs
Nate Brunelle Today: Conditional Decision Statements
CHAPTER 5: Control Flow Tools (if statement)
Control Structures: Selection Statement
Pages:51-59 Section: Control1 : decisions
Chapter 2 Sets Active Learning Lecture Slides
Control Structures.
Presentation transcript:

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 selection tool in Python and represents the Logic process

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

Outline Simple If statement If... else statement If… else if … else statement

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”

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

If... else statement if 3>4: print “3 is larger than 4” else: print “3 is NOT larger than 4” if 0: print “true” else: print “false”

If... else statement a=10 if a==10: print “a=10” else: print “a is not equal to 10” 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.' # New block starts here print "(but you do not win any prizes!)" # New block ends here elif guess < number: print 'No, it is a little higher than that' # Another block # You can do whatever you want in a block... else: print 'No, it is a little lower than that' # you must have guess > number to reach here

Some More Logic Expression and >>> 5>4 and 8>7 True >>> 5>4 and 8>9 False

Some More Logic Expression or >>> 5>4 or 8>7 True >>> 5>4 or 8>9 True