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

Similar documents
Lecture 7 Tao Wang 1

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

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

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

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

REPETITION CONTROL STRUCTURE LOGO

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

Chapter 5: Loops and Files

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

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

Increment and the While. Class 15

CSc 10200! Introduction to Computing. Lecture 4-5 Edgardo Molina Fall 2013 City College of New York

Repetition Structures

Lecture 5 Tao Wang 1

Why Is Repetition Needed?

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

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

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

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

To become familiar with array manipulation, searching, and sorting.

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

Flow Control. CSC215 Lecture

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

Looping. Arizona State University 1

VARIABLES & ASSIGNMENTS

EP241 Computing Programming

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

CHAPTER : 9 FLOW OF CONTROL

An Introduction to Programming with C++ Sixth Edition. Chapter 8 More on the Repetition Structure

CMSC 202 Midterm Exam 1 Fall 2015

Chapter 5: Control Structures II (Repetition)

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Lab Instructor : Jean Lai

Programming Language. Functions. Eng. Anis Nazer First Semester

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

In this chapter you will learn:

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

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS

Control Structures of C++ Programming (2)

Lecture 23: Pointer Arithmetic

Structured Programming. Flowchart Symbols. Structured Programming. Selection. Sequence. Control Structures ELEC 330 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.

Arithmetic Operators. Binary Arithmetic Operators. Arithmetic Operators. A Closer Look at the / Operator. A Closer Look at the % Operator

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

Problem Solving: Storyboards for User Interaction

1) What of the following sets of values for A, B, C, and D would cause the string "one" to be printed?

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Other operators. Some times a simple comparison is not enough to determine if our criteria has been met.

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

CSc Introduction to Computing

Unit 7. 'while' Loops

Introduction to Programming using C++

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

Lecture 3 Tao Wang 1

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

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

Introduction to Programming

Chapter 2. Flow of Control. Copyright 2016 Pearson, Inc. All rights reserved.

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy

Exceptions, Case Study-Exception handling in C++.

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

C++ Programming Lecture 1 Software Engineering Group

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

Chapter 6 Topics. While Statement Syntax Count-Controlled Loops Event-Controlled Loops Using the End-of-File Condition to Control Input Data

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

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

Chapter Four: Loops. Slides by Evan Gallagher. C++ for Everyone by Cay Horstmann Copyright 2012 by John Wiley & Sons. All rights reserved

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

LECTURE 5 Control Structures Part 2

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

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

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

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

Study recommendations for Quiz 5 - Chapter 5

C++ Quick Reference. switch Statements

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

Objectives. In this chapter, you will:

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

The American University in Cairo Department of Computer Science & Engineeringt CSCI &09 Dr. KHALIL Exam-I Fall 2009

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

ADARSH VIDYA KENDRA NAGERCOIL COMPUTER SCIENCE. Grade: IX C++ PROGRAMMING. Department of Computer Science 1

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

University of Michigan EECS 183: Elem. Programming Concepts Fall 2011 Exam 1: Part 1: Form 1. Professors: ML Dorf, Elliot Soloway

Quiz Determine the output of the following program:

Add Subtract Multiply Divide

Control structures in C. Going beyond sequential

Programming Fundamentals

do { statements } while (condition);

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

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

CSCI 1061U Programming Workshop 2. C++ Basics

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

Ch 6. Functions. Example: function calls function

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

Functions. CS111 Lab Queens College, CUNY Instructor: Kent Chin

PIC 10A Flow control. Ernest Ryu UCLA Mathematics

Chapter 10 Introduction to Classes

Transcription:

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

The Null Statement Null statement Semicolon with nothing preceding it ; Do-nothing statement required for syntax purposes only C++ for Engineers and Scientists, Third Edition 19

for Loops for statement: A loop with a fixed count condition that handles alteration of the condition Syntax: for (initializing list; expression; altering list) statement; Initializing list: Sets the starting value of a counter Expression: Contains the maximum or minimum value the counter can have; determines when the loop is finished C++ for Engineers and Scientists, Third Edition 20

for Loops (continued) Altering list: Provides the increment value that is added or subtracted from the counter in each iteration of the loop If initializing list is missing, the counter initial value must be provided prior to entering the for loop If altering list is missing, the counter must be altered in the loop body Omitting the expression will result in an infinite loop C++ for Engineers and Scientists, Third Edition 21

for Loops (continued) C++ for Engineers and Scientists, Third Edition 22

