CPS120 Introduction to Computer Science Iteration (Looping)

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

CS0004: Introduction to Programming Repetition – Do Loops.
CHAPTER 5: LOOP STRUCTURES Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
LOOP / REPETITION while loop. for loop do/while loop We assume that loops are not meant to be infinite. That is, there should always be a way out of the.
Do/Loops A loop repeats a series of instructions. An iteration is a single execution of the statement(s) in the loop. Used when the exact number of iterations.
Computer Science 1620 Loops.
1 Lecture 11:Control Structures II (Repetition) (cont.) Introduction to Computer Science Spring 2006.
Introduction to Computers and Programming Lecture 9: For Loops New York University.
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.
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.
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.
New Mexico Computer Science For All More Looping in NetLogo Maureen Psaila-Dombrowski.
While Loops and Do Loops. Suppose you wanted to repeat the same code over and over again? System.out.println(“text”); System.out.println(“text”); System.out.println(“text”);
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
CPS120: Introduction to Computer Science Decision Making in Programs.
CPS120 Introduction to Computer Science Iteration (Looping)
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.
Control Structures By Shyam Gurram. Control Structure In this chapter we have two different types of structures. Conditional Structure Iterative Control.
Current Assignments Homework 2 is available and is due in three days (June 19th). Project 1 due in 6 days (June 23 rd ) Write a binomial root solver using.
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,
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
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.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
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.
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. 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.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
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.
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.
Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop 
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Chapter 3 Loops Section 3.3 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
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
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 DO 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

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