Chapter 4: Looping CSCI-UA 0002 – Introduction to Computer Programming Mr. Joel Kemp.

Slides:



Advertisements
Similar presentations
CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Advertisements

Introduction to Computing Science and Programming I
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
ITC 240: Web Application Programming
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
COMP 14 Introduction to Programming Mr. Joshua Stough February 16, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
Python (yay!) November 16, Unit 7. Recap We can store values in variables using an assignment statement >>>x = We can get input from the user using.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
Loops – While, Do, For Repetition Statements Introduction to Arrays
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
Fundamentals of Python: From First Programs Through Data Structures
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
Fundamentals of Python: First Programs
More Looping Structures
Chapter 6 – Repetition Statements : Objectives After you have read and studied this chapter, you should be able to Implement repetition control in a program.
Copyright 2009 by Pearson Education Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos: Ch.
Control Structures FOR Statement Looping.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Module 3 Fraser High School. Module 3 – Loops and Booleans import statements - random use the random.randint() function use while loop write conditional.
Iterative Constructs Review l What are named constants? Why are they needed? l What is a block? What is special about declaring a variable inside a block?
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
© 2006 Pearson Education 1 More Operators  To round out our knowledge of Java operators, let's examine a few more  In particular, we will examine the.
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.
Logic Our programs will have to make decisions in terms of what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if.
Chapter 3: Branching and Program Flow CSCI-UA 0002 – Introduction to Computer Programming Mr. Joel Kemp.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 5 Repetition.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Repetition Structures.
CSC 1010 Programming for All Lecture 4 Loops Some material based on material from Marty Stepp, Instructor, University of Washington.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
9. ITERATIONS AND LOOP STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
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.
Loops (While and For) CSE 1310 – Introduction to Computers and Programming 1.
For Loop GCSE Computer Science – Python. For Loop The for loop iterates over the items in a sequence, which can be a string or a list (we will discuss.
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
26/06/ Iteration Loops For … To … Next. 226/06/2016 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Week of 12/12/16 Test Review.
Topics Introduction to Repetition Structures
Python: Control Structures
Warm-up Program Use the same method as your first fortune cookie project and write a program that reads in a string from the user and, at random, will.
Loop Structures.
Topics Introduction to Repetition Structures
Lecture 07 More Repetition Richard Gesick.
Lecture 4B More Repetition Richard Gesick
Chapter 5 - Functions Outline 5.1 Introduction
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.
Repetition Chapter 6 12/06/16 & 12/07/16 1 1
T. Jumana Abu Shmais – AOU - Riyadh
Loops CIS 40 – Introduction to Programming in Python
Introduction to TouchDevelop
More Looping Structures
Alternate Version of STARTING OUT WITH C++ 4th Edition
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
CHAPTER 6: Control Flow Tools (for and while loops)
Topics Introduction to Repetition Structures
More Looping Structures
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Fundamentals of Python: First Programs Second Edition
Presentation transcript:

Chapter 4: Looping CSCI-UA 0002 – Introduction to Computer Programming Mr. Joel Kemp

Agenda while for range() for... in

Loop Loop: a coding construct that allows for some code to be run repeatedly. A loop, like a branch: – Has a condition (loop condition). – Executes a chunk of code (the loop body) when the condition evaluates to True. What does this look like in Python?

while Loop One of the simplest forms of looping! Syntax: while (expression): # Loop body goes here Example: The Simpsons answer = input(“Homer, are we there yet? ”) while (answer == “no”): answer = input(“Homer, are we there yet? ”) print(“Finally! Ay Caramba!”) Alternative: answer = “no” # Makes sure the loop runs while (answer == “no”): answer = input(“Homer, are we there yet? ”) print(“Finally! Ay Caramba!”) Refer to: homerWhileLoop.py

Alternative Solutions Multiple input() calls: answer = input(“Homer, are we there yet? ”) while (answer == “no”): answer = input(“Homer, are we there yet? ”) print(“Finally! Ay Caramba!”) Preset answer to start the loop: answer = “no” # Makes sure the loop runs while (answer == “no”): answer = input(“Homer, are we there yet? ”) print(“Finally! Ay Caramba!”) Near-infinite loop: while (True): # Say whaaaat?! answer = input(“Homer, are we there yet? ”) if (answer != “no”): break # Terminates the loop instantly print(“Finally! Ay Caramba!”) Refer to: whileLoopAlternatives.py

Back to Guessing! Let’s fix the problem with our game! – While the guess is wrong, keep asking for a guess! Here’s where we left off!

One Solution Multiple input() calls : Refer to: guessingv3.py Let’s explore some alternate solutions!

One Final Change Let’s make this game really worthwhile! Instead of the secret number always being 5, how can we make the number be a random number? – Let’s say between numbers 1 to 10. Hmm, random you say?

Python Random How can we generate a random number? At the top of your code, add the line: from random import randint – This tells the interpreter that we’re using randint() from the “ random ” Python library. Library – a collection of related functions that can be referenced. – randint() takes two arguments (e.g., x and y) and generates a random number between x and y (inclusive). Refer to: random.py

The Final Code Refer to: guessingv4.py

while Exercises 1.Write a program that asks the user to enter a number that is greater than 0. – The program will keep on asking the user for the number until it is valid. – The program will then print the valid number. 2.Write a program that asks the user for the number N and range R of random numbers to generate. – Generate and print N random numbers between 0 and R. – If N is 0, then output “No numbers will be generated!” 3.Write a program that asks the user for the number, N, of even numbers to print. – Your program should compute and print the first N even numbers starting from 0. Refer to: whileExercises.py

OTHER TYPES OF LOOPS

for loop A for loop is a very popular looping construct In python, a for loop iterates over: – Lists of numbers – Lists of objects – Text files How do we define a list? – We’ll see this more formally later. – For now, we’ll use range() ! The range() function is predominantly used with for loops! Example: for num in range(5): print(num)

range() The range function has the following form: range(lower_bound, upper_bound, step) – lower_bound is optional and defaults to 0. – step is optional and defaults to 1. What does it do? – Generates a list of numbers from the lower bound up to but not including the upper bound. Examples: range(1, 5)# [1, 2, 3, 4] range(1, 6, 2) # [1, 3, 5] range(4)# [1, 2, 3] Refer to: range.py

Exercises 1.Generate and print the even numbers up to and including the number Print the square roots for the numbers from 1 to 10. – You’ll need to use the sqrt() function of the math library. – from math import sqrt 3.Create a program that computes the factorial of a user supplied number. 0! = 1 1! = 1 2! = 2 * 1 3! = 3 * 2 * 1 4! = 4 * 3* 2* 1

Nested Loops We can have loops within loops! Example: for i in range(5): for j in range(2): print(“Hi”) How does it work? 1.Perform the inner loop until it completes 2.Iterate the outer loop 3.Repeat until we exceed the range of the outer loop. Output: – The string “Hi” will be printed 10 times. Refer to: nestedLoops.py