for (int i=0; i<limit; i++) { // do something } Figure 5.7 for loop flowchart. for Loops (continued) C++ for Engineers and Scientists, Third Edition 23

A Closer Look: Loop Programming Techniques These techniques are suitable for pretest loops (for and while): Interactive input within a loop Includes a cin statement within a while or for loop Selection within a loop Using a for or while loop to cycle through a set of values to select those values that meet some criteria C++ for Engineers and Scientists, Third Edition 24

A Closer Look: Loop Programming Techniques (continued) C++ for Engineers and Scientists, Third Edition 25

A Closer Look: Loop Programming Techniques (continued) Evaluating functions of one variable Used for functions that must be evaluated over a range of values Noninteger increment values can be used C++ for Engineers and Scientists, Third Edition 26

A Closer Look: Loop Programming Techniques (continued) C++ for Engineers and Scientists, Third Edition 27

A Closer Look: Loop Programming Techniques (continued) Interactive loop control Variable is used to control the loop repetitions Provides more flexibility at run-time C++ for Engineers and Scientists, Third Edition 28

A Closer Look: Loop Programming Techniques (continued) C++ for Engineers and Scientists, Third Edition 29

Nested Loops Nested loop: A loop contained within another loop All statements of the inner loop must be completely contained within the outer loop; no overlap allowed Different variables must be used to control each loop For each single iteration of the outer loop, the inner loop runs through all of its iterations C++ for Engineers and Scientists, Third Edition 30

Nested Loops (continued) Figure 5.9 For each i, j loops. C++ for Engineers and Scientists, Third Edition 31

Nested Loops (continued) C++ for Engineers and Scientists, Third Edition 32

Nested loops (independent loops) #include <iostream> #include <cmath> using namespace std; int main() { double x, y; const double MAX_X = 5; const double MAX_Y = 4; } for (x = 1; x <= MAX_X; x++) { cout << "x = " << x << endl; } >./forloop.exe x = 1 y = 1 y = 2 y = 3 y = 4 x = 2 y = 1 y = 2 y = 3 y = 4 x = 3 y = 1 y = 2 for(y=1; y <= MAX_Y; y++){ y = 3 y = 4 cout << "\ty = " << y << endl; x = 4 } y = 1 y = 2 y = 3 y = 4 x = 5 y = 1 y = 2 y = 3 C++ for Engineers and Scientists, Third Edition y = 4 33 return 0; Sample Run

Nested loops (dependent loops) #include <iostream> #include <cmath> using namespace std; int main() { double x, y; const double MAX_X = 5; const double MAX_Y = 4; for (x = 1; x <= MAX_X; x++) { cout << "x = " << x << endl; } for(y=1; y <= x; y++){ cout << "\ty = " << y << endl; } return 0; >./forloop.exe x = 1 y = 1 x = 2 y = 1 y = 2 x = 3 y = 1 y = 2 y = 3 x = 4 y = 1 y = 2 y = 3 y = 4 x = 5 y = 1 y = 2 y = 3 y = 4 y = 5 Sample Run } C++ for Engineers and Scientists, Third Edition 34

Nested loops and using continue #include <iostream> #include <cmath> using namespace std; int main() { double x, y; const double MAX_X = 5; const double MAX_Y = 4; for (x = 1; x <= MAX_X; x++) { cout << "x = " << x << endl; } if (x < 4) { continue; } for(y=1; y <= x; y++){ cout << "\ty = " << y << endl; } return 0; >./forloop.exe x = 1 x = 2 x = 3 x = 4 y = 1 y = 2 y = 3 y = 4 x = 5 y = 1 y = 2 y = 3 y = 4 y = 5 Sample Run } C++ for Engineers and Scientists, Third Edition 35

do while Loops do while loop is a posttest loop Loop continues while the condition is true Condition is tested at the end of the loop Syntax: do statement; while (expression); All statements are executed at least once in a posttest loop C++ for Engineers and Scientists, Third Edition 36

do while Loops (continued) Figure 5.10 The do while loop structure. C++ for Engineers and Scientists, Third Edition 37

do while Loops (continued) Figure 5.11 The do statement s flow of control. C++ for Engineers and Scientists, Third Edition 38

Validity Checks Useful in filtering user-entered input and providing data validation checks Can enhance with if-else statement C++ for Engineers and Scientists, Third Edition 39

Common Programming Errors Making the off by one error: loop executes one too many or one too few times Using the assignment operator (=) instead of the equality comparison operator (==) in the condition expression Testing for equality with floating-point or doubleprecision operands; use an epsilon value instead C++ for Engineers and Scientists, Third Edition 40

Common Programming Errors (continued) Placing a semicolon at the end of the for clause, which produces a null loop body Using commas instead of semicolons to separate items in the for statement Changing the value of the control variable Omitting the final semicolon in a do statement C++ for Engineers and Scientists, Third Edition 41

Summary Loop: A section of repeating code, whose repetitions are controlled by testing a condition Three types of loops: while for do while Pretest loop: Condition is tested at beginning of loop; loop body may not ever execute; ex., while, for loops C++ for Engineers and Scientists, Third Edition 42

Summary (continued) Posttest loop: Condition is tested at end of loop; loop body executes at least once; ex., do while Fixed-count loop: Number of repetitions is set in the loop condition Variable-condition loop: Number of repetitions is controlled by the value of a variable C++ for Engineers and Scientists, Third Edition 43