C++ Programming: From Problem Analysis to Program Design, Third Edition

Similar documents
C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 5: Control Structures II (Repetition)

Chapter 5: Control Structures II (Repetition) Objectives (cont d.) Objectives. while Looping (Repetition) Structure. Why Is Repetition Needed?

Why Is Repetition Needed?

REPETITION CONTROL STRUCTURE LOGO

Chapter 5: Control Structures II (Repetition)

Islamic University of Gaza Computer Engineering Dept. C++ Programming. For Industrial And Electrical Engineering By Instructor: Ruba A.

Java. Programming: Chapter Objectives. Why Is Repetition Needed? Chapter 5: Control Structures II. Program Design Including Data Structures

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

INTRODUCTION TO C++ PROGRAM CONTROL. Dept. of Electronic Engineering, NCHU. Original slides are from

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

C++ Programming Lecture 7 Control Structure I (Repetition) Part I

LECTURE 5 Control Structures Part 2

Introduction. C provides two styles of flow control:

CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

Chapter 5 Control Statements: Part 2 Section 5.2 Essentials of Counter-Controlled Repetition

Control Structures. Control Structures Conditional Statements COMPUTER PROGRAMMING. Electrical-Electronics Engineering Dept.

Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.

8. Control statements

C++ Programming: From Problem Analysis to Program Design, Third Edition

Programming Language. Control Structures: Repetition (while) Eng. Anis Nazer Second Semester

C++ Final Exam 2017/2018

Repetition and Loop Statements Chapter 5

Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.

Chapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++

Theory of control structures

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 4: Control Structures I (Selection)

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Quiz Determine the output of the following program:

Lecture 7 Tao Wang 1

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. Looping. ++ is the increment operator.

In this chapter you will learn:

*Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* COMPUTER PROGRAMMING LECTURE 05 LOOPS IMRAN IHSAN

CS110D: PROGRAMMING LANGUAGE I

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

Chapter 3. More Flow of Control. Copyright 2008 Pearson Addison-Wesley. All rights reserved.

Chapter 2: Functions and Control Structures

Chapter Overview. More Flow of Control. Flow Of Control. Using Boolean Expressions. Using Boolean Expressions. Evaluating Boolean Expressions

Control Statements. Musa M. Ameen Computer Engineering Dept.

Control Structures II. Repetition (Loops)

causing a set of statements (the body) to be executed repeatedly. C++ provides three control structures to support iteration (or looping).

Repetition, Looping CS101

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS240 BRANCHING STATEMENTS

CIS 3260 Intro to Programming in C#

CS112 Lecture: Repetition Statements

Chapter 5: Loops and Files

Programming Basics and Practice GEDB029 Decision Making, Branching and Looping. Prof. Dr. Mannan Saeed Muhammad bit.ly/gedb029

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures

An Introduction to Programming with C++ Sixth Edition. Chapter 7 The Repetition Structure

causing a set of statements (the body) to be executed repeatedly. C++ provides three control structures to support iteration (or looping).

CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING Chapter 4: Repetition Control Structure

Increment and the While. Class 15

STUDENT LESSON A12 Iterations

CS112 Lecture: Loops

204111: Computer and Programming

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

V3 1/3/2015. Programming in C. Example 1. Example Ch 05 A 1. What if we want to process three different pairs of integers?

Flow Control. CSC215 Lecture

CS 106 Introduction to Computer Science I

Loops and Files. of do-while loop

Chapter 5: Control Structures

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Control Structures of C++ Programming (2)

Introduction. Pretest and Posttest Loops. Basic Loop Structures ว ทยาการคอมพ วเตอร เบ องต น Fundamentals of Computer Science

Looping. Arizona State University 1

Chapter 3. More Flow of Control

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

3 The L oop Control Structure

L o o p s. for(initializing expression; control expression; step expression) { one or more statements }

Repetition, Looping. While Loop

C++ PROGRAMMING SKILLS Part 2 Programming Structures

Chapter 4 C Program Control

Repetition Structures

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

Control structures in C. Going beyond sequential

Programming for Experimental Research. Flow Control

Programming - 1. Computer Science Department 011COMP-3 لغة البرمجة 1 لطالب كلية الحاسب اآللي ونظم المعلومات 011 عال- 3

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. The Increment and Decrement Operators

