Chapter 5 Repetition or loop structure. What is repetition or loop? repeat the execution of one or a group (block; instruction enclosed in a pair of braces)

Slides:



Advertisements
Similar presentations
Repetition There are three different ways that a set of instructions can be repeated, and each way is determined by where the decision to repeat is.
Advertisements

Repetition Control Structures
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
Chapter 04 (Part III) Control Statements: Part I.
Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Loops. Outline of control structures zSequential zDecision (selection) yA. Logical IF yB. Block IF x1. Single alternative IF x2. Double alternative IF.
CS 106 Introduction to Computer Science I 02 / 11 / 2008 Instructor: Michael Eckmann.
An Introduction to Programming with C++ Fifth Edition
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
Repetition Control Structures
CS 106 Introduction to Computer Science I 09 / 28 / 2007 Instructor: Michael Eckmann.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
Chapter 3 Planning Your Solution
Presented by Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 12 Boolean Expressions, Switches, For-Loops Chapter 7.
An Introduction to Textual Programming
Repetition Statements Repeating an Action A specified number of times While a Condition is True Until a Condition is True.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
1 Three C++ Looping Statements Chapter 7 CSIS 10A.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
PROBLEM SOLVING WITH LOOPS Chapter 7. Concept of Repetition Structure Logic It is a computer task, that is used for Repeating a series of instructions.
Coding Design Tools Rachel Gauci. Task: Counting On Create a program that will print out a sequence of numbers from "1" to a "number entered”. Decision’s.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
Repetition Control Structures Simple Program Design Third Edition A Step-by-Step Approach 5.
Structured Program Development Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
Chapter 4 Selection: the if-else and switch-case instructions.
A loop is a repetition control structure. body - statements to be repeated control statement - decides whether another repetition needs to be made leading.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
1 09/27/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
1 10/3/05CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Chapter 7 Problem Solving with Loops
Chapter 5: Repetition and Loop Statements By: Suraya Alias.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.
COMP Loop Statements Yi Hong May 21, 2015.
CISC105 – General Computer Science Class 4 – 06/14/2006.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
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.
CS 101 – Oct. 7 Solving simple problems: create algorithm Structure of solution –Sequence of steps (1,2,3….) –Sometimes we need to make a choice –Sometimes.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
COP 3275 – Finishing Loops and Beginning Arrays Instructor: Diego Rivera-Gutierrez.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
Introduction to Computer Programming
REPETITION CONTROL STRUCTURE
- Standard C Statements
JavaScript: Control Statements I
Chapter 4 – Control Structures Part 1
Programming Fundamentals
Lecture 07 More Repetition Richard Gesick.
Lecture 4B More Repetition Richard Gesick
Logical Operators and While Loops
Chapter 8 The Loops By: Mr. Baha Hanene.
Chapter (3) - Looping Questions.
3 Control Statements:.
Let’s all Repeat Together
CHAPTER 4 Iterative Structure.
Logical Operators and While Loops
EPSII 59:006 Spring 2004.
Java Programming Loops
Chapter 4: Repetition Structures: Looping
ICS103: Programming in C 5: Repetition and Loop Statements
Presentation transcript:

Chapter 5 Repetition or loop structure

What is repetition or loop? repeat the execution of one or a group (block; instruction enclosed in a pair of braces) of instructions a certain number of times.

Why repetition or loop? better understood by a simple example: the hourly payroll program introduced earlier computes the pay of only one employee! Yet in general, a company has many employees. Can you think of other examples?

The repetition instructions There are three different instructions you can choose from: the while instruction, the for instruction, and the do-while instruction. Which one to choose? (will discuss later.)

The while loop format: while (logical expression) { loop task } the logical expression is similar to logical expression you saw in if-else structure discussed in chapter 4.

Simple hourly payroll program problem specification: user is prompted to enter # employees. For each employee, the user is asked to enter hours worked and hourly rate. The program then computes and prints out the pay for each employee until. The process repeats until all employees are processed. Let's work it out together!

A program that computes the average of a list of scores sometime it may not be practical to ask the user to enter how many scores are there (e.g., if the number of scores or students who tool the test are large, it is very time-consuming to count how many scores are there; the manual counting process is error-prone, too.) A simple solution is to use a sentinel value which is also called a trailer. Let's work out the program together. How many variables need to be declared? Remember accumulator and counter?

The rocket count-down problem output: Blast off! Can you work out the program? Let give it a try.

Another simple example Add integers from 1 to 100. Add integers from n1 to n2. Add odd integers from 1 to 99. Add odd integers from n1 to n2. Add even integers from 1 to 99. Add even integers from n1 to n2.

How about the temperature conversion table FahrenheitCelsius … …

How about tables for math functions sin(x) cos(x) etc.