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

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

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?

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

In this chapter you will learn:

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

C++ PROGRAMMING SKILLS Part 2 Programming Structures

Introduction. C provides two styles of flow control:

LECTURE 5 Control Structures Part 2

REPETITION CONTROL STRUCTURE LOGO

Chapter 4 C Program Control

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

Add Subtract Multiply Divide

Why Is Repetition Needed?

Chapter 2 - Control Structures

DELHI PUBLIC SCHOOL TAPI

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

2.11 Assignment Operators. Assignment expression abbreviations c = c + 3; can be abbreviated as c += 3; using the addition assignment operator

Lecture 7 Tao Wang 1

Structured Programming. Dr. Mohamed Khedr Lecture 9

Looping. Arizona State University 1

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

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

Chapter 2 - Control Structures

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

Chapter 2 - Control Structures

Chapter 4 C Program Control

Structured Program Development

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

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

Control Structures of C++ Programming (2)

C Program Control. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

CS110D: PROGRAMMING LANGUAGE I

Chapter 2: Functions and Control Structures

Theory of control structures

Chapter 5: Control Structures II (Repetition)

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

Introduction to Programming

Programming for Engineers Iteration

Introduction to C++ Lecture Set 2. Introduction to C++ Week 2 Dr Alex Martin 2013 Slide 1

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

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

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

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

Introduction to the Java Basics: Control Flow Statements

CSc Introduc/on to Compu/ng. Lecture 8 Edgardo Molina Fall 2011 City College of New York

Prepared by: Shraddha Modi

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

Chapter 3 Structured Program Development

Name SECTION: 12:45 2:20. True or False (12 Points)

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

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

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

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

Control Structures II. Repetition (Loops)

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

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

CS313D: ADVANCED PROGRAMMING LANGUAGE

CSE123 LECTURE 3-1. Program Design and Control Structures Repetitions (Loops) 1-1

Introduction to C/C++ Lecture 3 - Program Flow Control

Day06 A. Young W. Lim Mon. Young W. Lim Day06 A Mon 1 / 16

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.

PIC 10A Flow control. Ernest Ryu UCLA Mathematics

Control Statements. Musa M. Ameen Computer Engineering Dept.

Sudeshna Sarkar Dept. of Computer Science & Engineering. Indian Institute of Technology Kharagpur

CompSci 125 Lecture 11

Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5-1

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

3 The L oop Control Structure

Flow Control. CSC215 Lecture

Computer Programming: C++

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

Quiz Determine the output of the following program:

CS106X Handout 03 Autumn 2012 September 24 th, 2012 Getting Started

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

C++ Programming Language Lecture 2 Problem Analysis and Solution Representation

Chapter 5: Loops and Files

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

CPE 112 Spring 2015 Exam III (100 pts) April 8, True or False (12 Points)

Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

SFU CMPT Topic: Control Statements

204111: Computer and Programming

Control Statements: Part Pearson Education, Inc. All rights reserved.

Unit 3 Decision making, Looping and Arrays

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

Day06 A. Young W. Lim Wed. Young W. Lim Day06 A Wed 1 / 26

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

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

Outline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples.

BIL101E: Introduction to Computers and Information systems Lecture 8

Object-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering

Programming Fundamentals

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

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

Second Term ( ) Department of Computer Science Foundation Year Program Umm Al Qura University, Makkah

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

Dept. of CSE, IIT KGP

IS 0020 Program Design and Software Tools

Increment and the While. Class 15

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Transcription:

C++ Programming Lecture 7 Control Structure I (Repetition) Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department

while Repetition Structure I Repetition structure Programmer specifies an action to be repeated while some condition remains true. Also called looping or simply loop. Psuedocode while there are more items on my shopping list Purchase next item and cross it off my list while loop repeated until condition becomes false where the next line of code after while loop will be executed. Example int product = 2; while ( product <= 1000 ) product = 2 * product; The body of the while loop is the code block contained within the braces after the while, otherwise it is the first statement after the while only. 2

while Repetition Structure II Flowchart of while loop. Infinite loop: Logical error in the while structure. The condition of the while is always true, i.e. the body of the while loop does modify the condition value. product <= 1000 false true product = 2 * product 3

Essentials of Counter-Controlled Repetition Counter-controlled repetition requires: The name of a control variable (or loop counter). The initial value of the control variable. The condition that tests for the final value of the control variable (i.e., whether looping should continue). The increment (or decrement) by which the control variable is modified each time through the loop. Example: int counter =1; //initialization while (counter <= 10){ //repetition condition cout << counter << endl; ++counter; //increment } As possible avoid the usage of floating point counter values since floating points are approximate. 4

for Repetition Structure I Handles all the details of counter-controlled repetition in a concise way. The general format when using for loops is: for (initialization; LoopContinuationTest; increment/decrement ) statement Example: for( int counter = 1; counter <= 10; counter++ ) cout << counter << endl; Prints the integers from one to ten Pay attention to the off-by-one error. 5

for Repetition Structure II After the condition of the for is violated, the first statement after the for loop is executed. The for loop body is the code block after it (if braces exist) otherwise it is the first statement after for structure. for loops can usually be rewritten as while loops: initialization; while ( loopcontinuationtest){ statement increment; } Initialization and increment as comma-separated lists for (int i = 0, j = 0; j + i <= 10; j++, i++) cout << j + i << endl; 6

for Repetition Structure III Scope of the counter variable defined inside for loop differs based on the used C++ compiler: Known only inside for structure. Or known inside the whole program. The three parts of the for loop are optional, if condition is omitted this will create an infinite loop since the compiler assumes that the for condition is true. for loop parts can contain arithmetic expressions. Flowchart of the for loop is similar to the while loop. 7

Examples Using for Loop On board. 8

do...while Repetition Structure I The do/while repetition structure is similar to the while structure, Condition for repetition tested after the body of the loop is executed Format: do { statement } while ( condition ); Example (letting counter = 1): do { cout << counter << " "; } while (++counter <= 10); This prints the integers from 1 to 10 Pay attention to post/pre increment/decrement. All actions are performed at least once. 9

do...while Repetition Structure II Flowchart of do...while loop action(s) condition true false 10

break Statement break Causes immediate exit from a while, for, do/while or switch structure Program execution continues with the first statement after the structure Common uses of the break statement: Escape early from a loop Skip the remainder of a switch structure Using break outside a loop or switch (e.g. inside if/else) statement is a syntax error. 11

continue Statement continue Skips the remaining statements in the body of a while, for or do/while structure and proceeds with the next iteration of the loop. Also, can be used with switch. In while and do/while, the loop-continuation test is evaluated immediately after the continue statement is executed In the for structure, the increment/decrement expression is executed, then the loop-continuation test is evaluated Using continue outside a loop or switch (e.g. inside if/else) statement is a syntax error. 12

Additional Notes This lecture covers the following material from the textbook: Chapter 2: Sections 2.7, 2.13, 2.14, 2.15, 2.17, 2.18 13