ITEC 109 Lecture 18 Looping. Review Questions? Conditionals –if / elif / else –and / or / not.

Slides:



Advertisements
Similar presentations
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures.
Advertisements

CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay.
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
CS0004: Introduction to Programming Repetition – Do Loops.
ITEC 109 Lecture 19 For loops. Review While loops –Sentinels –Counter variable –Break –Continue.
James Tam Loops In Python In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
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
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Introduction to Computers and Programming More Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
Lecture 12 Another loop for repetition The while loop construct © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Building Java Programs Chapter 5 Program Logic and Indefinite Loops Copyright (c) Pearson All rights reserved.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
Repetition Statements Repeating an Action A specified number of times While a Condition is True Until a Condition is True.
Mastery Objective: Students will understand how to use while loops in computer programming.
Chapter 5 Loops.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
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.
GAME102 - INTRO WHILE LOOPS G. MacKay. Fundamental Control Structures  STRAIGHT LINE  CONDITIONAL  LOOPS.
1 CSCI N201 Programming Concepts and Database 9 – Loops Lingma Acheson Department of Computer and Information Science, IUPUI.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
A loop is a repetition control structure. body - statements to be repeated control statement - decides whether another repetition needs to be made leading.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Designing While Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Counting Loops.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 10 Iteration: Loops 05/02/09 Python Mini-Course: Day 3 - Lesson 10 1.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Loop Blocks Chapter 6 Part A. Program Blocks 1.Actions- commands, messages, methods 2.Branches- decisions to be made 3.Loops- actions to be repeated.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Today… Python Keywords. Iteration (or “Loops”!) Winter 2016CISC101 - Prof. McLeod1.
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
This is a while loop. The code is done while the condition is true. The code that is done is enclosed in { }. Note that this program has three parts: Housekeeping.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures Lecture 10.
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
CSc 110, Autumn 2017 Lecture 17: while Loops and Sentinel Loops
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
Chapter 5: Repetition Structures
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Repetition-Counter control Loop
Logical Operators and While Loops
CSc 110, Autumn 2016 Lecture 12: while Loops, Fencepost Loops, and Sentinel Loops Adapted from slides by Marty Stepp and Stuart Reges.
CSc 110, Spring 2018 Lecture 16: Fencepost Loops and while loops
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
Chapter 3 – Control Structures
Repetition Structures
CMPT 102 Introduction to Scientific Computer Programming
Let’s all Repeat Together
A LESSON IN LOOPING What is a loop?
Logical Operators and While Loops
Repetition Statements (Loops) - 2
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

ITEC 109 Lecture 18 Looping

Review Questions? Conditionals –if / elif / else –and / or / not

Looping Objectives Learn how to repeat code Counter variables Sentinels Incrementing counter variables

Looping Repetition Without input, this is your program What if you wanted to do it again?

Looping View of code x=2; x = x*2; printNow(x); Linear execution def readColor(): robot.readColor() color = readColor(); printNow(color); Code to handle a robot x = int(raw_input()) if (x < 3): goodCredit(); else: badCredit(); Linear Branching

Looping New x = x + int(raw_input()); Linear execution Decision on whether or not to do that again x = int(raw_input()); x = x + int(raw_input()); Read in and sum an integer 3 times New way

Looping Python if (condition ): #Code executed once while ( condition ) : #Code executed as long as condition = true x = x+int(raw_input()); printNow(x) Linear execution Decision on whether or not to do that again x=raw_input() while (x>0):

Looping Example i=0; while (i < 10): i = i+1; printNow(i); i = 0 i = 1 i = 2 i = 3 i = 4 i = 5 i = 6 i = 7 i = 8 i = 9 i = 10

Looping Example Enter commands until the user tells system to quit command = raw_input(); while (command !=“quit”): command = raw_input(); printNow(command)

Looping Example See the result of 10 hits on a player hp = 100; damage = 6; i=0; while (i < 10): hp = hp – damage; i = i +1; printNow(hp);

Looping Basics of looping What do you need to repeat? Why do you need to repeat it? How do you tell when to stop repeating? How do you update it?

Looping Counter variable Have: Logic that works on any case Guide through the loop (provides case) Starts at initial value Updates during each part of the loop

Looping Infinite loop i=0; while (i < 10): printNow(“SPAM”); What does this print out?What modification should you make?

Looping Examples i=0; while (i < 10): printNow(i); i = i+2; i=0; while (i < 10): printNow(i); i = i+3; i=10; while (i > 0): printNow(i); i = i-1; i=10; while (i > 0): printNow(i); i = i-2;

Looping Problem How do you add the even numbers from 0 to10? Hand version Code version

Looping Problems Sum numbers until user types in -1 Interactive menu with commands

Looping Breaking out What if you get cold feet halfway through? How do you tell a loop to stop executing? Two statements to modify behavior of loops –continue –break

Looping Continue Allows you to stop a loop, begin at the top while(true): command = raw_input(); if (command == “wait”): continue; elif(command == “move”): move();

Looping Palindrom es word =raw_input(); left=0; right = len(word)-1; while (left < right): left = left + 1; right = right -1; if (word[left] != word[right]) break; if (left >= right): printNow(word + “ is a palindrome”); else: printNow(word + “ is not a palindrome”); What is the counter variable? What is the sentinel? How is it updated?

Looping Review While loops –Counter variable –Sentinel –Incrementing Fine grain control –Break –continue