Flow Control. So Far: Writing simple statements that get executed one after another.

Similar documents
PRG PROGRAMMING ESSENTIALS. Lecture 2 Program flow, Conditionals, Loops

Selection statements. CSE 1310 Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

CS Introduction to Data Structures How to Parse Arithmetic Expressions

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

Flow Control: Branches and loops

Flow of Control. Flow of control The order in which statements are executed. Transfer of control

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed

CS 115 Lecture. Boolean logic Taken from notes by Dr. Neil Moore

Conditional Expressions and Decision Statements

DEBUGGING TIPS. 1 Introduction COMPUTER SCIENCE 61A

WEEK 4 OPERATORS, EXPRESSIONS AND STATEMENTS

Selection Statement ( if )

CS106X Handout 03 Autumn 2012 September 24 th, 2012 Getting Started

CSE 115. Introduction to Computer Science I

CS 115 Lecture 8. Selection: the if statement. Neil Moore

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

Information Science 1

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

Lecture 4 8/24/18. Expressing Procedural Knowledge. Procedural Knowledge. What are we going to cover today? Computational Constructs

Lab1. Introduction to Python. Lab 4: Selection Statement. Eng. Mai Z. Alyazji

Chapter 17. Fundamental Concepts Expressed in JavaScript

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

Control, Quick Overview. Selection. Selection 7/6/2017. Chapter 2. Control

Operators & Expressions

Control Structures. Code can be purely arithmetic assignments. At some point we will need some kind of control or decision making process to occur

Module 3 SELECTION STRUCTURES 2/15/19 CSE 1321 MODULE 3 1

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

Introduction to Conditionals

Fundamentals. Fundamentals. Fundamentals. We build up instructions from three types of materials

Python Programming: An Introduction To Computer Science

egrapher Language Reference Manual

Lecture 3 (02/06, 02/08): Condition Statements Decision, Operations & Information Technologies Robert H. Smith School of Business Spring, 2017

Boolean Algebra A B A AND B = A*B A B A OR B = A+B

Software Design & Programming I

Overview: Programming Concepts. Programming Concepts. Names, Values, And Variables

Overview: Programming Concepts. Programming Concepts. Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript

Lecture 4. Conditionals and Boolean Expressions

Computational Expression

CS313D: ADVANCED PROGRAMMING LANGUAGE

Program Development. Chapter 3: Program Statements. Program Statements. Requirements. Java Software Solutions for AP* Computer Science A 2nd Edition

Python for Non-programmers

Lecture 8. Conditionals & Control Flow

Chapter 3: Program Statements

Python for Informatics

Chapter 4: Making Decisions

Outline. 1 If Statement. 2 While Statement. 3 For Statement. 4 Nesting. 5 Applications. 6 Other Conditional and Loop Constructs 2 / 19

Python Programming: An Introduction To Computer Science

Conditionals and Loops

Chapter 4: Making Decisions

Introduction to Computer Programming CSCI-UA 2. Review Midterm Exam 1

Announcements. Homework 0: using cin with 10/3 is NOT the same as (directly)

1 Truth. 2 Conditional Statements. Expressions That Can Evaluate to Boolean Values. Williams College Lecture 4 Brent Heeringa, Bill Jannen

CS100: CPADS. Decisions. David Babcock / Don Hake Department of Physical Sciences York College of Pennsylvania

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Conditionals. C-START Python PD Workshop

Control and Environments Fall 2017 Discussion 1: August 30, Control. If statements. Boolean Operators

CMSC201 Computer Science I for Majors

CS 61A Control and Environments Spring 2018 Discussion 1: January 24, Control. If statements. Boolean Operators

Logic Design: Part 2

Conditions, logical expressions, and selection. Introduction to control structures

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

Lesson 38: Conditionals #2 (W11D3)

CONTROL AND ENVIRONMENTS 1

Conditionals & Control Flow

Chapter 3: Operators, Expressions and Type Conversion

COMP-202: Foundations of Programming. Lecture 3: Boolean, Mathematical Expressions, and Flow Control Sandeep Manjanna, Summer 2015

Lecture 5: Making Decisions

Formal Methods of Software Design, Eric Hehner, segment 1 page 1 out of 5

