Topics. Introduction to Repetition Structures Often have to write code that performs the same task multiple times. Controlled Loop

Similar documents
Repetition Structures

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

Loops / Repetition Statements. There are three loop constructs in C. Example 2: Grade of several students. Example 1: Fixing Bad Keyboard Input

Loops / Repetition Statements

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. The Increment and Decrement Operators

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. Looping. ++ is the increment operator.

*Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* COMPUTER PROGRAMMING LECTURE 05 LOOPS IMRAN IHSAN

Condition Controlled Loops. Introduction to Programming - Python

Looping. Arizona State University 1

CONDITION CONTROLLED LOOPS. Introduction to Programming - Python

A Beginner s Guide to Programming Logic, Introductory. Chapter 5 Looping

Lecture 7 Tao Wang 1

REPETITION CONTROL STRUCTURE LOGO

Condition Controlled Loops. Introduction to Programming - Python

An Introduction to Programming with C++ Sixth Edition. Chapter 7 The Repetition Structure

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

Chapter 5: Prefix vs. Postfix 8/19/2018. The Increment and Decrement Operators. Increment and Decrement Operators in Program 5-1

Repetition and Loop Statements Chapter 5

C++ Programming: From Problem Analysis to Program Design, Third Edition

Chapter 5: Loops and Files

INTRODUCTION TO C++ PROGRAM CONTROL. Dept. of Electronic Engineering, NCHU. Original slides are from

While Loops CHAPTER 5: LOOP STRUCTURES. While Loops. While Loops 2/7/2013

Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

LOOPS. Repetition using the while statement

Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5-1

Test #2 October 8, 2015

PLD Semester Exam Study Guide Dec. 2018

Indicate the answer choice that best completes the statement or answers the question. Enter the appropriate word(s) to complete the statement.

Repetition CSC 121 Fall 2014 Howard Rosenthal

Loops / Repetition Statements

Lesson 6A Loops. By John B. Owen All rights reserved 2011, revised 2014

STUDENT LESSON A12 Iterations

Errors. And How to Handle Them

Repetition Structures II

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

CSCE 206: Structured Programming in C++

Loops In Python. In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.

Loops. Repeat after me

Repeating Instructions. C# Programming: From Problem Analysis to Program Design 2nd Edition. David McDonald, Ph.D. Director of Emerging Technologies

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva

Chapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 5: Control Structures II (Repetition)

Program Development. Chapter 3: Program Statements. Program Statements. Requirements. Java Software Solutions for AP* Computer Science A 2nd Edition

Chapter 3: Program Statements

Chapter 4: Control structures. Repetition

While Loops A while loop executes a statement as long as a condition is true while condition: statement(s) Statement may be simple or compound Typical

Chapter Goals. Contents LOOPS

Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

Chapter 4: Control structures

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

CS112 Lecture: Repetition Statements

An Introduction to Programming with C++ Sixth Edition. Chapter 8 More on the Repetition Structure

This is the basis for the programming concept called a loop statement

Loop structures and booleans

Introduction to Computer Programming for Non-Majors

Laboratory 5: Implementing Loops and Loop Control Strategies

CS112 Lecture: Loops

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

Repetition Through Recursion

Introduction to Computer Programming for Non-Majors

Introduction. C provides two styles of flow control:

Information Science 1

Information Science 1

Python Programming: An Introduction to Computer Science

Assignment #2: Intro to Java Due: 11AM PST on Wednesday, July 12

REPETITIVE EXECUTION: LOOPS

The Typed Racket Guide

Algorithm Discovery and Design

Loops In Python. In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.

(Refer Slide Time: 1:27)

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

The for Loop, Accumulator Variables, Seninel Values, and The Random Class. CS0007: Introduction to Computer Programming

Computer Programming I - Unit 5 Lecture page 1 of 14

Please answer the following questions. Do not re-code the enclosed codes if you have already completed them.

Lecture. Loops && Booleans. Richard E Sarkis CSC 161: The Art of Programming

Principles of Computer Science I

Functions. Lecture 6 COP 3014 Spring February 11, 2018

Program Development. Java Program Statements. Design. Requirements. Testing. Implementation

Chapter 4: Loops and Files

CIS 3260 Intro to Programming in C#

Building Java Programs

Building Java Programs

Chapter 4: Loops and Files

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

Chapter 3. Iteration

Islamic University of Gaza Computer Engineering Dept. C++ Programming. For Industrial And Electrical Engineering By Instructor: Ruba A.

5. Control Statements

9/4/2018. Chapter 2 (Part 1) MATLAB Basics. Arrays. Arrays 2. Arrays 3. Variables 2. Variables

Topic 5 for loops and nested loops

Repetition Structures Chapter 9

