Control Structures of C++ Programming (2)

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

Chapter 7 Array. Array. C++, How to Program

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

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

Increment and the While. Class 15

Chapter 9 Classes : A Deeper Look, Part 1

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

REPETITION CONTROL STRUCTURE LOGO

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

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

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

C++, How to Program. Spring 2016 CISC1600 Yanjun Li 1

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

Why Is Repetition Needed?

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

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

Chapter 5: Control Structures II (Repetition)

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

Name Section: M/W T/TH Number Definition Matching (8 Points)

Repetition Structures

In this chapter you will learn:

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

Looping. Arizona State University 1

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

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

Introduction. C provides two styles of flow control:

Name Section: M/W T/TH Number Definition Matching (6 Points)

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

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

DELHI PUBLIC SCHOOL TAPI

LECTURE 5 Control Structures Part 2

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

The University of Alabama in Huntsville Electrical and Computer Engineering CPE Example of Objective Test Questions for Test 4

ECE 122. Engineering Problem Solving with Java

Chapter 5: Loops and Files

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

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.

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

Iteration: Intro. Two types of loops: 1. Pretest Condition precedes body Iterates 0+ times. 2. Posttest Condition follows body Iterates 1+ times

while for do while ! set a counter variable to 0 ! increment it inside the loop (each iteration)

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

Lecture 7 Tao Wang 1

Introduction to Programming

Solving Problems Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

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

C++ Final Exam 2017/2018

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

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?

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

Computer Programming

Unit 7. 'while' Loops

Introduction to Programming

EP578 Computing for Physicists

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

Add Subtract Multiply Divide

EP241 Computing Programming

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

CAMBRIDGE SCHOOL, NOIDA ASSIGNMENT 1, TOPIC: C++ PROGRAMMING CLASS VIII, COMPUTER SCIENCE

CPSC 3740 Programming Languages University of Lethbridge. Control Structures

Computer Programming I - Unit 5 Lecture page 1 of 14

34. Recursion. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

C Programming for Engineers Structured Program

Computer Department. Question (1): State whether each of the following is true or false. Question (2): Select the correct answer from the following:

Boolean Data-Type. Boolean Data Type (false, true) i.e. 3/6/2018. The type bool is also described as being an integer: bool bflag; bflag = true;

CIS 3260 Intro to Programming in C#

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

3/12/2018. Structures. Programming in C++ Sequential Branching Repeating. Loops (Repetition)

SOLUTIONS TO EXERCISES PART ONE: Problem-Solving Techniques

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

CS110D: PROGRAMMING LANGUAGE I

Chapter 3 Structured Program Development

Control Structures (Deitel chapter 4,5)

SFU CMPT Topic: Control Statements

Control Statements. Musa M. Ameen Computer Engineering Dept.

CSI33 Data Structures

Chapter 3 Structured Program Development in C Part II

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

204111: Computer and Programming

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

Programming Fundamentals

4.1. Chapter 4: Simple Program Scheme. Simple Program Scheme. Relational Operators. So far our programs follow a simple scheme

Review Questions I Spring 2010

Class 2: Variables and Memory. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

CSCI 111 Midterm 1, version A Exam Fall Solutions 09.00am 09.50am, Tuesday, October 13, 2015

C++ PROGRAMMING SKILLS Part 2 Programming Structures

UEE1302(1066) F12: Introduction to Computers and Programming Function (II) - Parameter

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

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

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

If Control Construct

Chapter 2 - Control Structures

Review. Outline. Array Pointer Object-Oriented Programming. Fall 2013 CISC2200 Yanjun Li 1. Fall 2013 CISC2200 Yanjun Li 2

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

int x = 5; double y = 3; // Integer division rounds the result down to the nearest whole number. cout << "1a: " << x / 3 << endl; //1

Chapter Overview. C++ Basics. Variables and Assignments. Variables and Assignments. Keywords. Identifiers. 2.1 Variables and Assignments

Review. Outline. Array Pointer Object-Oriented Programming. Fall 2017 CISC2200 Yanjun Li 1. Fall 2017 CISC2200 Yanjun Li 2

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

Building on the foundation. Now that we know a little about cout cin math operators boolean operators making decisions using if statements

Quiz Determine the output of the following program:

Lab Instructor : Jean Lai

Transcription:

Control Structures of C++ Programming (2) CISC1600/1610 Computer Science I/Lab Fall 2016 CISC 1600 Yanjun Li 1 Loops Purpose: Execute a block of code multiple times (repeat) Types: for, while, do/while Continue to execute until the condition is Fall 2016 CISC 1600 Yanjun Li 2 1

for Statement (1) Format: for (Initialization; Test; Update) //the block of code which is executed multiple times Initialization : Initial value of the control variable. Test : A boolean expression. Loop-continuation condition that tests for the final value of the control variable Update: Update the Variable. : Increment/decrement of control variable at each iteration Fall 2016 CISC 1600 Yanjun Li 3 for Statement (2) Assign 1 to counter counter <=10 cout Increment counter Fall 2016 CISC 1600 Yanjun Li 4 2

