1 Parts of a Loop (reminder) Every loop will always contain three main elements: –Priming: initialize your variables. –Testing: test against some known.

Slides:



Advertisements
Similar presentations
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.
Advertisements

CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Computer Science 1620 Loops.
Introduction to Computers and Programming Lecture 9: For Loops New York University.
COMP 14 Introduction to Programming Miguel A. Otaduy May 21, 2004.
Nested For Loops It is also possible to place a for loop inside another for loop. int rows, columns; for (rows=1; rows
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Chapter 5: Control Structures II (Repetition)
Chapter 5: Loops and Files.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
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.
Chapter 5: Repetition Statements. In this chapter, you will learn about: Basic loop structures while loops Interactive while loops for loops Loop programming.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The switch Multiple-Selection Statement switch.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
Chapter 6: Iteration Part 1. To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To understand.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
CPS120 Introduction to Computer Science Iteration (Looping)
Loops and Iteration for Statements, while Statements and do-while Statements.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Chapter 8 Repetition Statements. Introduction Iteration - process of looping or the repetition of one or more statements Loop body - the statement, or.
AEEE 195 – Repetition Structures: Part B Spring semester 2011.
CSE1222: Lecture 7The Ohio State University1. logExample.cpp // example of log(k) for k = 1,2,..,8... int main() { cout
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 4 Looping.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements.
Loops George Mason University. Loop Structure Loop- A structure that allows repeated execution of a block of statements Loop body- A block of statements;
Overview Go over parts of quiz? Another iteration structure for loop.
CPS120 Introduction to Computer Science Iteration (Looping)
Repetition Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
Loops and Files. 5.1 The Increment and Decrement Operators.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
A FIRST BOOK OF C++ CHAPTER 5 REPETITION. OBJECTIVES In this chapter, you will learn about: The while Statement Interactive while Loops The for Statement.
A First Book of C++ Chapter 5 Repetition.
Decision Making and Branching (cont.)
Repetition Repetition allows you to repeat an operation or a series of operations many times. This is called looping and is one of the basic structured.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
PGT C Programming1 Week 4 – Repetition Structures / Loops.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
CONTENTS Loop Statements Parts of a loop Types of Loops Nested Loops
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Lecture 4b Repeating With Loops
Chapter 4 – C Program Control
Chapter 4 Repetition Statements (loops)
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Lecture 7: Repeating a Known Number of Times
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
Lecture 07 More Repetition Richard Gesick.
Arrays, For loop While loop Do while loop
MSIS 655 Advanced Business Applications Programming
Outline Altering flow of control Boolean expressions
CSC215 Lecture Control Flow.
Alternate Version of STARTING OUT WITH C++ 4th Edition
Repetition Statements (Loops) - 2
Chapter 4 - Program Control
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
CSC215 Lecture Control Flow.
Presentation transcript:

1 Parts of a Loop (reminder) Every loop will always contain three main elements: –Priming: initialize your variables. –Testing: test against some known condition. –Updating: update the variable that is tested.

2 Loop types (reminder) Indefinite Loop: –You do not know ahead of time how many times your loop will execute. –For example, you do not know how many books a person might order. Definite Loop: –You know exactly how many times you want the loop to execute. –not at compile time necessarily

3 For loops Another type of loop in c is the for loop. It is very good for definite loops All the parts (priming, testing and updating) are in one place. format: for (prime expression; test expression; update expression) Since the expressions are all in one place, many people prefer for to while when the number of iterations is known.

4 Basic For Loop Syntax For loops are good for creating definite loops. int counter; for (counter =1;counter <= 10;counter++) printf ("%d\n", counter); 1. Priming: Set the start value. 2. Test Condition: Set the stop value. 3. Update: Update the value. Note that each section is separated by a semicolon.

5 for Loop Flowchart 1. Priming Set counter=1 2. Test counter <= 10 3a. print counter 3b. Update counter++; TRUE FALSE

6 Infinite Loop You can still end up with an infinite loop when using for loops for (counter=0; counter<=10; counter--)

7 For Loop Variations Increment may be negative: for (i=100; i>=1; i--) –This counts from 100 to 1. Increment may be greater than 1: for (i=100; i>=5; i-=5) –This counts from 100 to 5 in steps of 5

8 For Loop Variations any expression may be more complex: for (i=100*y; i>=1; i--) for (i=100; i>=y/2; i--) for (i=100; i>=1; i-=4*y)

9 For Loop Variations by using commas, you can put more than one statement in any expression for (i=100, y=0; i>=1; i--)

10 Nested For Loops It is also possible to place a for loop inside another for loop. int rows, columns; for (rows=1; rows<=5; rows++) { for (columns=1; columns<=10; columns++) printf ("*"); printf ("\n"); } Outer Loop Inner Loop Output:**************************************************

11 Nested For Loops, Example #2 #include main () { int rows, columns; for (rows=1; rows<=5; rows++) { for (columns=1; columns<=rows; columns++) printf ("*"); printf ("\n"); } Output:*************** Outer Loop Inner Loop

12 Warnings Do not use a float or double for the counter –May result in imprecise counter values and faulty evaluation for loop termination purposes Don’t use commas instead of semicolons to separate the components of the for loop –(very common error) As in the if and while, do not put a semicolon ; right after the parentheses – will be an empty loop!

13 Off-by-one error In the first example, shown here, if had written counter < 10 then loop would execute 9 times, not the desired 10 times for (counter = 1; counter <= 10; counter++) { printf ("%d\n", counter); } /* end for counter */

14 Help avoid off-by-one errors Try to make your conditions in the form <= not < –Avoid code like counter < 11 or counter < 10

15 Good Programming Practices If you use the counter variable (some programmers avoid this altogether) in the loop, do not change its value! –Do not attempt to manually adjust it to force an exit –Can cause subtle errors that are difficult to catch –Instead change your logic

16 Good Programming cont’d Do not put other expressions in the for control structure –Manipulations of other variables should appear before or within the body of the loop depending on whether or not you want to repeat them Put a blank line before and after each major control structure Try to limit nesting to three levels, if possible –More than three levels of indentation can get confusing Limit the for control header to one line if possible