Control Statements. If Statement if statement tests a particular condition

Outline for Today CSE 142. Programming a Teller Machine. CSE142 Wi03 I-1. ATM Algorithm for Dispensing Money

code-it.co.uk Exploring Regular 2D Shapes & Patterns using sequence, repetition, nested loops, Program Aim: Program regular 2D shapes

CSc Introduc/on to Compu/ng. Lecture 8 Edgardo Molina Fall 2011 City College of New York

School of Computer Science CPS109 Course Notes 6 Alexander Ferworn Updated Fall 15. CPS109 Course Notes 6. Alexander Ferworn

while (condition) { body_statements; for (initialization; condition; update) { body_statements;

LESSON 3. In this lesson you will learn about the conditional and looping constructs that allow you to control the flow of a PHP script.

Flow Control. CSC215 Lecture

Transcription:

Topics C H A P T E R 4 Repetition Structures Introduction to Repetition Structures The for Loop: a Count- Sentinels Nested Loops Introduction to Repetition Structures Often have to write code that performs the same task multiple times Disadvantages to duplicating code Makes program large Time consuming May need to be corrected in many places Repetition structure: makes computer repeat included code as necessary Includes condition-controlled s and countcontrolled s while : while condition is true, do something Two parts: Condition tested for true or false value Statements repeated as long as condition is true In flow chart, line goes back to previous part General format: while condition: statements In order for a to stop executing, something has to happen inside the to make the condition false Iteration: one execution of the body of a while is known as a pretest Tests condition before performing an iteration Will never execute if condition is false to start with Requires performing some steps prior to the 1

Infinite Loops Loops must contain within themselves a way to terminate Something inside a while must eventually make the condition false Infinite : that does not have a way of stopping Repeats until program is interrupted Occurs when programmer forgets to include stopping code in the The for Loop: a Count- Count-Controlled : iterates a specific number of times Use a for statement to write count-controlled Designed to work with sequence of data items Iterates once for each item in the sequence General format: for variable in [val1, val2, etc]: statements Target variable: the variable which is the target of the assignment at the beginning of each iteration Using the range Function with the for Loop The range function simplifies the process of writing a for range returns an iterable object Iterable: contains a sequence of values that can be iterated over range characteristics: One argument: used as ending limit Two arguments: starting value and ending limit Three arguments: third argument is step value Using the Target Variable Inside the Loop Purpose of target variable is to reference each item in a sequence as the iterates Target variable can be used in calculations or tasks in the body of the Example: calculate square root of each number in a range 2

Letting the User Control the Loop Iterations Sometimes the programmer does not know exactly how many times the will execute Can receive range inputs from the user, place them in variables, and call the range function in the for clause using these variables Be sure to consider the end cases: range does not include the ending limit Generating an Iterable Sequence that Ranges from Highest to Lowest The range function can be used to generate a sequence with numbers in descending order Make sure starting number is larger than end limit, and step value is negative Example: range (10, 0, -1) Programs often need to calculate a total of a series of numbers Typically include two elements: A that reads each number in series An accumulator variable Known as program that keeps a running total: accumulates total and reads in series At end of, accumulator will reference the total The Augmented Assignment Operators In many assignment statements, the variable on the left side of the = operator also appears on the right side of the = operator Augmented assignment operators: special set of operators designed for this type of job Shorthand operators The Augmented Assignment Operators 3

Sentinels Sentinel: special value that marks the end of a sequence of items When program reaches a sentinel, it knows that the end of the sequence of items was reached, and the terminates Must be distinctive enough so as not to be mistaken for a regular value in the sequence Example: when reading an input file, empty line can be used as a sentinel Computer cannot tell the difference between good data and bad data If user provides bad input, program will produce bad output GIGO: garbage in, garbage out It is important to design program such that bad input is never accepted Input validation: inspecting input before it is processed by the program If input is invalid, prompt user to enter correct data Commonly accomplished using a while which repeats as long as the input is bad If input is bad, display error message and receive another set of data If input is good, continue to process the input Nested Loops Nested : that is contained inside another Example: analog clock works like a nested Hours hand moves once for every twelve movements of the minutes hand: for each iteration of the hours, do twelve iterations of minutes Seconds hand moves 60 times for each movement of the minutes hand: for each iteration of minutes, do 60 iterations of seconds 4

Nested Loops Key points about nested s: Inner goes through all of its iterations for each iteration of outer Inner s complete their iterations faster than outer s Total number of iterations in nested : number_iterations_inner x number_iterations_outer Summary This chapter covered: Repetition structures, including: Condition-controlled s Count-controlled s Nested s Infinite s and how they can be avoided range function as used in for s Calculating a running total and augmented assignment operators Use of sentinels to terminate s 5