Question How many times your code is executed? for (int i = 0; i < 4; i++) cout<< loop A! <<endl; Assign 0 to counter counter < 4 cout Increment counter Fall 2016 CISC 1600 Yanjun Li 5 Question How many times your code is executed? for (int i = 1; i < 4; i++) cout<< loop B! <<endl; Assign 1 to counter counter < 4 cout Increment counter Fall 2016 CISC 1600 Yanjun Li 6 3

Question How many times your code is executed? for (int i = 1; i <= 4; i++) cout<< loop C! <<endl; Assign 1 to counter counter <= 4 cout Increment counter Fall 2016 CISC 1600 Yanjun Li 7 Examples Using the for Statement Vary control variable from 1 to 5 in increments of 1 for ( int i = 1; i <= 5; i++ ) Vary control variable from 5 to 1 in increments of -1 for ( int i = 5; i >= 1; i-- ) Vary control variable from 7 to 77 in steps of 7 for ( int i = 7; i <= 77; i += 7 ) Vary control variable from 20 to 2 in steps of -2 for ( int i = 20; i >= 2; i -= 2 ) Vary control variable over the sequence: 2, 5, 8, 11, 14, 17, 20 for ( int i = 2; i <= 20; i += 3 ) Vary control variable over the sequence: 99, 88, 77, 66, 55, 44, 33, 22, 11, 0 for ( int i = 99; i >= 0; i -= 11 ) examplethree.cpp Fall 2016 CISC 1600 Yanjun Li 8 4

while Statement Action repeated while some condition remains. Format //condition variable is initiated while ( Condition ) // the block of code which is executed many times //condition variable is updated while loop repeats until condition becomes Fall 2016 CISC 1600 Yanjun Li 9 while Statement Pseudocode While there are more items on my shopping list Purchase next item and cross it off my list Example int listitem = 3; int shoppingcart = 0; while ( listitem > 0 ) shoppingcart++; listitem --; The variable involved in the condition must be updated inside the loop. Fall 2016 CISC 1600 Yanjun Li 10 5

while Statement Example: int value = 2; while ( value < 100 ) cout<< value << endl; value = value*2; Decision Logic A pretest loop value < 100 update value cout Fall 2016 CISC 1600 Yanjun Li 11 Example of Using while Statement bool flag = ; while (flag == ) int numberone; cout<< Please give me an integer : <<endl; cin >> numberone; if (numberone % 2 == 0) flag = ; What does this program do? (examplefour.cpp) Fall 2016 CISC 1600 Yanjun Li 12 6

Question Which should I use, while or for? while initialization; while ( loopcontinuationcondition ) ; update; for for ( initialization; loopcontinuationcondition; udpate ) ; The number of loops is known in advance. Fall 2016 CISC 1600 Yanjun Li 13 do while Statement do while Similar to while Tests loop-continuation after performing body of loop Loop body always executes at least once Fall 2016 CISC 1600 Yanjun Li 14 7

do while Statement Format //condition variable is initiated do // the block of s which is executed many times //condition variable is updated while ( Condition ); The block of code is always executed at least once. The variable involved in Condition must be updated inside the loop. Fall 2016 CISC 1600 Yanjun Li 15 do while Statement Example int value = 2; do cout << value is << value << endl; value = value * 2; while ( value < 100 ); Decision Logic A posttest loop print update value value < 100 Fall 2016 CISC 1600 Yanjun Li 16 8

break Statement Alter flow of control Causes immediate exit from control structure Used in while, for, do while or switch s Fall 2016 CISC 1600 Yanjun Li 17 Local Variable Variables declared in the loop structure only exist within the block. If the control variable is declared in the initialization expression of for It will be unknown outside the for Example ( exampletwo.cpp ): for (int counter = 1; counter<=4; counter++) int numberone = 4; cout << numberone * << counter << = << numberone*counter << endl; Fall 2016 CISC 1600 Yanjun Li 18 9

Non-local Variables (1) Variables declared outside the loop structure are non-local variables. The values of these variables are kept after finishing each iteration of the loop structure. Example: int sum=0; for (int i=0; i<5; i++) sum +=i; cout << sum of i after 5 iterations is << sum << endl; Fall 2016 CISC 1600 Yanjun Li 19 Non-local Variables (2) Another example ( exampleeight.cpp ): int cost = 0; for (int i=0; i<5; i++) int x, y; //two choices cout << enter the first choice <<endl; cin >> x; if (x == 1) cost += 100; else cost += 200; cout<< enter the second choice <<endl; cin >> y; if (y == 1) cost += 100; cout << cost for car no. << (i+1) << is $ <<cost<<endl; * Is this program correct? Fall 2016 CISC 1600 Yanjun Li 20 10

Reference Reproduced from the Cyber Classroom for C++, How to Program, 5/e by Deitel & Deitel. Reproduced by permission of Pearson Education, Inc. Fall 2016 CISC 1600 Yanjun Li 21 11