Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)

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

CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay.
Project on VB Control Structures
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.
Conditional Statements Introduction to Computing Science and Programming I.
Overview Program flow Decision / branching / selection structures True & False in Python Comparison operators & Boolean expressions if … if … else if …
Week 10 Recap CSE 115 Spring For-each loop When we have a collection and want to do something to all elements of that collection we use the for-each.
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control Loops in Java.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
INTRODUCTION TO PYTHON PART 3 - LOOPS AND CONDITIONAL LOGIC CSC482 Introduction to Text Analytics Thomas Tiahrt, MA, PhD.
Chapter 2 Control. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Repetition, quick overview.
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
Chapter 2 Control.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Python Control Flow statements There are three control flow statements in Python - if, for and while.
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.
1. We’ve learned that our programs are read by the compiler in order, from top to bottom, just as they are written The order of statement execution is.
© 2004 Pearson Addison-Wesley. All rights reserved February 17, 2006 The ‘while’ Statement ComS 207: Programming I (in Java) Iowa State University, SPRING.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
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.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Repetition Intro to Computer Science CS1510 Dr. Sarah Diesburg.
8. DECISION STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
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.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Python Flow of Control CS 4320, SPRING Iteration The ‘for’ loop is good for stepping through lists The code below will print each element in the.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
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.
CMSC201 Computer Science I for Majors Lecture 07 – While Loops
Control Flow (Python) Dr. José M. Reyes Álamo.
Chapter 4 Repetition Statements (loops)
Chapter 3: Decisions and Loops
Selection and Python Syntax
Python: Control Structures
Topics The if Statement The if-else Statement Comparing Strings
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Topics The if Statement The if-else Statement Comparing Strings
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Types, Truth, and Expressions (Part 2)
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions (Part 2)
3 Control Statements:.
Repetition Control Structure
Chapter 8: More on the Repetition Structure
3. Decision Structures Rocky K. C. Chang 19 September 2018
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Introduction to Programming Using Python PART 2
Selection Statements.
Types, Truth, and Expressions (Part 2)
CHAPTER 5: Control Flow Tools (if statement)
Types, Truth, and Expressions
REPETITION Why Repetition?
Types, Truth, and Expressions
Presentation transcript:

Control Flow (Python) Dr. José M. Reyes Álamo

2 Control Flow Sequential statements Decision statements Repetition statements (loops)

3 Sequential Statements Statements are executed one after the other in descending order

4 Relational Operators Less than: < Greater than: > Equal to: == (Not the same as = ) Not equal to: != Less than or equal to: <= Greater than or equal to: >=

5 Logical Operator AND – result is True if both conditions are true, False otherwise –i.e. x > 0 and x < 10 OR – result is True if either condition is true, False otherwise –i.e. n == ‘a’ or n == ‘b’ NOT – Negates a Boolean expression –i.e. not (y < 5)

6 Selection Statements (Conditional) Selection statements allows a program to make choices Evaluate the Boolean condition (rendering True or False) If Boolean expression is True, execute all statements in the body

7

8 Warning About Indentation Elements of the body must all be indented the same number of spaces or tabs Python only recognizes the body when the lines of code are indented at the same level

9 Python Selection, Round 2 if Boolean expression: bodyTrue else: bodyFalse Python evaluates the Boolean expression: If expression is True, runs bodyTrue If expression is False, runs bodyFalse

10 Python Selection, Chained Conditionals if Boolean expression 1: body1 elif Boolean expression 2 : body2 else : bodyFalse Python evaluates the Boolean expression 1: If expression 1 is True, runs body1 If expression 1 is False, checks expression 2 If expression 2 is True, runs body2 If expression2 is False, runs bodyFalse

11 Chained Conditionals Example if x < y: print 'x is less than y' elif x > y: print 'x is greater than y' else: print 'x and y are equal'

12 Python Selection, Nested Conditionals if Boolean expression 1: if Boolean expression 2 : body2 else : body2False else: body1False Python evaluates the Boolean expression 1: If expression 1 is True, it evaluates expression 2 If expression 2 is True, executes body2 If expression 2 is False, executes body2False If expression1 is False, runs body1False

13 Nested Conditionals Example if x == y: print 'x and y are equal' else: if x < y: print 'x is less than y' else: print 'x is greater than y'

Repetition: A Quick Overview

15 Repetition Statements (Loops) Besides selecting which statements to execute, a fundamental need in a program is repetition –repeat a set of statements under certain conditions With sequential, selection, and repetition, we have the fundamental programming statements

16 While and For Statements The while loop is the general repetition statement. It repeats a set of statements while the condition is True. The for loop is useful for iterating through the elements of data structure, one at a time. –We will study the for loop later on.

17 while Loop Characteristics: –test the Boolean expression before entering the loop –test the Boolean expression before each iteration of the loop Syntax: while Boolean expression: loop body

18

19 Repeat While the Boolean Expression is True while loop will repeat the statements in the body while the Boolean expression is True If the Boolean expression never changes during the course of the loop, the loop will continue forever. The Practice of Computing Using Python, Punch, Enbody, © 2011 Pearson Addison-Wesley. All rights reserved

20 Example

21 General Approach to a while Loop Outside the loop: initialize the Boolean expression Inside the loop: perform some operations which changes the state of the program, eventually leading the Boolean expression to become False, exiting the loop Need them both!

22 OpenLab and Blackboard Check OpenLab for any new lab. Check Blackboard for any new quiz.