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.

Slides:



Advertisements
Similar presentations
Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.
Advertisements

Loops (Part 1) Computer Science Erwin High School Fall 2014.
Branching Constructs Review l what are branching constructs? what type of branching constructs have we studied? l what is nested if? l what is multiway.
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.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Chapter 5: Loops and Files.
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.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
Chapter 5: Control Structures II (Repetition)
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
CPS120 Introduction to Computer Science Iteration (Looping)
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
Computer Science Department LOOPS. Computer Science Department Loops Loops Cause a section of your program to be repeated a certain number of times. The.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
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.
ITERATIVE STATEMENTS. Definition Iterative statements (loops) allow a set of instruction to be executed or performed several until condition are met.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Overview Go over parts of quiz? Another iteration structure for loop.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
CPS120 Introduction to Computer Science Iteration (Looping)
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
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.
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.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: The while Statement cin within a while Loop The for.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Chapter 6 - Repetition. while Loop u Simplest loop u Two parts: test expression and loop body u Pre-tested loop –Execute loop body if test true –Bypass.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop 
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
Lecture 4b Repeating With Loops
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Unit 3 Lesson 9 Repetition Statements (Loops)
Loop Structures.
Java Programming: Guided Learning with Early Objects
Programming Fundamentals
JavaScript: Control Statements.
Arrays, For loop While loop Do while loop
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
3 Control Statements:.
Repetition Control Structure
Chapter 6: Repetition Statements
Computing Fundamentals
Repetition Statements (Loops) - 2
Based on slides created by Bjarne Stroustrup & Tony Gaddis
PROGRAM FLOWCHART Iteration Statements.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Presentation transcript:

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 a group of statements a given number of items, the repetition is accomplished using a loop. Loops are iteration structures. Each loop or pass through a group of statements is called an iteration.

3 Repetition Statements Our third control structure: iteration or repetition (completes our three control structures: sequence, selection, iteration) Two main categories of repetition: ◦ definite loop  repeats a predetermined number of times ◦ indefinite loop  repeats a number of times that has not been predetermined.

4 Repetition Forms ◦ Three loop types:  for  while  do while ◦ Three basic constructs  A variable is assigned some value.  The value of the variable changes at some point in the loop.  The loop repeats until the variable reaches a predetermined value, the program then executes the next statement after the loop.

5 Pretest Loops Pretest Loop (Entrance Controlled Loops) ◦ a loop where the control condition (Boolean expression) is tested BEFORE the loop. ◦ If the condition is true, the loop is executed. ◦ If the condition is false the loop is not executed ◦ Therefore, it is possible that these loops may not be executed at all (when the condition is False) ◦ There are two pretest loops  for loop  while loop

6 Post Test Loops Post Test Loops (exit-controlled loop) ◦ a loop where the control condition (Boolean expression) is tested AFTER the loop has been executed. ◦ If the condition is true, the loop is executed again. ◦ If the condition is false the loop is not executed again. ◦ Therefore, this type of loop will always be executed at least once. ◦ There is one post test loop: do…while

7 Fixed repetition loops Fixed repetition loop ◦ a loop used when you know in advance how many repetitions need to be executed, or when you ask the user how many repetitions are needed. ◦ also known as a definite loop:  The programmer knows, or the user chooses the definite number of repetitions necessary to solve the problem. ◦ the “for” loop is:  a fixed repetition loop  and a pretest loop

8 Variable Condition Loops ◦ needed to solve problems where the conditions change within the body of the loop. ◦ Also called indefinite loops:  the loop repeats an indefinite number of iterations until some condition is met, or while some condition is met.  While and do…while loops are variable condition loops.

9 The for Loop The for loop repeats one or more statements a specified number of times. Like an if statement, the for loop uses parentheses. In the parentheses are three items called parameters, which are needed to make a for loop work. Each parameter in a for loop is an expression.

10 The for Loop General form: for( ; ; ) for(counter = 1; counter <= 10; counter++) //Loop Heading cout<< counter << endl; //Loop body

11 Syntax and Semantics of the for Loop for ( ; ; ) termination statement true false initializer update Loop header Loop body

12 The for Loop Internal Logic The control variable is assigned an initial value in the initialization expression The termination condition is evaluated If termination condition is true ◦ the body of the loop is executed and the update expression is evaluated If the termination condition is false ◦ program control is transferred to the first statement following the loop.

13 Code List # include void main( ) { int counter ; // counter variable for (counter = 1; counter <= 3; counter ++) cout << counter << endl; }

14 Counting Backward and Other Tricks A counter variable can also count backward by having the step expression decrement the value rather than increment it. The program in Code List counts backward from 10 to 1. ◦ The counter is initialized to 10. ◦ With each iteration, the decrement operator subtracts 1 from the counter.

15 Code List #include void main ( ) { int counter ; // counter variable for(counter = 10; counter >= 0; counter --) cout << counter << end1; cout << “”End of loop.\n”; }

16 Code List #include void main ( ) { int counter ; // counter variable for (counter = 1; counter <= 100; counter = counter + counter ) cout << counter << end1; }

17 Scope of Loop Control Variable The loop control variable must be declared before it is used. ◦ The rules for the scope of the variable apply here. If the variable is only going to be used as a loop counter, and for nothing else… ◦ You can limit it’s scope by declaring it when it is initialized in the loop for(int counter = 1; counter <=10; ++ counter ) cout<< counter <<endl;// counter is only // referenced in the loop

18 For Loops For loops can count down (decrement) for(int counter=20; counter>=15; --counter) cout<< counter << endl; For loops can count by factors other than one for(int counter=2; counter<=10; counter=counter+2) cout<< counter << endl;

19 For Statement Flexibility The for statement gives you a lot of flexibility. As you have already seen, the step expression can increment, decrement, or count in other ways.

20 Some more examples of for statements are shown in Table

21Accumulator ◦ An accumulator is a variable used to keep a running total or sum of successive values of another variable  i.e. sum = sum + grade;  you should initialize the value of the accumulator before the loop: sum = 0;  the accumulator statement occurs in the body of the loop //lcv means loop control variable sum=0; for(lcv = 1; lcv <= 100; ++lcv) sum = sum + lcv;

22 Using a Statement Block in a for Loop If you need to include more than one statement in the loop, place all the statements that are to be part of the loop inside braces {curly brackets}. The statements in the braces will be repeated each time the loop iterates. The statements that follow the braces are not part of the loop. In Code List an output statement has been added inside the loop of the backward.cpp program. The phrase inside loop will appear with each iteration of the loop.

23 Code-List #include voidmain ( ) { int I; for( i = 10; i >= 0; i--) { cout << i << endl; cout << “Inside Loop\n”; } cout << “End of loop.\n”; }

24 Errors with for Loops Do NOT place a ; (semicolon) directly after the command for in a for loop: Don’t do this for example: for(int i = 1; i <= 10; i ++) ; //Don’t do this! cout << i << end1; This will prevent any lines of code within the loop from being repeated or iterated. This will result in a logic error, the compiler will NOT tell you that there is a syntax error.

Q & A