DELHI PUBLIC SCHOOL TAPI

Structured Programming. Flowchart Symbols. Structured Programming. Selection. Sequence. Control Structures ELEC 330 1

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Chapter 5: Prefix vs. Postfix 8/19/2018. The Increment and Decrement Operators. Increment and Decrement Operators in Program 5-1

Introduction to the Java Basics: Control Flow Statements

Chapter 2 - Control Structures

Iteration. CSE / ENGR 142 Programming I. Chapter 5. Motivating Loops. What s Wrong with Fahrenheit/Celsius Program? One More Type of Control Flow

Chapter 4 - Notes Control Structures I (Selection)

Unit 7. 'while' Loops

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

5. Control Statements

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.5. for loop and do-while loop

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

Flow of Control: Loops. Objectives. Java Loop Statements: Outline 6/27/2014. Chapter 4

Loops / Repetition Statements. There are three loop constructs in C. Example 2: Grade of several students. Example 1: Fixing Bad Keyboard Input

n Group of statements that are executed repeatedly while some condition remains true

Transcription:

C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 5: Control Structures II (Repetition) Why Is Repetition Needed? Repetition allows you to efficiently use variables Can input, add, and average multiple numbers using a limited number of variables For example, to add five numbers: Declare a variable for each number, input the numbers and add the variables together Create a loop that reads a number into a variable and adds it to a variable that contains the sum of the numbers C++ Programming: From Problem Analysis to Program Design, Third Edition 3 Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct and use countcontrolled, sentinel-controlled, flag-controlled, and EOF-controlled repetition structures Examine break and continue statements Discover how to form and use nested control structures The while Loop The general form of the while statement is: while (expression) statement while is a reserved word Statement can be simple or compound Expression acts as a decision maker and is usually a logical expression Statement is called the body of the loop The parentheses are part of the syntax C++ Programming: From Problem Analysis to Program Design, Third Edition 2 C++ Programming: From Problem Analysis to Program Design, Third Edition 4 1

The while Loop Expression provides an entry condition Statement executes if the expression initially evaluates to true Loop condition is then reevaluated Statement continues to execute until the expression is no longer true C++ Programming: From Problem Analysis to Program Design, Third Edition 5 The while Loop Infinite loop: continues to execute endlessly Can be avoided by including statements in the loop body that assure exit condition will eventually be false C++ Programming: From Problem Analysis to Program Design, Third Edition 6 2

Sentinel-Controlled while Loops Sentinel variable is tested in the condition and loop ends when sentinel is encountered C++ Programming: From Problem Analysis to Program Design, Third Edition 11 Counter-Controlled while Loops If you know exactly how many pieces of data need to be read, the while loop becomes a counter-controlled loop Flag-Controlled while Loops A flag-controlled while loop uses a bool variable to control the loop The flag-controlled while loop takes the form: C++ Programming: From Problem Analysis to Program Design, Third Edition 10 C++ Programming: From Problem Analysis to Program Design, Third Edition 12 3

The for Loop The general form of the for statement is: for (initial statement; loop condition; statement update statement) The initial statement, loop condition, and update statement are called for loop control statements The for loop executes as follows: 1. The initial statement executes. 2. The loop condition is evaluated. If the loop condition evaluates to true i. Execute the for loop statement. ii. Execute the update statement (the third expression in the parentheses). 3. Repeat Step 2 until the loop condition evaluates to false. The initial statement usually initializes a variable (called the for loop control, or for indexed, variable). In C++, for is a reserved word. C++ Programming: From Problem Analysis to Program Design, Third Edition 13 4

The for Loop (comments) A semicolon at the end of the for statement (just before the body of the loop) is a semantic error. In this case, the action of the for loop is empty. In the for statement, if the loop condition is omitted, it is assumed to be true. In a for statement, you can omit all three statements initial statement, loop condition, and update statement. The following is a legal for loop: for (;;) cout << "Hello" << endl; C++ Programming: From Problem Analysis to Program Design, Third Edition 19 The for Loop (comments) The following are some comments on for loops: If the loop condition is initially false, the loop body does not execute. The update expression, when executed, changes the value of the loop control variable (initialized by the initial expression), which eventually sets the value of the loop condition to false. The for loop body executes indefinitely if the loop condition is always true. C++ allows you to use fractional values for loop control variables of the double type (or any real data type). Because different computers can give these loop control variables different results, you should avoid using such variables. C++ Programming: From Problem Analysis to Program Design, Third Edition 18 5

