And now for something completely different . . .

Slides:



Advertisements
Similar presentations
While loops.
Advertisements

 2002 Prentice Hall. All rights reserved Control Structures 3 control structures –Sequential structure Built into Python –Selection structure The.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
Structured programming
Loops – While, Do, For Repetition Statements Introduction to Arrays
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Chapter 4: Control Structures II
The University of Texas – Pan American
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Chapter 4: Control Structures II
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
Control Structures II: Repetition.  Learn about repetition (looping) control structures  Explore how to construct and use count-controlled, sentinel-controlled,
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
C Language 1 Program Looping. C Language2 Topics Program looping Program looping Relational operators / expressions Relational operators / expressions.
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.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Loops (While and For) CSE 1310 – Introduction to Computers and Programming 1.
Java Fundamentals 4. Java Programming: From Problem Analysis to Program Design, Second Edition2 Parsing Numeric Strings  Integer, Float, and Double are.
Loops ( while and for ) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan 1.
Introduction to Loop. Introduction to Loops: The while Loop Loop: part of program that may execute > 1 time (i.e., it repeats) while loop format: while.
Java Fundamentals 4.
Chapter 9 Repetition.
EKT120 COMPUTER PROGRAMMING
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Loops Upsorn Praphamontripong CS 1110 Introduction to Programming
Introduction To Repetition The for loop
Chapter 5: Control Structures II
Chapter 5: Control Structures II
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Repetition-Counter control Loop
CiS 260: App Dev I Chapter 4: Control Structures II.
Repetition-Sentinel,Flag Loop/Do_While
Chapter 2.2 Control Structures (Iteration)
( Iteration / Repetition / Looping )
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
JavaScript: Control Statements I
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Chapter 5: Control Structures II
Lecture 4B More Repetition Richard Gesick
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Chapter 5 Repetition.
Chapter 4 Control structures and Loops
Arrays, For loop While loop Do while loop
Iteration with While You can say that again.
And now for something completely different . . .
The University of Texas – Pan American
Loops CIS 40 – Introduction to Programming in Python
Chapter 3 – Control Structures
Module 4 Loops.
Control Statements Loops.
Chapter 2.2 Control Structures (Iteration)
CS 1111 Introduction to Programming Spring 2019
Repetition Statements (Loops) - 2
Control Statements Loops.
And now for something completely different . . .
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Unit 3: Variables in Java
Presentation transcript:

And now for something completely different . . . 10-Nov-18 AHD c 2010

Part 5 Python 3 - Repetition and String Formatting 10-Nov-18 AHD c 2010

Python 3 Repetition and String Formatting 5.1 The while loop 5.2 The for loop 5.3 String formatting 10-Nov-18 AHD c 2010

Python Repetition and String Formatting 5.1 The while loop 5.2 The for loop 5.3 String formatting 10-Nov-18 AHD c 2010

The while statement Like the if statement, the while statement makes use of a Boolean expression... x = 1 while x < 5: print ('Hi spam') x = x + 1 print ('done') 10-Nov-18 AHD c 2010

The while statement The Boolean expression (condition) in this example is: x < 5 The expression has a value of true or false (1 or 0). x = 1 while x < 5: print ('Hi spam') x = x + 1 print ('done') 10-Nov-18 AHD c 2010

The while statement If the condition is true, the indented statements are executed, otherwise, the statements are skipped. x = 1 while x < 5: print ('Hi spam') x = x + 1 print ('done') 10-Nov-18 AHD c 2010

The while statement 05-01.py 05-02.py x = 1 while x < 5: print ('Hi spam') x = x - 1 print ('done') 05-01.py 05-02.py These programs are examples of counter-controlled while loops. 10-Nov-18 AHD c 2010

The infinite while loop print ('Hi spam') print ('done') 05-03.py 05-04.py 10-Nov-18 AHD c 2010

The break statement 05-05.py while 1: print ('Spam') answer = input('Press y to end this loop') if answer == 'y': print ('Fries with that?') break print ('Have a ') print ('nice day!') 10-Nov-18 AHD c 2010

The continue statement 05-06.py while 1: print ('Spam') answer = input('Press y for large fries ') if answer == 'y': print ('Large fries with spam, mmmm, yummy ') continue answer = input('Had enough yet? ') break print ('Have a ') print ('nice day!') 10-Nov-18 AHD c 2010

