CSCI/CMPE 4341 Topic: Programming in Python Chapter 4: Control Structures (Part 2) Xiang Lian The University of Texas – Pan American Edinburg, TX 78539.

Slides:



Advertisements
Similar presentations
 2002 Prentice Hall. All rights reserved Control Structures 3 control structures –Sequential structure Built into Python –Selection structure The.
Advertisements

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Introduction to working with Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
 2002 Prentice Hall. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 The if Selection Structure.
Chapter 3 - Structured Program Development
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Control Structures Outline 2.1 Introduction 2.2 Algorithms 2.3 Pseudocode 2.4 Control Structures.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
Structured programming
Introduction to Computers and Programming Lecture 8: More Loops New York University.
Introduction to Computers and Programming More Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection Statement 4.6 if else Selection Statement 4.7 while.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 3: Control Structures (Part 1) – Exercises 1 Xiang Lian The University of Texas – Pan American Edinburg,
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
Structured Program Development in C
 2002 Prentice Hall. All rights reserved. 1 Chapter 3 – Control Structures Outline 3.1 Introduction 3.2 Algorithms 3.3 Pseudocode 3.4Control Structures.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 8 - JavaScript: Control Structures I Outline 8.1 Introduction 8.2 Algorithms 8.3 Pseudocode 8.4.
The University of Texas – Pan American
 2003 Prentice Hall, Inc. All rights reserved.  2004 Prentice Hall, Inc. All rights reserved. Chapter 8 - JavaScript: Control Statements I Outline 8.1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Control Structures Outline 2.1 Introduction 2.2 Algorithms 2.3 Pseudocode 2.4 Control Structures.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Structured Program Development Outline 2.1Introduction 2.2Algorithms 2.3Pseudo code 2.4Control Structures 2.5The If Selection Structure 2.6The If/Else.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 18 - WMLScript: Control Structures II Outline 18.1 Introduction 18.2 Essentials of Counter-Controlled.
C Lecture Notes 1 Structured Program Development.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
C++ Programming Lecture 6 Control Structure II (Repetition) By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control.
Loop.  While Loop  Do-while Loop  For Loop Continue Statement Conclusion Loop Loop.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Control Structures Outline 2.1 Introduction 2.2 Algorithms 2.3 Pseudocode 2.4 Control Structures.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Control Structures: Part 1.
1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 The if Selection Structure 4.6 The if / else Selection Structure 4.7.
 2003 Prentice Hall, Inc. All rights reserved. 1 Control Structures Outline -Introduction -Algorithms -Pseudocode -Control Structures -if Selection Structure.
C Programming 2002 Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
 2003 Prentice Hall, Inc. All rights reserved. Chapter 8 - JavaScript: Control Statements I Outline 8.1 Introduction 8.2 Algorithms 8.3 Pseudocode 8.4.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 8 - JavaScript: Control Structures I Outline 8.1 Introduction 8.2 Algorithms 8.3 Pseudocode 8.4.
Structured Program Development Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010.
CSCI/CMPE 4341 Topic: Programming in Python Review: Exam I Xiang Lian The University of Texas – Pan American Edinburg, TX 78539
 2003 Prentice Hall, Inc. All rights reserved. Outline 1 fig02_07.cpp (1 of 2) 1 // Fig. 2.7: fig02_07.cpp 2 // Class average program with counter-controlled.
1 Lecture 3 Control Structures else/if and while.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Lecture 5: Stopping with a Sentinel. Using a Sentinel Problem Develop a class-averaging program that will process an arbitrary number of grades each time.
 2002 Prentice Hall. All rights reserved. 1 Chapter 4 – Control Structures Part 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures.
 2003 Prentice Hall, Inc. All rights reserved. 1 Will not cover 4.14, Thinking About Objects: Identifying Class Attributes Chapter 4 - Control Structures.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (while) Outline 3.7The While Repetition.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Control Structures Outline 2.1 Introduction 2.2 Algorithms 2.3 Pseudocode 2.4 Control Structures.
JavaScript: Control Structures I Outline 1 Introduction 2 Algorithms 3 Pseudocode 4 Control Structures 5 if Selection Structure 6 if/else Selection Structure.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
1 Chapter 4 - Control Statements: Part 1 Outline 4.1 Introduction 4.4 Control Structures 4.5 if Selection Structure 4.6 if/else Selection Structure 4.7.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
Chapter 4 – C Program Control
while Repetition Structure
Control Statements Kingdom of Saudi Arabia
The University of Texas – Pan American
Structured Program
The University of Texas – Pan American
Chapter 3 – Control Structures
2.6 The if/else Selection Structure
EPSII 59:006 Spring 2004.
Presentation transcript:

CSCI/CMPE 4341 Topic: Programming in Python Chapter 4: Control Structures (Part 2) Xiang Lian The University of Texas – Pan American Edinburg, TX

Objectives In this chapter, you will: – Learn the basic problem-solving techniques – Develop algorithms through the process of top-down, stepwise refinement – Become familiar with if, if/else and if/elif/else structures – Learn the while and for repetition structures – Understand counter-controlled and sentinel-controlled repetition – Use augmented assignment symbols and logical operators in Python – Explore the break and continue program control statement 2

Control Structure 3 control structures – Sequential structure Built into Python – Selection structure The if statement The if/else statement The if/elif/else statement – Repetition structure The while repetition structure The for repetition structure 3

Augmented Assignment Symbols Augmented addition assignment symbols – x = x +5 is the same as x += 5 – y = y + 1 is the same as y += 1 (or y++ in C++) Other math signs – The same rule applies to any other mathematical symbol *=, **=, /=, %= 4

