 2003 Prentice Hall, Inc. All rights reserved. ECE 2551 Dr. S. Kozaitis Fall 2005 1 Chapter 5 - Control Statements: Part 2 Outline 5.3 for Repetition.

Slides:



Advertisements
Similar presentations
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Control Structures Outline 2.11 Assignment Operators 2.12 Increment and Decrement Operators.
Advertisements

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
 2008 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
PD GMIT Modified from  2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Control Structures Outline 2.1 Introduction 2.2 Algorithms 2.3 Pseudocode.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Control Structures Outline 2.1 Introduction 2.2 Algorithms 2.3 Pseudocode 2.4 Control Structures.
LOOPING do { } while ( ) for (I = 0; I < 5; I++) { } while ( ) { }
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Control Structures Outline 2.1 Introduction 2.2 Algorithms 2.3 Pseudocode 2.4 Control Structures.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Control Statements (II) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
 2003 Prentice Hall, Inc. All rights reserved. 1 Input / Output Input/output –cin Standard input stream Normally keyboard –cout Standard output stream.
 2003 Prentice Hall, Inc. All rights reserved. 1 Algorithms Computing problems –Solved by executing a series of actions in a specific order Algorithm.
Control Structures in C++ while, do/while, for switch, break, continue.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
1 Review C++_ the unit of programming is the class from which objects are eventually instantiated. C++ classes contain functions that implement class class.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The switch Multiple-Selection Statement switch.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Control Structures Outline 2.1Introduction 2.2Algorithms 2.3Pseudocode 2.4Control Structures.
C How to Program, 6/e Summary © by Pearson Education, Inc. All Rights Reserved.
1 Lecture 4 for loops and switch statements Essentials of Counter-Controlled Repetition Counter-controlled repetition requires  Name of control.
 2003 Prentice Hall, Inc. All rights reserved. 1 Control Structures Outline Assignment Operators Increment and Decrement Operators Essentials of Counter-Controlled.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Control Structures Outline 2.1 Introduction 2.2 Algorithms 2.3 Pseudocode 2.4 Control Structures.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Control Structures Outline 2.1 Introduction 2.2 Algorithms 2.3 Pseudocode 2.4 Control Structures.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
EC-111 Algorithms & Computing Lecture #4 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
C Program Control Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (Switch, do-while, break) Outline 4.7The.
Chapter 4 C Program Control. Objectives In this chapter, you will learn: –To be able to use the for and do … while repetition statements. –To understand.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Control Structures Outline 2.1Introduction 2.2Algorithms 2.3Pseudocode 2.4Control Structures.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Control Structures Outline 2.1 Introduction 2.2 Algorithms 2.3 Pseudocode 2.4 Control Structures.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Control Structures Outline 2.1 Introduction 2.2 Algorithms 2.3 Pseudocode 2.4 Control Structures.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Control Structures Outline 2.1 Introduction 2.2 Algorithms 2.3 Pseudocode 2.4 Control Structures.
Chapter 05 (Part III) Control Statements: Part II.
Spring 2005, Gülcihan Özdemir Dağ Lecture 5, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 5 Outline 5.0 Revisiting.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 2 - Control Structures Outline 2.1Introduction 2.2Algorithms 2.3Pseudocode 2.4Control Structures.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
C++ Programming Lecture 7 Control Structure I (Selection) – Part II The Hashemite University Computer Engineering Department.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Control Structures Outline 2.1 Introduction 2.2 Algorithms 2.3 Pseudocode 2.4 Control Structures.
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1Introduction 5.2 Essentials of Counter-Controlled Repetition.
 2002 Prentice Hall. All rights reserved. 1 Chapter 4 – Control Structures Part 1 Outline Counter-Controlled Repetition: Example Sentinel-Controlled Repetition:
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 6.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Control Structures Outline 2.1 Introduction 2.2 Algorithms 2.3 Pseudocode 2.4 Control Structures.
 2008 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Control Structures Outline 2.1 Introduction 2.2 Algorithms 2.3 Pseudocode 2.4 Control Structures.