The do while Loop The general form of a do...while statement is: do statement while (expression); The statement executes first, and then the expression is evaluated If the expression evaluates to true, the statement executes again As long as the expression in a do...while statement is true, the statement executes C++ Programming: From Problem Analysis to Program Design, Third Edition 21 The do while Loop To avoid an infinite loop, the loop body must contain a statement that makes the expression false The statement can be simple or compound If compound, it must be in braces do...while loop has an exit condition and always iterates at least once (unlike for and while) C++ Programming: From Problem Analysis to Program Design, Third Edition 22 6

break & continue Statements The break statement is used for two purposes: 1. To exit early from a loop 2. To skip the remainder of the switch structure After the break statement executes, the program continues with the first statement after the structure The use of a break statement in a loop can eliminate the use of certain (flag) variables C++ Programming: From Problem Analysis to Program Design, Third Edition 27 break & continue Statements break and continue alter the flow of control When the break statement executes in a repetition structure, it immediately exits The break statement, in a switch structure, provides an immediate exit The break statement can be used in while, for, and do...while loops break & continue Statements continue is used in while, for, and do while structures When executed in a loop It skips remaining statements and proceeds with the next iteration of the loop C++ Programming: From Problem Analysis to Program Design, Third Edition 26 C++ Programming: From Problem Analysis to Program Design, Third Edition 28 7

break & continue Statements Nested Control Structures In a while and do while structure Expression (loop-continue test) is evaluated immediately after the continue statement In a for structure, the update statement is executed after the continue statement Then the loop condition executes Since five lines are to be printed, we start with the following for statement for (i = 1; i <= 5 ; i++) The value of i in the first iteration is 1, in the second iteration it is 2, and so on Can use the value of i as limit condition in another for loop nested within this loop to control the number of starts in a line C++ Programming: From Problem Analysis to Program Design, Third Edition 29 C++ Programming: From Problem Analysis to Program Design, Third Edition 31 Nested Control Structures Suppose we want to create the following pattern * ** *** **** ***** In the first line, we want to print one star, in the second line two stars and so on Nested Control Structures The syntax is: for (i = 1; i <= 5 ; i++) { for (j = 1; j <= i; j++) cout << "*"; cout << endl; } C++ Programming: From Problem Analysis to Program Design, Third Edition 30 C++ Programming: From Problem Analysis to Program Design, Third Edition 32 8

Nested Control Structures What pattern does the code produce if we replace the first for statement with the following? for (i = 5; i >= 1; i--) Answer: ***** **** *** ** * Summary while: expression is the decision maker, and the statement is the body of the loop In a counter-controlled while loop, Initialize counter before loop Body must contain a statement that changes the value of the counter variable A sentinel-controlled while loop uses a sentinel to control the while loop C++ Programming: From Problem Analysis to Program Design, Third Edition 33 C++ Programming: From Problem Analysis to Program Design, Third Edition 35 Summary C++ has three looping (repetition) structures: while, for, and do while while, for, and do are reserved words while and for loops are called pre-test loops do...while loop is called a post-test loop while and for may not execute at all, but do...while always executes at least once Summary for loop: simplifies the writing of a countcontrolled while loop Executing a break statement in the body of a loop immediately terminates the loop Executing a continue statement in the body of a loop skips to the next iteration After a continue statement executes in a for loop, the update statement is the next statement executed C++ Programming: From Problem Analysis to Program Design, Third Edition 34 C++ Programming: From Problem Analysis to Program Design, Third Edition 36 9

Example looping Write C++ program to print following pattern: i) ********** ********** ********** ********** ii) * ** *** **** ***** iii) * ** *** **** ***** iv) * *** ***** ******* ********* v) 1 222 33333 4444444 555555555 vi) 1 212 32123 4321234 543212345 Loops C++ Programming: From Problem Analysis to Program Design, Third Edition 37 10