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

Slides:



Advertisements
Similar presentations
Chapter 4 - Control Statements
Advertisements

The If Then Else Statement Using JavaScript TIP The If Statement is the fundamental control structure that allows branches in the flow of control in a.
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.
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
CS0004: Introduction to Programming Repetition – Do Loops.
Overview Program flow Decision / branching / selection structures True & False in Python Comparison operators & Boolean expressions if … if … else if …
Do Loops A Do..Loop terminates based on a condition that is specified Execution of a Do..Loop continues while a condition is True or until a condition.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
© 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.
Lecture Review (If-else Statement) if-else statement has the following syntax: if ( condition ) { statement1; } else { statement2; } The condition.
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.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
Computer Science 111 Fundamentals of Programming I Making Choices with if Statements.
ITEC113 Algorithms and Programming Techniques
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
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.
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.
Decision Structures and Boolean Variables. Sequence Structures Thus far, we’ve been programming “sequence structures” Thus far, we’ve been programming.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
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)
Decision Making CMSC 201 Chang (rev ).
Shell script – part 2 CS 302. Special shell variable $0.. $9  Positional parameters or command line arguments  For example, a script myscript take 2.
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.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Loops (While and For) CSE 1310 – Introduction to Computers and Programming 1.
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
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.
Making Choices with if Statements
Python: Control Structures
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Selection CIS 40 – Introduction to Programming in Python
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)
Repetition Control Structure
3. Decision Structures Rocky K. C. Chang 19 September 2018
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Visual Basic – Decision Statements
Introduction to Programming Using Python PART 2
Selection Statements.
Types, Truth, and Expressions (Part 2)
An Introduction to Linux
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
CHAPTER 5: Control Flow Tools (if statement)
Types, Truth, and Expressions
REPETITION Why Repetition?
Types, Truth, and Expressions (Part 2)
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 These statements are fundamental for computing

7

8 Python if Statement if Boolean expression : body Evaluate the Boolean condition (rendering True or False) If Boolean expression is True, execute all statements in the body

9 Warning About Indentation Elements of the body must all be indented the same number of spaces/tabs Python only recognizes the body when they are indented the same distance You must be careful to get the indentation right.

10 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

11 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

12 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'

13 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

14 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'

15 Example: Positive or Negative number? Write a program that ask the user to input a number and displays a message saying whether the number is positive or negative.

16 The code

Repetition: A Quick Overview

18 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

19 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 iteration, moving through all the elements of data structure, one at a time. We will study the for loop later on.

20

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

22 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

23 Example simpleloop.py Get it from the class website s=0&d=1 s=0&d=1

24

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

LAB3_Python2