Chapter 4 – C Program Control
Chapter 2 - Control Structures
Chapter 4 - Program Control
Chapter 2.1 Control Structures (Selection)
Chapter 2 - Control Structures
مبانی برنامه‌سازی با C++ جلسه دوم
- Additional C Statements
Chapter 4 - Program Control
Program Control Topics While loop For loop Switch statement
Chapter 2 - Control Structures
Capitolo 2 - Control Structures
switch Selection Structure
Chapter 2 - Control Structures
2.6 The if/else Selection Structure
Dale Roberts, Lecturer IUPUI
Chapter 4 - Program Control
Chapter 2 - Control Structures
C++ Programming Lecture 7 Control Structure I (Selection) – Part II
Presentation transcript:

 2003 Prentice Hall, Inc. All rights reserved. ECE 2551 Dr. S. Kozaitis Fall Chapter 5 - Control Statements: Part 2 Outline 5.3 for Repetition Structure 5.4Examples Using the for Statement 5.6 switch Multiple-Selection Structure 5.7 break and continue Statements 5.8 Logical Operators 5.9 Confusing Equality (==) and Assignment (=) Operators

 2003 Prentice Hall, Inc. All rights reserved. ECE 2551 Dr. S. Kozaitis Fall for Repetition Structure General format when using for loops for ( initialization; LoopContinuationTest; increment ) statement Example for( int counter = 1; counter <= 10; counter++ ) cout << counter << endl; –Prints integers from one to ten No semicolon after last statement Declare and initialize variable Loop-continuation condition

 2003 Prentice Hall, Inc. All rights reserved. Outline 3 1 // Fig. 2.17: fig02_17.cpp 2 // Counter-controlled repetition with the for structure. 3 #include 4 5 using std::cout; 6 using std::endl; 7 8 // function main begins program execution 9 int main() 10 { 11 // Initialization, repetition condition and incrementing 12 // are all included in the for structure header for ( int counter = 1; counter <= 10; counter++ ) 15 cout << counter << endl; return 0; // indicate successful termination } // end function main

 2003 Prentice Hall, Inc. All rights reserved. ECE 2551 Dr. S. Kozaitis Fall Examples Using the for Statement a)Vary the control variable from 1 to 100 in increments of 1. for ( int i = 1; i <= 100; i++ ) b)Vary the control variable from 100 down to 1 in increments of -1 (that is, decrements of 1). for ( int i = 100; i >= 1; i-- ) c)Vary the control variable from 7 to 77 in steps of 7. for ( int i = 7; i <= 77; i += 7 ) d)Vary the control variable from 20 down to 2 in steps of -2. for ( int i = 20; i >= 2; i -= 2 ) e)Vary the control variable over the following sequence of values: 2, 5, 8, 11, 14, 17, 20. for ( int i = 2; i <= 20; i += 3 ) f)Vary the control variable over the following sequence of values: 99, 88, 77, 66, 55, 44, 33, 22, 11, 0. for ( int i = 99; i >= 0; i -= 11 )

 2003 Prentice Hall, Inc. All rights reserved. ECE 2551 Dr. S. Kozaitis Fall switch Multiple-Selection Structure true false case acase a action(s)break case bcase b action(s)break false case z case z action(s)break true default action(s)

 2003 Prentice Hall, Inc. All rights reserved. ECE 2551 Dr. S. Kozaitis Fall switch Multiple-Selection Structure switch –Test variable for multiple values –Consists of a series of case labels and optional default case switch ( variable ) { case value1: // taken if variable == value1 statements break; // necessary to exit switch case value2: case value3: // taken if variable == value2 or == value3 statements break; default: // taken if variable matches no other cases statements break; }

 2003 Prentice Hall, Inc. All rights reserved. ECE 2551 Dr. S. Kozaitis Fall switch Multiple-Selection Structure Upcoming examples –Program to read grades (A-F) –Display number of each grade entered Details about characters –Single characters typically stored in a char data type char a 1-byte integer, so char s can be stored as int s

 2003 Prentice Hall, Inc. All rights reserved. Outline 8 1 // Fig. 2.22: fig02_22.cpp 2 // Counting letter grades. 3 #include 4 5 using std::cout; 6 using std::cin; 7 using std::endl; 8 9 // function main begins program execution 10 int main() 11 { 12 int grade; // one grade 13 int aCount = 0; // number of As 14 int bCount = 0; // number of Bs 15 int cCount = 0; // number of Cs 16 int dCount = 0; // number of Ds 17 int fCount = 0; // number of Fs cout << "Enter the letter grades." << endl 20 << "Enter the EOF character to end input." << endl; 21

 2003 Prentice Hall, Inc. All rights reserved. Outline 9 22 // loop until user types end-of-file key sequence 23 while ( ( grade = cin.get() ) != EOF ) {// Grade not equal to EOF // determine which grade was input 26 switch ( grade ) { // switch structure nested in while case 'A': // grade was uppercase A 29 case 'a': // or lowercase a 30 ++aCount; // increment aCount 31 break; // necessary to exit switch case 'B': // grade was uppercase B 34 case 'b': // or lowercase b 35 ++bCount; // increment bCount 36 break; // exit switch case 'C': // grade was uppercase C 39 case 'c': // or lowercase c 40 ++cCount; // increment cCount 41 break; // exit switch 42 cin.get() This function gets 1 character from the keyboard (after Enter pressed), and it is assigned to grade. cin.get() returns EOF (end-of- file) after the EOF character is input, to indicate the end of data. EOF may be ctrl-d or ctrl-z, depending on your OS. Assignment statements have a value, which is the same as the variable on the left of the =. The value of this statement is the same as the value returned by cin.get(). Line 23: while the input from the keyboard is not equal to the value of EOF. EOF is a symbolic integer defined in. EOF is entered when you enter:, or (system-dependent). When EOF is entered, it usually has value -1.

 2003 Prentice Hall, Inc. All rights reserved. Outline 10 fig02_22.cpp (2 of 4) 22 // loop until user types end-of-file key sequence 23 while ( ( grade = cin.get() ) != EOF ) { // determine which grade was input 26 switch ( grade ) { // switch structure nested in while case 'A': // grade was uppercase A 29 case 'a': // or lowercase a 30 ++aCount; // increment aCount 31 break; // necessary to exit switch case 'B': // grade was uppercase B 34 case 'b': // or lowercase b 35 ++bCount; // increment bCount 36 break; // exit switch case 'C': // grade was uppercase C 39 case 'c': // or lowercase c 40 ++cCount; // increment cCount 41 break; // exit switch 42 Compares grade (an int ) to the numerical representations of A and a. Program accepts lowercase or uppercase letter grades.

 2003 Prentice Hall, Inc. All rights reserved. Outline 11 fig02_22.cpp (2 of 4) 22 // loop until user types end-of-file key sequence 23 while ( ( grade = cin.get() ) != EOF ) { // determine which grade was input 26 switch ( grade ) { // switch structure nested in while case 'A': // grade was uppercase A 29 case 'a': // or lowercase a 30 ++aCount; // increment aCount 31 break; // necessary to exit switch case 'B': // grade was uppercase B 34 case 'b': // or lowercase b 35 ++bCount; // increment bCount 36 break; // exit switch case 'C': // grade was uppercase C 39 case 'c': // or lowercase c 40 ++cCount; // increment cCount 41 break; // exit switch 42 break causes switch to end, and the program continues with the first statement after the switch structure.

 2003 Prentice Hall, Inc. All rights reserved. Outline 12 fig02_22.cpp (3 of 4) 43 case 'D': // grade was uppercase D 44 case 'd': // or lowercase d 45 ++dCount; // increment dCount 46 break; // exit switch case 'F': // grade was uppercase F 49 case 'f': // or lowercase f 50 ++fCount; // increment fCount 51 break; // exit switch case '\n': // ignore newlines, 54 case '\t': // tabs, 55 case ' ': // and spaces in input 56 break; // exit switch default: // catch all other characters 59 cout << "Incorrect letter grade entered." 60 << " Enter a new grade." << endl; 61 break; // optional; will exit switch anyway } // end switch } // end while 66 Notice the default statement, which catches all other cases. This test is necessary because Enter is pressed after each letter grade is input. This adds a newline character that must be removed. Likewise, we want to ignore any whitespace.

 2003 Prentice Hall, Inc. All rights reserved. Outline 13 fig02_22.cpp (4 of 4) 67 // output summary of results 68 cout << "\n\nTotals for each letter grade are:" 69 << "\nA: " << aCount // display number of A grades 70 << "\nB: " << bCount // display number of B grades 71 << "\nC: " << cCount // display number of C grades 72 << "\nD: " << dCount // display number of D grades 73 << "\nF: " << fCount // display number of F grades 74 << endl; return 0; // indicate successful termination } // end function main

 2003 Prentice Hall, Inc. All rights reserved. ECE 2551 Dr. S. Kozaitis Fall switch Multiple-Selection Structure Portability Tip 2.3 Testing for EOF rather than -1 makes programs more portable. EOF could have different values on different systems. Portability Tip 2.4 The keystroke combinations for entering EOF are system-dependent. Good Programming Practice 2.19 Provide a default case in switch statements. Cases not tested without a default are ignored. It is best to place the default case last.

 2003 Prentice Hall, Inc. All rights reserved. ECE 2551 Dr. S. Kozaitis Fall break and continue Statements break statement –Immediate exit from while, for, do/while, switch –Program continues with first statement after structure Common uses –Escape early from a loop –Skip the remainder of switch continue statement –Used in while, for, do/while –Skips remainder of loop body –Proceeds with next iteration of loop

 2003 Prentice Hall, Inc. All rights reserved. ECE 2551 Dr. S. Kozaitis Fall Logical Operators Used as conditions in loops, if statements && (logical AND ) –true if both conditions are true if ( gender == 1 && age >= 65 ) ++seniorFemales; –Increment seniorFemales if gender is 1, AND age is greater than 65. || (logical OR ) –true if either of condition is true if ( semesterAverage >= 90 || finalExam >= 90 ) cout << "Student grade is A" << endl; –Print “Student grade is A” if semeterAverage is greater than or equal to 90, OR finalExam is greater then or equal to 90.

 2003 Prentice Hall, Inc. All rights reserved. ECE 2551 Dr. S. Kozaitis Fall Logical Operators ! (logical NOT, logical negation) –Returns true when its condition is false, & vice versa if ( !( grade == sentinelValue ) ) cout << "The next grade is " << grade << endl; –Alternative: if ( grade != sentinelValue ) cout << "The next grade is " << grade << endl; –If grade is not equal to sentinelValue, print “The next grade is (grade)” The && operator has a higher precedence than the | | operator.

 2003 Prentice Hall, Inc. All rights reserved. ECE 2551 Dr. S. Kozaitis Fall Confusing Equality (==) and Assignment (=) Operators Example if ( payCode == 4 ) cout << "You get a bonus!" << endl; –If paycode is equal to 4, bonus given If == was replaced with = if ( payCode = 4 ) cout << "You get a bonus!" << endl; –Paycode assigned value of 4 (no matter what it was before) –Statement is true (since 4 is non-zero) –Condition is always true. Bonus given in every case