A Sentinel-Controlled while loop 05-07.py 10-Nov-18 AHD c 2010

Python Repetition and String Formatting 5.1 The while loop 5.2 The for loop 5.3 String formatting 10-Nov-18 AHD c 2010

Counter-controlled repetition with the for loop for c in range (10): print (c) 05-08.py This program prints the numbers 0 through 9. 10-Nov-18 AHD c 2010

Counter-controlled repetition with the for loop for c in range (5,10): print (c) 05-09.py This program prints the numbers 5 through 9. 10-Nov-18 AHD c 2010

Using continue with the for loop for c in range (1,6): if c == 3: continue print (c) 05-10.py This program prints the numbers 1, 2, 4, 5, (skips 3). 10-Nov-18 AHD c 2010

Using break with the for loop for c in range (1,6): if c == 3: break print (c) 05-11.py This program prints the numbers 1 and 2. 10-Nov-18 AHD c 2010

Python Repetition and String Formatting 5.1 The while loop 5.2 The for loop 5.3 String formatting 10-Nov-18 AHD c 2010

Printing strings and numbers We can print string values and number values from the same print statement by separating them with commas: d = 10 c = 75 print ('Total is: ', d, 'dollars and', c, ' cents ') >>> Total is: 10 dollars and 75 cents 05-12.py 10-Nov-18 AHD c 2010

Numbers can be printed from within a single string by using a special method known as string formatting. 10-Nov-18 AHD c 2010

String Formatting and % In string formatting, we use the % symbol. The % operator can also be used for a different purpose as the modulus operator (finding the remainder after an integer division). The % symbol is said to be overloaded. 10-Nov-18 AHD c 2010

String Formatting and % An overloaded operator behaves differently depending on the context. In the following example we see the % operator being used to specify how a string should be printed (i.e. string formatting). 10-Nov-18 AHD c 2010

Printing integers within a string x = 20 y = 75 print ('The sum of %d and %d is %d' % (x, y, x + y)) >>> The sum of 20 and 75 is 95 The three %d formatting codes represent where the decimal integers shown in brackets after the % symbol should be printed. 05-13.py 10-Nov-18 AHD c 2010

Printing floats within a string x = 20.512 y = 15.269 print ('The sum of %f and %f is %f' % (x, y, x + y)) >>> The sum of 20.512000 and 15.269000 is 35.781000 The three %f represent where the float values shown in brackets after the % symbol should be printed. 05-13.py 10-Nov-18 AHD c 2010

Specifying the number of decimal places x = 20.512 y = 15.269 print ('The sum of %0.2f and %0.2f is %0.2f' % (x, y, x + y)) >>> The sum of 20.51 and 15.27 is 35.78 The three %0.2f represent where the float values (to 2 decimal places) shown in brackets after the % symbol should be printed. 05-13.py 10-Nov-18 AHD c 2010

String formatting codes %d and %f are only two of a set of formatting codes available for string formatting. See: http://www.annedawson.net/Python_Help.htm for more details. 10-Nov-18 AHD c 2010

Repeating a progam at the user’s request print ("This is the start of the program") answer = 'y' while (answer == 'y' or answer == 'Y'): print ("This is a statement from within the while loop") print ("This is another statement from within the while loop") answer = input("Do you want to run this program again? y/n") print ("Goodbye! ") 05-14.py 10-Nov-18 AHD c 2010

Loops within loops - nested loops for i in range (1,6): for j in range (1,6): print ("i: " + str(i) + " j: " + str(j) ) print() ''' Notice that with a loop repeating 5 times, ***within*** a loop that repeats 5 times means that you can control 25 processes. 05-15 - 05-18.py 10-Nov-18 AHD c 2010

This presentation uses the following program files: http://www.annedawson.net/Python3Programs.txt 05-01.py 05-02.py 05-03.py 05-04.py 05-05.py 05-06.py 05-07.py 05-08.py 05-09.py 05-10.py 05-11.py 05-12.py 05-13.py 05-14.py 05-15.py 05-16.py 05-17.py 05-18.py 10-Nov-18 AHD c 2010

End of Python_Repetition_StringFormatting.ppt 10-Nov-18 AHD c 2010

Last updated: Sunday 24th May 2009, 15:58 PT, AHD 10-Nov-18 AHD c 2010