Relational Expressions. Boolean Expressions. Boolean Expressions. ICOM 4036 Programming Languages. Boolean Expressions

CONTROL AND HIGHER ORDER FUNCTIONS 1

Information Science 1

Boolean Expressions. So, for example, here are the results of several simple Boolean expressions:

Introduction to Computer Programming for Non-Majors

BRANCHING if-else statements

Announcements. HW0 is posted on schedule, due next Friday at 9pm (pretty easy)

Lecture 4 CSE July 1992

The Three Rules. Program. What is a Computer Program? 5/30/2018. Interpreted. Your First Program QuickStart 1. Chapter 1

Python - Conditional Execution

Note that in this definition, n + m denotes the syntactic expression with three symbols n, +, and m, not to the number that is the sum of n and m.

cs1114 REVIEW of details test closed laptop period

Decision Structures. Selection. Selection options (in Java) Plain if s (3 variations) Each action could be any of: if else (3 variations)

CME 193: Introduction to Scientific Python Lecture 1: Introduction

Internet & World Wide Web How to Program, 5/e by Pearson Education, Inc. All Rights Reserved.

2/5/2018. Expressions are Used to Perform Calculations. ECE 220: Computer Systems & Programming. Our Class Focuses on Four Types of Operator in C

CIS 110: Introduction to Computer Programming

Python Evaluation Rules

C ONTROL AND H IGHER O RDER F UNCTIONS

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods

C/C++ Programming Lecture 7 Name:

Comparing Data. Comparing Floating Point Values. Comparing Float Values. CS257 Computer Science I Kevin Sahr, PhD

Mathematical Logic Part One

Flow of Control: Programs can control the order in which their instructions are executed. Four types of flow:

Variables and Values

DECISION STRUCTURES: USING IF STATEMENTS IN JAVA

LECTURE 04 MAKING DECISIONS

Introduction to Visual Basic and Visual C++ Arithmetic Expression. Arithmetic Expression. Using Arithmetic Expression. Lesson 4.

Transcription:

Flow Control So Far: Writing simple statements that get executed one after another.

Flow Control So Far: Writing simple statements that get executed one after another. Flow control allows the programmer to specify how the program should act depending on some criteria.

Booleans A boolean is a value that is either True or False.

Booleans A boolean is a value that is either True or False. Can you think of an expression that would use booleans?

Booleans A boolean is a value that is either True or False. Can you think of an expression that would use booleans? Python has a data type for boolean values.

Booleans A boolean is a value that is either True or False. Can you think of an expression that would use booleans? Python has a date type for boolean values. Python boolean values are written as True or False (notice the capitalization)

Booleans >>> file_exists = True >>> file_exists True >>> file_exists = False >>> file_exists False

Comparison Operators Allow us to compare values of variables, literals, or expressions and return a boolean value (lower precedence than arithmetic operators). == equals!= not equals < less than > greater than <= less than or equal >= greater than or equal

Comparison (equals) p == q returns True - if operands (variables p and q in these examples) are equal False - if operands are not equal Think of it as a question... (is p equal to q?) Do not confuse this with assignment (single equals '=') where we are setting values equal to eachother p = q, is not the same as p == q p = q sets the variable p to the variable q p == q returns whether p is equal to q <see example3-0.py>

Comparison (others) Nothing worth mentioning for the other standard types. We will learn about two more later (is/in). Multiple comparisons: x < y < z Is expanded by python to match standard arithmetic operation: x < y and y < z So let's talk about and.

Boolean Logic Operators We've talked about arithmetic operators, and comparison operators, but python provides boolean logic operators as well. Boolean logic operators return...

Boolean Logic Operators We've talked about arithmetic operators, and comparison operators, but python provides boolean logic operators as well. Boolean logic operators return... booleans!

Boolean Logic Operators Negation: not And: and Or: or Examples: p and q P or q not p

Negation not p returns the opposite of p True - if p is False False - if p is True <see example3-0.py>

And p and q returns True - if both operands are true False - if either operand is false <see example3-0.py>

Or p or q returns True - if either operand is true False - if neither operand is true <see example3-0.py>

Truth Tables p q p and q p or q not p not q p and not q True True True False False True False False

Truth Tables p q p and q p or q not p not q p and not q True True True True False False True False False

Truth Tables p q p and q p or q not p not q True True True True False False False True False False False

