CPS120 Introduction to Computer Science Iteration (Looping)

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

CHAPTER 5: LOOP STRUCTURES Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
Computer Science 1620 Loops.
1 Lecture 11:Control Structures II (Repetition) (cont.) Introduction to Computer Science Spring 2006.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
1 Parts of a Loop (reminder) Every loop will always contain three main elements: –Priming: initialize your variables. –Testing: test against some known.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Chapter 5: Control Structures II (Repetition)
Chapter 5: Loops and Files.
Objectives You should be able to describe:
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
An Object-Oriented Approach to Programming Logic and Design Chapter 6 Looping.
Control Structures - Repetition Chapter 5 2 Chapter Topics Why Is Repetition Needed The Repetition Structure Counter Controlled Loops Sentinel Controlled.
Flow of Control. 2 Control Structures Control structure: An instruction that determines the order in which other instructions in a program are executed.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
CPS120 Introduction to Computer Programming The Programming Process.
PHP Conditional Statements Conditional statements in PHP are used to perform different actions based on different conditions. Conditional Statements Very.
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
Chapter 5 Loops. Overview u Loop Statement Syntax  Loop Statement Structure: while, for, do-while u Count-Controlled Loops u Nested Loops u Loop Testing.
C++ for Engineers and Scientists, Third Edition1 Objectives In this chapter, you will learn about: Basic loop structures while loops Interactive while.
Control Structures II (Repetition). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
Controlling Execution Dong Shao, Nanjing Unviersity.
Chapter 8 Repetition Statements. Introduction Iteration - process of looping or the repetition of one or more statements Loop body - the statement, or.
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,
L OO P S While writing a program, there may be a situation when you need to perform some action over and over again. In such situation you would need.
Control Structures CPS120: Introduction to Computer Science Lecture 5.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
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.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
CPS120 Introduction to Computer Science Iteration (Looping)
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
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.
A loop is a repetition control structure. body - statements to be repeated control statement - decides whether another repetition needs to be made leading.
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.
A First Book of C++ Chapter 5 Repetition.
Looping ROBERT REVAES. Logical Operators  && AND  Both have to be true for it to evaluate to be true.  || OR  One or the other has to be true for.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Chapter 5 Repetition. 2 Objectives You should be able to describe: The while Statement cin within a while Loop The for Statement The do Statement Common.
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.
COMP Loop Statements Yi Hong May 21, 2015.
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Introduction To Repetition The for loop
Quick Test What do you mean by pre-test and post-test loops in C?
CPS120: Introduction to Computer Science
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Chapter 8: More on the Repetition Structure
CPS120: Introduction to Computer Science
Alternate Version of STARTING OUT WITH C++ 4th Edition
Repetition Statements (Loops) - 2
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
CSC215 Lecture Control Flow.
CPS120: Introduction to Computer Science
Presentation transcript:

CPS120 Introduction to Computer Science Iteration (Looping)

Looping Loops allow your program to repeat groups of statements a specified number of times. Loops are an example of an iteration structure.

Iterate A program loop is a form of iteration. A computer can be instructed to repeat instructions under certain conditions.

Looping with Keyword goto Uses concepts covered in decision making discussion Implemented with a simple if..then structure loop: counter ++; cout <<"Processing: Counter = "<< counter << endl; if (counter < 5) goto loop;

Iteration Control Structures Iteration control structures are looping mechanisms. Loops repeat an activity until stopped. The location of the stopping mechanism determines how the loop will work: Leading decisions Trailing decisions

Leading Decisions If the stop is at the beginning of the iteration, then the control is called a leading decision. The command WHILE performs the iteration and places the stop at the beginning.

‘For’ Loops A for loop always executes a specific number of iterations. Use a for loop when you know exactly how many times a set of statements must be repeated A for loop is called a determinant or definite loop because the programmer knows exactly how many times it will iterate

Syntax of a for Loop for (initializing expression; control expression; step expression) { // one or more statements } The initializing expression sets the counter variable for the loop to its initial value. The control expression ends the loop at the specified moment. The step expression changes the counter variable Semi-colons, not commas, divide the expressions

Things to Remember About For Loops It is possible to have variable increase by a value other than one for (num = 1; num <= 10; num = num + 3) It is possible to initialize the loop variable within the initializing expression. But I recommend against it Declare loop variables at the top of the function where they belong If an if statement only contains one body statement, the compiler doesn't require the use of curly braces

Looping Statements The while statement is used to repeat a course of action Let’s look at two distinct types of repetitions

Looping Statements Count-controlled loops Repeat a specified number of times Use of a special variable called a loop control variable Flow of control of while statement

Looping Statements Count-controlled loops

Looping Statements Event-controlled loops The number of repetitions is controlled by an event that occurs within the body of the loop itself

Looping Statements Event-controlled loops

WHILE Loop No Yes Entry Exit Test condition p Loop statement a

While Loops A while loop does not necessarily iterate a specified number of times As long as its control expression is true, a while loop will continue to iterate An indeterminate or indefinite loop because only at run-time can it be determined how many times it will iterate While is considered a top-checking loop The control expression is located on the first line of code If the control expression initially evaluates to FALSE, the loop will not execute even once.

While Loop Syntax while (control expression) { // one or more statements } The control expression must evaluate to TRUE in order for the while loop to iterate even once

While (true) Loops This type of loop will never end unless you put a break in it while (true) { counter ++; if (counter > 10) break; }

Trailing Decisions If the stop is at the end of the iteration, the control mechanism is called a trailing decision. The command DO / WHILE performs the iteration and puts the stop at the end of the loop.

DO WHILE Loop Loop statement a NoYes Entry Test condition p Exit

Do While Loops As with a while loop, a do while loop does not necessarily iterate a specified number of times A do while loop will iterate at least one time because its control expression is placed at the end of the loop Considered to be an indeterminate or indefinite loop Considered to be a bottom-checking loop, since the control expression is located after the body of the loop

Do While Syntax do { // body statements would be placed here }while (control expression); Don't forget to include the required semicolon after the control expression

Break and Continue Statements A break statement is used to stop the execution of a loop immediately and to continue by executing the statement that comes directly after the loop A continue statement is used to stop the execution of the statements in the loop's body on that particular iteration and to continue by starting the next iteration of the loop

Nested Loops A loop of any kind may be placed in another loop (of any kind). Be sure to entirely encapsulate the inner loop inside of the outer loop, otherwise an error is sure to occur. Two loops are considered to be nested loops if one is enclosed within the other

In Summary Loops goto loops -- simple if…then structure for-- fixed number of loops while -- may never be run while (true)-- always true, needs break do … while-- always run once continue causes while, do… while, and for loops to start over break causes while, do … while, for and switch statements to end