Count Controlled Loops

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming
Advertisements

Computer Science 1620 Loops.
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
Control Structures FOR Statement Looping.
Count Controlled Loops Look at the little children … Why is the sun’s face features orange …
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
Count Controlled Loops (Nested) Ain’t no sunshine when she’s gone …
Exception Handling and String Manipulation. Exceptions An exception is an error that causes a program to halt while it’s running In other words, it something.
For Friday Read No quiz Program 6 due. Program 6 Any questions?
Few More Math Operators
CSC 211 Java I for loops and arrays.
Python - Iteration A FOR loop is ideal if we know how many times we want to repeat. Try this code: for loopCounter in range(10): print(loopCounter) There.
Repetition Structures
REPETITION CONTROL STRUCTURE
Python: Experiencing IDLE, writing simple programs
ECE Application Programming
Module 5 Working with Data
Chapter 4 C Program Control Part I
Introduction to Python
Chapter 3 Assignment and Interactive Input.
Line Continuation, Output Formatting, and Decision Structures
Computer Programming Fundamentals
Loops BIS1523 – Lecture 10.
Control Structures II Chapter 3
Lecture 6 Repetition Richard Gesick.
CHAPTER 5A Loop Structure
Variables, Expressions, and IO
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: Repetition Structures
JavaScript: Functions.
Topics Introduction to Repetition Structures
Lecture 07 More Repetition Richard Gesick.
Do it now activity Green pen activity in books.
Writing Functions( ) (Part 5)
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Lecture 4A Repetition Richard Gesick.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Line Continuation, Output Formatting, and Decision Structures
Few More Math Operators
Python Primer 2: Functions and Control Flow
Decision Structures, String Comparison, Nested Structures
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
Repetition Structures
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
Data Structures – 1D Lists
ARRAYS 1 GCSE COMPUTER SCIENCE.
Repetition Structures
Topics Introduction to Repetition Structures
Chapter 6 Control Statements: Part 2
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
Writing Functions( ) (Part 4)
Conditional and iterative statements
Computing Fundamentals
Python programming exercise
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Building Java Programs
CHAPTER 6: Control Flow Tools (for and while loops)
Topics Introduction to Repetition Structures
Thinking about programming
COMPUTER PROGRAMMING SKILLS
Control Operations Basic operations are input, output, arithmetic, etc. Control operations allow for sequencing other operations Choose between several.
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Class code for pythonroom.com cchsp2cs
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Lecture 6 - Recursion.
Presentation transcript:

Count Controlled Loops Look at the little children … Why is the sun’s face features orange …

Warm Up Practice Write a program that continually asks the user for a price value When the user enters the word “end” you should end the program and add the total price of all the products In addition, display the highest priced item and the lowest priced item

Warm Up Practice Extension: Now, try writing your program so that it does not crash.

Count Controlled Loops Previously, we learned the “while” loop, which we defined as the condition controlled loop It is so called “condition controlled” because it iterates the number of times in which a Boolean expression holds True Today, we will look at what is called a “count controlled” loop

Count Controlled Loops It is called “count controlled” because it iterates a specific number of times It is not dependent on the truth value of a Boolean expression, or condition

Count Controlled Loop Now, it’s important to remember that a lot of times in Python, we can accomplish the same tasks with various different methods i.e. ELIF’s versus nested IF’s In the same way, a count controlled loop can be created by using a “while” loop These various methods are really for convenience’s sake (We should also note that a while loop can be duplicated by a function controlled loop)

Count Controlled Loop Example: counter = 0 while counter < 5: print (“this will print 5 times!”) counter +=1

Lists Python has such things called “lists” Lists are denoted by brackets [ a, b, c, d ] and each item in the list is separated by commas Lists can be stored and named as variables Example: x = [a, b, c, d]

Lists One important thing to note about lists is that they can hold various data types all at once Example: list = [“name”, “word”, 1, 2, 3]

The ‘’for’’ loop The “for” loop is Python’s native count controlled loop Example: for num in [1, 2, 3, 4, 5]: print(“this will also print 5 times”)