Truth Tables p q p and q p or q not p not q (p and q) or (p and not q) True True True True False False False True False False False False

Truth Tables p q p and q p or q not p not q (p and q) or (p and not q) True True True True False False False True False False False False

Truth Tables p q p and q p or q not p not q (p and q) or (p and not q) True True True True True False False False True False False False False

Truth Tables p q p and q p or q not p not q (p and q) or (p and not q) True True True True True False False True False True False False False False

Truth Tables p q p and q p or q not p not q (p and q) or (p and not q) True True True True True False False True False True False True False False False

Truth Tables p q p and q p or q not p not q (p and q) or (p and not q) True True True True True False False True False True False True False False False False

Truth Tables p q p and q p or q not p not q p and not q (p and q) or (p and not q) True True True True False True False False True False True False True False False False False

Truth Tables p q p and q p or q not p not q p and not q (p and q) or (p and not q) True True True True False False True False False True False True False True False True True False False False False False True True

Truth Tables p q p and q p or q not p not q p and not q (p and q) or (p and not q) True True True True False False False True False False True False True False True False True True False False False False False True True

Truth Tables p q p and q p or q not p not q p and not q (p and q) or (p and not q) True True True True False False False True False False True False True True False True False True True False False False False False True True

Truth Tables p q p and q p or q not p not q p and not q (p and q) or (p and not q) True True True True False False False True False False True False True True False True False True True False False False False False False True True

Truth Tables p q p and q p or q not p not q p and not q (p and q) or (p and not q) True True True True False False False True False False True False True True False True False True True False False False False False False True True False

Truth Tables p q p and q p or q not p not q p and not q (p and q) or (p and not q) True True True True False False False True True False False True False True True False True False True True False False False False False False True True False

Truth Tables p q p and q p or q not p not q p and not q (p and q) or (p and not q) True True True True False False False True True False False True False True True True False True False True True False False False False False False True True False

Truth Tables p q p and q p or q not p not q p and not q (p and q) or (p and not q) True True True True False False False True True False False True False True True True False True False True True False False False False False False False True True False

Truth Tables p q p and q p or q not p not q p and not q (p and q) or (p and not q) True True True True False False False True True False False True False True True True False True False True True False False False False False False False True True False False

Complex Boolean Expressions Just as you can have multiple arithmetic operators in a given expression (3+4-3*2), you can have multiple boolean operators in an expression (False and not False or True) Order of operations (comparison operations are done before any of these), highest priority first: 1st: not 2nd: and 3rd: or <see example3-0.py>

Complex Boolean Expressions Style: Use parenthesis to show order of operations p and q or p and not q (p and q) or (p and not q) Never have to remember order of operations Makes switching languages easier Always use parenthesis around equivalence operators Exception: avoid parenthesis when using same operator over and over: True or False or False or True <see example3-0.py>

Short-Circuit Evaluation As soon as python can determine the outcome of a boolean logic operator (not comparison operators) it returns the result Faster code False and Anything: is always False True or Anything: is always True Python will not execute the code in 'Anything' True or (p and q) (p and q) is never run False and (p or q) (p or q) is never run Can lead to bugs if you rely on code in 'Anything' being run

if statements We can use all these boolean operators to control the flow of the program. In other words, we can have the program make decisions based the current state of the program The 'if' statement looks like: if <condition>: <statement>... <statement> <See example3-1.py>

Indention and Code Blocks You'll notice that the lines after the if statement are indented: if <condition>: <statement>... <statement> The indented code is called a 'block' of code. This block of code is run only if <condition> is True. Otherwise this entire 'block' of code is skipped.

if-else statements We can use all these boolean operators to control the flow of the program. In other words, we can have the program make decisions based on these operators. The if-else statement looks like: if <condition>: <statements> else: <statements> <See example3-1.py>

if-elif statements We can use all these boolean operators to control the flow of the program. In other words, we can have the program make decisions based on these operators. The if-elif statement looks like: if <condition>: <statements> elif <condition>: # repeat as many time as needed <statements> else: # this block is optional <statements> <See example3-1.py>

Conditional expressions We can also do a shorthand version of this that allows assignment: even = True if (x%2 == 0) else False Sets even to True if x is divisible by 2 otherwise, sets even to False.