Chapter 2.1 Repetition.

Slides:



Advertisements
Similar presentations
CS0004: Introduction to Programming Repetition – Do Loops.
Advertisements

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.
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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
1 CSE1301 Computer Programming Lecture 10: Iteration (Part 1)
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Objectives You should be able to describe:
Computing Sums and Averages 04/17/15. Totaling Up a List Write a program to input a list of four numbers and output the total.
Chapter 5: Repetition Statements. In this chapter, you will learn about: Basic loop structures while loops Interactive while loops for loops Loop programming.
A First Book of ANSI C Fourth Edition Chapter 3 Processing and Interactive Input.
Chapter 5 Control Structure (Repetition). Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
C++ for Engineers and Scientists, Third Edition1 Objectives In this chapter, you will learn about: Basic loop structures while loops Interactive while.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
2Object-Oriented Program Development Using C++ 3 Basic Loop Structures Loops repeat execution of an instruction set Three repetition structures: while,
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
Control Structures RepetitionorIterationorLooping Part I.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
A First Book of ANSI C Fourth Edition Chapter 5 Repetition.
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.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
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.
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.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
IT CS 200: R EPEATATION Lect. Napat Amphaiphan. T HE ABILITY TO DO THE SAME TASK AGAIN BY AGAIN UNTIL THE CONDITION IS MET LOOP 2.
Introduction to Loop. Introduction to Loops: The while Loop Loop: part of program that may execute > 1 time (i.e., it repeats) while loop format: while.
CNG 140 C Programming (Lecture set 3)
REPETITION CONTROL STRUCTURE
Chapter 5: Control Structures II (Repetition)
EKT120 COMPUTER PROGRAMMING
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Chapter 5: Control Structures II
Chapter 5: Control Structures II
Chapter 5: Control Structures II
Loop Structures.
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
Quick Test What do you mean by pre-test and post-test loops in C?
Control Structures II (Repetition)
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Chapter 5: Control Structures II
Control Structures - Repetition
Control Structures Lecture 7.
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Alternate Version of STARTING OUT WITH C++ 4th Edition
A First Book of ANSI C Fourth Edition
A First Book of ANSI C Fourth Edition
Chapter 6: Repetition Statements
UMBC CMSC 104 – Section 01, Fall 2016
Chapter 5: Control Structures II (Repetition)
Objectives You should be able to describe: The while Statement
More Loops Topics Counter-Controlled (Definite) Repetition
Programming Right from the Start with Visual Basic .NET 1/e
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Relational Operators Logical Operators for Loops.
More Loops Topics Counter-Controlled (Definite) Repetition
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.
More Loops Topics Counter-Controlled (Definite) Repetition
Presentation transcript:

Chapter 2.1 Repetition

A First Book of ANSI C, Fourth Edition Introduction A section of code that is repeated is called a loop: because after the last statement in the code is executed, the program branches, or loops, back to the first statement and starts another repetition through the code Each repetition is also called an iteration or pass through the loop A First Book of ANSI C, Fourth Edition

A First Book of ANSI C, Fourth Edition Basic Loop Structures Constructing a repeating section of code requires that four elements be present: Repetition statement while statement for statement do-while statement Condition A statement that initially sets the condition being tested A statement within the repeating section of code that alters the condition so that it eventually becomes false A First Book of ANSI C, Fourth Edition

Pretest and Posttest Loops 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. A First Book of ANSI C, Fourth Edition

Counter-Controlled Loop the condition is used to keep track of the number of repetitions Also known as a fixed-count loop int count=0; while (count < 5) { printf("Hello\n"); count = count + 1; } printf("Finished\n"); Hello Finished Press any key to continue Number of repetitions is known when entering the loop A First Book of ANSI C, Fourth Edition

Condition-Controlled Loop the tested condition does not depend on a count being achieved, but rather on a specific value being encountered int mark; printf("Enter a mark: "); scanf("%d", &mark); while (mark<0 || mark>100) { printf("Invalid! Enter a new mark: "); } printf("Finished\n"); Enter a mark: -12 Invalid! Enter a new mark: -65 Invalid! Enter a new mark: 1000 Invalid! Enter a new mark: 200 Invalid! Enter a new mark: 56 Finished Press any key to continue Number of repetitions is not known when entering the loop A First Book of ANSI C, Fourth Edition

Sentinel-Controlled Loop a special input value indicates that there is no more data int num; while (num != -1) { printf("Enter a number: "); scanf("%d", &num); } printf("Finished\n"); Enter a number: 12 Enter a number: 23 Enter a number: 20 Enter a number: -1 Finished Press any key to continue Number of repetitions is not known when entering the loop A First Book of ANSI C, Fourth Edition

A First Book of ANSI C, Fourth Edition The while Statement The general form of the while statement is while (expression) statement; The transfer of control back to the start of a while statement to reevaluate the expression is known as a program loop The following is a valid but infinite loop: int count=1; while (count <= 10) printf("%d ",count); A First Book of ANSI C, Fourth Edition

The while Statement (cont.) A First Book of ANSI C, Fourth Edition

The while Statement (cont.) Output is: 1 2 3 4 5 6 7 8 9 10 Output is: 10 9 8 7 6 5 4 3 2 1 A First Book of ANSI C, Fourth Edition

Computing Sums and Averages Using a while Loop A First Book of ANSI C, Fourth Edition

Computing Sums and Averages Using a while Loop (cont.) A First Book of ANSI C, Fourth Edition

Computing Sums and Averages Using a while Loop (cont.) A First Book of ANSI C, Fourth Edition