The ‘’for’’ loop The “for” keyword starts the loop The “num” is the name of the target variable “in” is another keyword [1, 2, 3, 4, 5] is the list of items to iterate over Note the indentation

The ‘’for’’ loop The “for” loop will iterate once for each item in the list passed to it when the loop begins During the first iteration, the target variable will assume the value of the first item in the list During the second iteration, it will assume the second item in the list This continues until you reach the end of the list

The ‘’for’’ loop for x in [1, 2, 3, 4, 5]: >> 1 print (x) 2 3 4 5

The ‘’for’’ loop for name in [“Josh”, “Jeen”, “Nicole”]: print(“My favorite student is”, name) >> My favorite student is Josh My favorite student is Jeen My favorite student is Nicole

Practice – Make Dat DOE Write a program that asks the user how much money they made from Monday to Sunday The program should specify the day each time it asks the user for a value Then sum up the total amount they made and print out

Practice – Mechanics Rewrite the following loop as a “while” loop: for x in [10, 20, 30, 40]: print (x)

The range( ) Function So far, we’ve been TIRELESSLY writing out lists of pre-defined values in our “for” loops The range( ) function allows us to dynamically generate lists based on pre-determined criteria

The range( ) Function for x in range(5): >> iteration # 0 print(“iteration #”, x) iteration # 1 iteration # 2 iteration # 3 iteration # 4

The range( ) Function The range( ) function takes at least one argument In it’s simplest form, it takes a single integer The range( ) function returns what we can think of as a list in Python When passed a single integer, it will return a list of integers from 0 to the number specific minus one

The range( ) Function range(5) [ 0, 1, 2, 3, 4] range (10) [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

The range( ) Function However, the range( ) function can behave in different ways It can take two arguments, which sets a start and an end value By default, the function increments by 1 range (1, 5) [ 1, 2, 3, 4] range (5, 10) [ 5, 6, 7, 8, 9]

The range( ) Function You can also pass three arguments and set a start value, an end value, and a step value range(0, 10, 2) [0, 2, 4, 6, 8] range(1, 15, 3) [1, 4, 7, 10, 13] If the step count does not perfectly fall on the end value, it will just include the number before the end value is reached

The range( ) Function Lastly, you can ask the range( ) function to count backwards by passing in a negative step count

Import ‘’time’’ We’ll talk more about this later, but another module we can import into Python is “time” We can ask Python to pause (for dramatic effect) by calling the sleep( ) function in time The sleep( ) function can take one argument, denoting the number of seconds to “sleep” **sleep( ) can also take floats

Import ‘’time’’ The sleep() function does not return anything and therefore can be called anywhere in your code print(“Hello”) time.sleep(2) print(“Goodbye”)

Practice – Countdown Write a program that counts down from 10 and then print out “HAPPY NEW YEAR!”

Import ‘’time’’ import time for x in range(10, 0, -1): print(x) time.sleep(1) print(“HAPPY NEW YEAR!”)

Loop Targets In a “for” loop, we generally use the target variable as a reference value for some kind of calculation Remember that the value of the target variable changes with each iteration of the loop

Practice – Squares Write a program that calculates the square of the numbers between 1 and 10 Print out the number and it’s square as your loop iterates

Practice – Stair Master Write a program that prints out the following: ** (2 stars) **** (4 stars) ****** (6 stars) ******** (8 stars) ********** (10 stars) ************ (12 stars)

User Controlled Ranges Sometimes, we need to ask the user to control the # of iterations within a loop You can do this by substituting a variable within the range( ) function to control the start, end, and step values of the list that will be generated

User Controlled Ranges x = int(input(“start value: “)) y = int(input(“end value: “)) z = int(input(“step value: “)) range(x, y, z)

User Controlled Ranges We can also just put the input( ) function directly into the range( ) function However, we must remember to convert it into an integer

User Controlled Ranges range(int(input(“start: “)), int(input(“end: “)), int(input(“step: “))) Just be careful, as this can be confusing and you need to keep count of how many parentheses you use

Practice – Stair Master Extend your stair master program to allow the user to decide how many flights of stairs they’d like Also allow the user to decide how many stars they’d like in each flight of stairs