Augmented Assignment Symbols (cont'd) 5

Essentials of Counter-Controlled Repetition Essentials – The counter A named variable to control the loop – Initial value That which the counter starts at – Increment Modifying the counter to make the loop eventually terminate – Condition The test that the counter must pass in order to continue looping 6

 2002 Prentice Hall. All rights reserved. Outline Fig03_17.py Program Output # Fig. 3.17: fig03_17.py # Counter-controlled repetition. counter = 0 while counter < 10: print (counter) counter += The counter with an initial value of zero The conditional that if the counter is above 10 the loop breaks The incrementing of the counter 7

Using the for Repetition Structure Fig. 3.21for repetition structure flowchart. Establish initial value of control variable Determine if final value of control variable has been processed more items to process True False Body of loop (this may be many statements) Update the control variable (Python does this automatically) x = first item in y print x x = next item in y 8

9 for Repetition Structure The for loop – Function range is used to create a list of values – range ( integer ) » Values go from 0 to given integer – range ( integer1, integer2) » Values go from first up to second integer – range ( integer1, integer2, integer ) » Values go from first up to second integer, but increases in intervals of the third integer – The loop will execute as many times as the value passed – for counter in range ( value ): [0, integer-1] [integer1, integer2-1] 9

 2002 Prentice Hall. All rights reserved. Outline Fig03_18.py Program Output # Fig. 3.18: fig03_18.py # Counter-controlled repetition with the # for structure and range function. for counter in range( 10 ): print (counter) Makes the counter go from zero to nine 10

for Repetition Structure (cont'd) Python 2.2b2 (#26, Nov , 11:44:11) [MSC 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> range( 10 ) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Python 2.2b2 (#26, Nov , 11:44:11) [MSC 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> range( 10, 0, -1 ) [10, 9, 8, 7, 6, 5, 4, 3, 2, 1] Fig. 3.20Function range with a third value. Fig. 3.19Function range. 11

 2002 Prentice Hall. All rights reserved. Outline Fig03_22.py Program Output # Fig. 3.22: fig03_22.py # Summation with for. sum = 0 for number in range( 2, 101, 2 ): sum += number print ("Sum is", sum) Sum is 2550 Loops from 2 to 101 in increments of 2 A sum of all the even numbers from 2 to 100 Example of the for Repetition (1) 12

 2002 Prentice Hall. All rights reserved. Outline Fig02_23.py Program Output # Fig. 3.23: fig03_23.py # Calculating compound interest. principal = # starting principal rate =.05 # interest rate print ("Year %21s" % "Amount on deposit“) for year in range( 1, 11 ): amount = principal * ( rate ) ** year print ("%4d%21.2f" % ( year, amount )) Year Amount on deposit rate is the same no matter what, therefore it should have been calculated outside of the loop Starts the loop at 1 and goes to 10 Example of the for Repetition (2) 13

break and continue Statements The break statement – Used to make a loop stop looping – The loop is exited and no more loop code is executed The continue statement – Used to continue the looping process – All following actions in the loop are not executed But the loop will continue to run 14

 2002 Prentice Hall. All rights reserved. Outline Fig03_24.py Program Output # Fig. 3.24: fig03_24.py # Using the break statement in a for structure. for x in range( 1, 11 ): if x == 5: break print (x) print ("\nBroke out of loop at x =", x) Broke out of loop at x = 5 Shows that the counter does not get to 10 like it normally would have When x equals 5 the loop breaks. Only up to 4 will be displayed The loop will go from 1 to 10 15

 2002 Prentice Hall. All rights reserved. Outline Fig03_25.py 1 # Fig. 3.25: fig03_25.py 2 # Using the break statement to avoid repeating code 3 # in the class-average program. 4 5 # initialization phase 6 total = 0 # sum of grades 7 gradeCounter = 0 # number of grades entered 8 9 while 1: 10 grade = input( "Enter grade, -1 to end: " ) 11 grade = int( grade ) # exit loop if user inputs if grade == -1: 15 break total += grade 18 gradeCounter += # termination phase 21 if gradeCounter != 0: 22 average = float( total ) / gradeCounter 23 print ("Class average is", average) 24 else: 25 print ("No grades were entered“) If the user enters –1 then the loop ends Keeps a count and a sum of all the grades This loop will continue no matter what, like True Finds the average by dividing total by the counter 16

 2002 Prentice Hall. All rights reserved. Outline Fig03_26.py Program Output 1 # Fig. 3.26: fig03_26.py 2 # Using the continue statement in a for/in structure. 3 4 for x in range( 1, 11 ): 5 6 if x == 5: 7 continue 8 9 print (x) print ("\nUsed continue to skip printing the value 5" ) Used continue to skip printing the value 5 The value 5 will never be output but all the others will The loop will continue if the value equals 5 17

Logical Operators Operators – and Evaluates to true if both expressions are true – or Evaluates to true if at least one expression is true – not Returns true if the expression is false Not required in any program 18

and Logical Operator 19

Example of and Logical Operator Python 2.2b2 (#26, Nov , 11:44:11) [MSC 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> if 0:... print ("0 is true")... else:... print ("0 is false")... 0 is false >>> if 1:... print ("non-zero is true")... non-zero is true >>> if -1:... print ("non-zero is true")... non-zero is true >>> print (2 < 3) 1 >>> print (0 and 1) 0 >>> print (1 and 3) 3 Fig. 3.28Truth values. 20

or Logical Operator 21

not Logical Operator 22

Summary Sequential Selection – The if, if/else and if/elsif/else statements Repetition – The while and for loops 23

Summary (cont'd) 24

Exercises Write a Python program using nested for loop to output the following pattern to screen: * ** *** **** ***** 25