Computing Sums and Averages Using a while Loop (cont.) Ensures that any previous value present in the storage locations assigned to the variable total is overwritten and the total starts at a correct value Accumulating statement Calculating an average A First Book of ANSI C, Fourth Edition

A First Book of ANSI C, Fourth Edition Sentinels A program, such as Program 5.7, can be made much more general by removing the restriction that exactly four numbers are to be entered The user enters a value for how many numbers will be averaged You can use a sentinel (a data value used to signal either the start or end of a data series) The sentinel values must be selected so as not to conflict with legitimate data values A First Book of ANSI C, Fourth Edition

Sentinels (continued) A First Book of ANSI C, Fourth Edition

Sentinels (continued) One useful sentinel in C is the named constant EOF (End Of File) The actual value of EOF is compiler-dependent, but it is always assigned a code that is not used by any other character EOF is defined in stdio.h A First Book of ANSI C, Fourth Edition

Sentinels (continued) A First Book of ANSI C, Fourth Edition

The break and continue Statements A break forces an immediate exit from while, switch, for, and do-while statements only Sample output 1: int count=1; float num; while(count <= 10) { printf("Enter a number: "); scanf("%f", &num); if (num > 76) printf("You lose!"); break; /* break out of the loop */ } else printf("Keep on truckin!"); count++; /* break jumps to here */ Enter a number: 1 Keep on truckin!Enter a number: 2 Keep on truckin!Enter a number: 20 Keep on truckin!Enter a number: 100 You lose!Press any key to continue Sample output 2: Enter a number: 1 Keep on truckin!Enter a number: 2 Keep on truckin!Enter a number: 3 Keep on truckin!Enter a number: 4 Keep on truckin!Enter a number: 5 Keep on truckin!Enter a number: 6 Keep on truckin!Enter a number: 7 Keep on truckin!Enter a number: 8 Keep on truckin!Enter a number: 9 Keep on truckin!Enter a number: 10 Keep on truckin!Press any key to continue A First Book of ANSI C, Fourth Edition

The break and continue Statements The continue applies to loops only; when a continue statement is encountered in a loop, the next iteration of the loop begins immediately Sample output 1: int count=0; float grade, total=0; while (count < 5) { printf("Enter a grade: "); scanf("%f", &grade); if(grade < 0 || grade > 100) continue; total = total + grade; count = count + 1; } printf("Total is %.2f\n", total); Enter a grade: 10 Enter a grade: 20 Enter a grade: 30 Enter a grade: 40 Enter a grade: 50 Total is 150.00 Press any key to continue Sample output 2: Enter a grade: 10 Enter a grade: 30 Enter a grade: -10 Enter a grade: 1000 Enter a grade: 20 Total is 80.00 Press any key to continue A First Book of ANSI C, Fourth Edition

A First Book of ANSI C, Fourth Edition The Null Statement A semicolon with nothing preceding it is also a valid statement, called the null statement ; Use the null statement where a statement is syntactically required, but no action is needed Null statements typically are used either with while or for statements A First Book of ANSI C, Fourth Edition

A First Book of ANSI C, Fourth Edition The for Statement The for statement combines all four elements required to easily produce a loop on the same line This statement does not require that any of the items in parentheses be present or that they actually be used for initializing or altering the values in the expression statements However, the two semicolons must be present for ( ; count <= 20;) is valid Omitting tested expression results in infinite loop for (initializing list; tested expression; altering list) statement; A First Book of ANSI C, Fourth Edition

The for Statement (continued) A First Book of ANSI C, Fourth Edition

The for Statement (continued) Output is: 2 4 6 8 10 12 14 16 18 20 A First Book of ANSI C, Fourth Edition

The for Statement (continued) A First Book of ANSI C, Fourth Edition

The for Statement (continued) A First Book of ANSI C, Fourth Edition

The for Statement (continued) Comma-separated list A First Book of ANSI C, Fourth Edition

Computing Sums and Averages Using a for Loop A First Book of ANSI C, Fourth Edition

Case Studies: Loop Programming Techniques Technique 1: Selection within a loop Technique 2: Input data validation Technique 3: Interactive loop control Technique 4: Evaluating equations A First Book of ANSI C, Fourth Edition

Technique 1: Selection within a Loop A First Book of ANSI C, Fourth Edition

Technique 2: Input Data Validation Same code used in lines 6-7! A First Book of ANSI C, Fourth Edition

Technique 2: Input Data Validation (cont.) A First Book of ANSI C, Fourth Edition

Technique 3: Interactive Loop Control A First Book of ANSI C, Fourth Edition

Technique 4: Evaluating Equations A First Book of ANSI C, Fourth Edition

Technique 4: Evaluating Equations (cont.) A First Book of ANSI C, Fourth Edition

A First Book of ANSI C, Fourth Edition Nested Loops A First Book of ANSI C, Fourth Edition

Nested Loops (continued) Sample run: i is now 1 j = 1 j = 2 j = 3 j = 4 i is now 2 i is now 3 i is now 4 i is now 5 A First Book of ANSI C, Fourth Edition

Nested Loops (continued) A First Book of ANSI C, Fourth Edition

The do-while Statement A First Book of ANSI C, Fourth Edition

The do-while Statement (cont.) The general form of the do statement is do statement; while (expression); do-while is a posttest loop One type of application is ideally suited for a posttest loop: input data validation application do { printf("\nEnter an ID number: "); scanf("%f", &idNum); } while (idNum < 1000 || idNum > 1999); A First Book of ANSI C, Fourth Edition