CPSC 217 T03 Week V Part #1: Iteration Hubert (Sathaporn) Hu.

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
1 CS101 Introduction to Computing Lecture 23 Flow Control & Loops (Web Development Lecture 8)
ITC 240: Web Application Programming
Loops – While, Do, For Repetition Statements Introduction to Arrays
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Fundamentals of Python: From First Programs Through Data Structures
Line up By Melissa Dalis Professor Susan Rodger Duke University June 2011.
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
Fundamentals of Python: First Programs
An Introduction to Textual Programming
Iteration. Adding CDs to Vic Stack In many of the programs you write, you would like to have a CD on the stack before the program runs. To do this, you.
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Week 5 - Wednesday.  What did we talk about last time?  Exam 1!  And before that?  Review!  And before that?  if and switch statements.
CPSC 217 T03 Week I Part #1: Unix and HELLO WORLD Hubert (Sathaporn) Hu.
Module 3 Fraser High School. Module 3 – Loops and Booleans import statements - random use the random.randint() function use while loop write conditional.
1 CS Programming Languages Class 15 October 17, 2000.
Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout
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.
CONTROLLING PROGRAM FLOW
Python Repetition. We use repetition to prevent typing the same code out many times and to make our code more efficient. FOR is used when you know how.
For loops in programming Assumes you have seen assignment statements and print statements.
CPSC 217 T03 Week I Part #2: Python Tricks for Exercise #1 Hubert (Sathaporn) Hu.
Week 6.  Lab 1 and 2 results  Common mistakes in Style  Lab 1 common mistakes in Design  Lab 2 common mistakes in Design  Tips on PE preparation.
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.
Variables and Expressions CMSC 201. Today we start Python! Two ways to use python: You can write a program, as a series of instructions in a file, and.
CPSC 217 T03 Week II Part #1: SimpleGraphics.py Hubert (Sathaporn) Hu.
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.
CPSC 217 T03 Week VI Part #2: Midterm Review Session Hubert (Sathaporn) Hu.
CPSC 217 T03 Week VI Part #1: A2 Post-Mortem and Functions Hubert (Sathaporn) Hu.
Iteration. Iteration: Review  If you wanted to display all the numbers from 1 to 1000, you wouldn’t want to do this, would you? Start display 1 display.
Repetition Intro to Computer Science CS1510 Dr. Sarah Diesburg.
COMP Loop Statements Yi Hong May 21, 2015.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
Logical Operators.  Quiz  Let's look at the schedule  Logical Operators 2.
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.
Repetition Structures The long awaited …. Repetition Structures I repeat …
CPSC 217 T03 Week IX Part #1: Function, Iteration and List Hubert (Sathaporn) Hu.
Controlling Program Structures. Big Picture We are learning how to use structures to control the flow of our programs Last week we looked at If statements.
Today… Preparation for doing Assignment 1. Invoking methods overview. Conditionals and Loops. Winter 2016CMPE212 - Prof. McLeod1.
Loops. About the Midterm Exam.. Exam on March 12 Monday (tentatively) Review on March 5.
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.
More about Iteration Victor Norman CS104. Reading Quiz.
Discussion 4 eecs 183 Hannah Westra.
Chapter 4 Repetition Statements (loops)
Topic: Iterative Statements – Part 1 -> for loop
Repetition Structures
Selection and Python Syntax
Lesson 05: Iterations Class Chat: Attendance: Participation
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.
While Loops in Python.
Engineering Innovation Center
While Loops (Iteration 2)
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.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Conditions and Ifs BIS1523 – Lecture 8.
Outline Altering flow of control Boolean expressions
Lesson 05: Iterations Topic: Introduction to Programming, Zybook Ch 4, P4E Ch 5. Slides on website.
CMSC201 Computer Science I for Majors Lecture 12 – Tuples
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CHAPTER 6: Control Flow Tools (for and while loops)
Midterm Review October 23, 2006 ComS 207: Programming I (in Java)
Programming Dr. Jalal Kawash.
While Loops in Python.
CMPT 120 Lecture 13 – Unit 2 – Cryptography and Encryption –
Presentation transcript:

CPSC 217 T03 Week V Part #1: Iteration Hubert (Sathaporn) Hu

Today’s Tutorial Returning A1 TIPS Iteration  While loop  Nested loop  For loop REMINDER: Today is the election day!

Returning A1 There are 2 students who got the bonus mark. They will receive +3 on top of what they already have. I won’t identify the winners here. Please check your assignment.  If you win, you will have “Bonus!” on the assignment.  If I think your work is good, it will have “Bonus candidate” on it.

TIPS 1.You may and should negotiate with me if you think I have made a mistake. However, since we use grade letter for marking, I will not change your mark unless your grade letter will change. 2.Although I encourage you to talk to me about your mark, in my own experience, your mark won’t likely change. 3.Midterm is coming. Be prepared!

Iteration When you want your program to repeat a section of the code, you will need to use iteration. In Python, you can only use 2 kinds of iterations:  While  For

While loop Syntax: while condition: body body will be repeated over and over again until condition becomes false. condition is a Boolean expression. TIP: Be wary of the spacing!

While loop What does this program do? x = 100 while x > 0: print(x, “ bottles. One fell.”)

While loop EXERCISE: Create a program that keeps asking users for a string. The program will only stop when the user enters the correct string. You can make the correct string whatever you want.  For example: Please guess: apple Wrong! Please guess: orange Wrong! Please guess: jackrabbit RIGHT! The program ends!

For loop for and while are logically equivalent. However, for does have a nicer syntax for some occasions. Unlike while, you don’t need to declare the variable used for iteration before hand. What does this program do? for x in range(1, 25): print(x, “ people now in the room.”) EXERCISE: Convert the program to use while.

Nested loop Did you know that you can put a loop in another? It doesn’t matter if you use for or while. You can nest them in any combination you want. You can nest loop in more than 2 levels. Normally, you use nested loops in order to do something multidimensional…  Like filling a multidimensional array.  Like manipulating a table.

Nested loop What does this program do? x = 1 y = 1 while x <= 12: while y <= 12: print (x, “*”, y, “:”, x * y) y += 1 x += 1 y = 0

Nested loop EXERCISE: Try out the exercises on loop given on course website.