Chapter 6 Repetition Objectives ❏ To understand basic loop concepts:

Slides:



Advertisements
Similar presentations
Chapter 4: Control Structures I (Selection)
Advertisements

Repetition control structures
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
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.
Repeating Actions While and For Loops
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Chapter 5: Control Structures II (Repetition)
CS1061: C Programming Lecture 8: Repetition A. O’Riordan, 2004.
Chapter 5: Repetition Statements. In this chapter, you will learn about: Basic loop structures while loops Interactive while loops for loops Loop programming.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand basic loop concepts: ■ pretest loops and post-test loops ■ loop.
Control Structures - Repetition Chapter 5 2 Chapter Topics Why Is Repetition Needed The Repetition Structure Counter Controlled Loops Sentinel Controlled.
Computer Programming Basics Assistant Professor Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea.
Computer Science: A Structured Programming Approach Using C1 6-9 Recursion In general, programmers use two approaches to writing repetitive algorithms.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Programming with C# Iteration LECTURE 3. Summary of last lecture SequenceSelectionif and switch statementsCastingRandom.
Chapter 5 Control Structures: Loops 5.1 The while Loop The while loop is probably the most frequently used loop construct. The while loop is a conditional.
 An instruction or group of instructions.  Computer executes program repeatedly for specified number of times. Or until some terminating conditions are.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Chapter 4 Loops Write code that prints out the numbers Very often, we want to repeat a (group of) statement(s). In C++, we have 3 major ways of.
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.
Chapter 6 Looping CS185/09 - Introduction to Programming Caldwell College.
1 Objectives ❏ To understand basic loop concepts: ■ pretest loops and post-test loops ■ loop initialization and updating ■ event and counter controlled.
Repetition Control Structures Simple Program Design Third Edition A Step-by-Step Approach 5.
CHAPTER 3 CONTROL STRUCTURES ( REPETITION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement.
Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Algorithms Computer Science: An Overview Tenth Edition by J. Glenn.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
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.
Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 5 Repetition.
Computer Science: A Structured Programming Approach Using C1 6-6 Loop Examples This section contains several short examples of loop applications. Each.
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.
1 1 Additional Control Structures Chapter 9 2 New and Improved... Ways to branch Ways to write loops Understanding the break and continue statements.
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.
Computer Science: A Structured Programming Approach Using C1 5-5 Incremental Development Part II In Chapter 4, we introduced the concept of incremental.
While ( number
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Chapter 5: Looping. Using the while Loop Loop – A structure that allows repeated execution of a block of statements Loop body – A block of statements.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand how decisions are made in a computer ❏ To understand the logical.
CHAPTER 6: REPETITION AND LOOP STATEMENTS Learning outcomes  Define the concept of repetition structure.  Specify.
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.
Introduction to C++ Programming Language
Statements (6 of 6) A statement causes an action to be performed by the program. It translates directly into one or more executable computer instructions.
Selection—Making Decisions
Topics discussed in this section:
Quick Test What do you mean by pre-test and post-test loops in C?
( Iteration / Repetition / Looping )
Computer Science Faculty
Topics discussed in this section:
Loops in C C has three loop statements: the while, the for, and the do…while. The first two are pretest loops, and the the third is a post-test loop. We.
Control Structures - Repetition
Control Structure Senior Lecturer
Topics discussed in this section:
MSIS 655 Advanced Business Applications Programming
Topics discussed in this section:
Repetition Control Structure
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Chapter 6: Repetition Statements
Computer Science Core Concepts
Topics discussed in this section:
Repetition Statements (Loops) - 2
Introduction to Computer Science
Statements in C Programming
The structure of programming
Thinking procedurally
Presentation transcript:

Chapter 6 Repetition Objectives ❏ To understand basic loop concepts: ■ pretest loops and post-test loops ■ loop initialization and updating ■ event and counter controlled loops ❏ To understand and be able to select the best loop construct for a given problem. ❏ To write programs that use the while, for, or do ... while statements. ❏ To understand the basic concepts and usage of recursion algorithms. ❏ To understand and be able to determine the efficiency of an algorithm through an analysis of its looping constructs. Computer Science: A Structured Programming Approach Using C

6-1 Concept of a loop The real power of computers is in their ability to repeat an operation or a series of operations many times. This repetition, called looping, is one of the basic structured programming concepts. Each loop must have an expression that determines if the loop is done. If it is not done, the loop repeats one more time; if it is done, the loop terminates. Computer Science: A Structured Programming Approach Using C

FIGURE 6-1 Concept of a Loop Computer Science: A Structured Programming Approach Using C

6-2 Pretest and Post-test Loops We need to test for the end of a loop, but where should we check it—before or after each iteration? We can have either a pre- or a post-test terminating condition. In a pretest loop , the condition is checked at the beginning of each iteration. In a post-test loop, the condition is checked at the end of each iteration. Computer Science: A Structured Programming Approach Using C

Note Pretest Loop In each iteration, the control expression is tested first. If it is true, the loop continues; otherwise, the loop is terminated. Post-test Loop In each iteration, the loop action(s) are executed. Then the control expression is tested. If it is true, a new iteration is started; otherwise, the loop terminates. Computer Science: A Structured Programming Approach Using C

FIGURE 6-2 Pretest and Post-test Loops Computer Science: A Structured Programming Approach Using C

FIGURE 6-3 Two Different Strategies for Doing Exercises Computer Science: A Structured Programming Approach Using C

FIGURE 6-4 Minimum Number of Iterations in Two Loops Computer Science: A Structured Programming Approach Using C

Topics discussed in this section: 6-3 Initialization and Updating In addition to the loop control expression, two other processes, initialization and updating, are associated with almost all loops. Topics discussed in this section: Loop Initialization Loop Update Computer Science: A Structured Programming Approach Using C

FIGURE 6-5 Loop Initialization and Updating Computer Science: A Structured Programming Approach Using C

FIGURE 6-6 Initialization and Updating for Exercise Computer Science: A Structured Programming Approach Using C

Topics discussed in this section: 6-4 Event- and Counter-Controlled Loops 6-4 Event- and Counter-Control All the possible expressions that can be used in a loop limit test can be summarized into two general categories: event-controlled loops and counter-controlled loops. Topics discussed in this section: Event-Controlled Loops Counter-Controlled Loops Loop Comparison Computer Science: A Structured Programming Approach Using C

FIGURE 6-7 Event-controlled Loop Concept Computer Science: A Structured Programming Approach Using C

FIGURE 6-8 Counter-controlled Loop Concept Computer Science: A Structured Programming Approach Using C

Table 6-1 Loop Comparisons Computer Science: A Structured Programming Approach Using C

Topics discussed in this section: 6-5 Loops in C C has three loop statements: the while, the for, and the do…while. The first two are pretest loops, and the the third is a post-test loop. We can use all of them for event-controlled and counter-controlled loops. Topics discussed in this section: The while Loop The for Loop The do…while Loop The Comma Expression Computer Science: A Structured Programming Approach Using C

FIGURE 6-9 C Loop Constructs Computer Science: A Structured Programming Approach Using C

FIGURE 6-10 The while Statement Computer Science: A Structured Programming Approach Using C

FIGURE 6-11 Compound while Statement Computer Science: A Structured Programming Approach Using C

Process-control System Example PROGRAM 6-1 Process-control System Example Computer Science: A Structured Programming Approach Using C

A while Loop to Print Numbers PROGRAM 6-2 A while Loop to Print Numbers Computer Science: A Structured Programming Approach Using C

A while Loop to Print Numbers PROGRAM 6-2 A while Loop to Print Numbers Computer Science: A Structured Programming Approach Using C

Adding a List of Numbers PROGRAM 6-3 Adding a List of Numbers Computer Science: A Structured Programming Approach Using C

Adding a List of Numbers PROGRAM 6-3 Adding a List of Numbers Computer Science: A Structured Programming Approach Using C

FIGURE 6-12 for Statement Computer Science: A Structured Programming Approach Using C

FIGURE 6-13 Compound for Statement Computer Science: A Structured Programming Approach Using C

more natural for counting loops. Note A for loop is used when a loop is to be executed a known number of times. We can do the same thing with a while loop, but the for loop is easier to read and more natural for counting loops. Computer Science: A Structured Programming Approach Using C

FIGURE 6-14 Comparing for and while Loops Computer Science: A Structured Programming Approach Using C

PROGRAM 6-4 Example of a for Loop Computer Science: A Structured Programming Approach Using C

PROGRAM 6-4 Example of a for Loop Computer Science: A Structured Programming Approach Using C

A Simple Nested for Loop PROGRAM 6-5 A Simple Nested for Loop Computer Science: A Structured Programming Approach Using C

A Simple Nested for Loop PROGRAM 6-5 A Simple Nested for Loop Computer Science: A Structured Programming Approach Using C

FIGURE 6-15 do…while Statement Computer Science: A Structured Programming Approach Using C

PROGRAM 6-6 Two Simple Loops Computer Science: A Structured Programming Approach Using C

PROGRAM 6-6 Two Simple Loops Computer Science: A Structured Programming Approach Using C

FIGURE 6-16 Pre- and Post-test Loops Computer Science: A Structured Programming Approach Using C

Adding a List with the do…while PROGRAM 6-7 Adding a List with the do…while Computer Science: A Structured Programming Approach Using C

Adding a List with the do…while PROGRAM 6-7 Adding a List with the do…while Computer Science: A Structured Programming Approach Using C

FIGURE 6-17 Nested Comma Expression Computer Science: A Structured Programming Approach Using C

Comparison of while and do…while PROGRAM 6-8 Comparison of while and do…while Computer Science: A Structured Programming Approach Using C

Comparison of while and do…while PROGRAM 6-8 Comparison of while and do…while Computer Science: A Structured Programming Approach Using C