C++ Programming Lecture 7 Control Structure I (Selection) – Part II

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

1 10/16/06CS150 Introduction to Computer Science 1 switch Selection Structure.
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.
Control Statements (II) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
 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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Control Structures Outline 2.1Introduction 2.2Algorithms 2.3Pseudocode 2.4Control Structures.
C++ Control Structures 程式的控制、運算、判斷跟你之前學過的程式語言是類 似的,所以這裡我們只用程式碼來說明。 你應該而且必須能看得懂這些程式 !!
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.
 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.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Chapter 02 (Part III) Introduction to C++ Programming.
 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.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Chapter 05 (Part III) Control Statements: Part II.
 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
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
 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.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1 Introduction 5.2 Essentials of Counter-Controlled.
CMSC 104, Version 9/011 The switch Statement Topics Multiple Selection switch Statement char Data Type and getchar( ) EOF constant Reading Section 4.7,
C++ Programming Lecture 3 C++ Basics – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
 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.
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Beginning C For Engineers Fall 2005 Lecture 3: While loops, For loops, Nested loops, and Multiple Selection Section 2 – 9/14/05 Section 4 – 9/15/05 Bettina.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
C++ Programming Lecture 12 Functions – Part IV
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
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.
 2003 Prentice Hall, Inc. All rights reserved. ECE 2551 Dr. S. Kozaitis Fall Chapter 5 - Control Statements: Part 2 Outline 5.3 for Repetition.
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 2 - Control Structures
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 4: Making Decisions.
Chapter 4 - Program Control
Chapter 2.1 Control Structures (Selection)
CSC113: Computer Programming (Theory = 03, Lab = 01)
Chapter 4: Making Decisions.
Chapter 4: Making Decisions.
Chapter 2 - Control Structures
مبانی برنامه‌سازی با C++ جلسه دوم
- Additional C Statements
C++ Programming Lecture 16 Arrays – Part III
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
C++ Programming Lecture 3 C++ Basics – Part I
C++ Programming Lecture 17 Pointers – Part I
Chapter 5 – Control Structures: Part 2
Dale Roberts, Lecturer IUPUI
Chapter 4 - Program Control
C++ Programming Lecture 20 Strings
Chapter 2 - Control Structures
Presentation transcript:

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

The Hashemite University Outline Introduction. switch statement. Examples. The Hashemite University

The Hashemite University Introduction In this lecture we will study the last control structure in C++. This control structure is associated with selection. It is used when there are multiple selection choices, i.e. more than 2. It can be used instead of nested if/else structure. The Hashemite University

The switch Multiple-Selection Structure Useful when variable or expression is tested for multiple values. Consists of a series of case labels and an optional default case. Used instead of nested if/else statements to make the code more readable and easier to trace. The Hashemite University

Flowchart Representation of switch true false . case a case a action(s) break case b case b action(s) case z case z action(s) default action(s) The Hashemite University

Notice how the case statement is used 1 // Fig. 2.22: fig02_22.cpp 2 // Counting letter grades 3 #include <iostream> 4 5 using std::cout; 6 using std::cin; 7 using std::endl; 8 9 int main() 10 { 11 int grade, // one grade 12 aCount = 0, // number of A's 13 bCount = 0, // number of B's 14 cCount = 0, // number of C's 15 dCount = 0, // number of D's 16 fCount = 0; // number of F's 17 18 cout << "Enter the letter grades." << endl 19 << "Enter the EOF character to end input." << endl; 20 21 while ( ( grade = cin.get() ) != EOF ) { 22 23 switch ( grade ) { // switch nested in while 24 25 case 'A': // grade was uppercase A 26 case 'a': // or lowercase a 27 ++aCount; 28 break; // necessary to exit switch 29 30 case 'B': // grade was uppercase B 31 case 'b': // or lowercase b 32 ++bCount; 33 break; 34 Notice how the case statement is used

Notice the default statement. 35 case 'C': // grade was uppercase C 36 case 'c': // or lowercase c 37 ++cCount; 38 break; 39 40 case 'D': // grade was uppercase D 41 case 'd': // or lowercase d 42 ++dCount; 43 break; 44 45 case 'F': // grade was uppercase F 46 case 'f': // or lowercase f 47 ++fCount; 48 break; 49 50 case '\n': // ignore newlines, why we need to process it??? 51 case '\t': // tabs, 52 case ' ': // and spaces in input 53 break; 54 55 default: // catch all other characters 56 cout << "Incorrect letter grade entered." 57 << " Enter a new grade." << endl; 58 break; // optional 59 } 60 } 61 62 cout << "\n\nTotals for each letter grade are:" 63 << "\nA: " << aCount 64 << "\nB: " << bCount 65 << "\nC: " << cCount 66 << "\nD: " << dCount 67 << "\nF: " << fCount << endl; 68 69 return 0; 70 } break causes switch to end and the program continues with the first statement after the switch structure. Notice the default statement.

Program Output Enter the letter grades. Enter the EOF character to end input. a B c C A d f E Incorrect letter grade entered. Enter a new grade. D ^Z   Totals for each letter grade are: A: 3 B: 2 C: 3 D: 2 F: 1 Program Output

The Hashemite University Notes I default statement is optional in switch. In the previous example variable grade is called the controlling expression. If no break statement is included, all case statements will be implemented once a match has been found. Logical errors: Forgetting break statement. Forgetting white space between case and the value to test against it( e.g. case3:). This will create a label. default and case statements can be placed in any order inside the switch structure. No braces are required around multiple statements for the same case structure in switch. break determine the end of the case code block. Identical case labels in switch is a syntax error. The Hashemite University

The Hashemite University Notes II switch is used for testing constant integral expressions only, i.e. float, arrays, strings are not allowed. Only integer values or single character values. These values can be the result of an expression, variable, or a constant value. (x + 5) for example is allowed. For case statements only constants integer values are allowed (either integer or single characters). No expressions, float/double values, and variables are allowed. What to do when you want to test double values??? (nested if/else statements). The Hashemite University

Differences between cin>> and cin.get() I the entered value must be consistence with the variable data type. can be used to read any data type from the keyboard. will stop at the first white space when you enter a complete string or when you press enter. cin.get() cin.get() read only one character at a time. Do not stop at white spaces. If the variable is not of type char, cin.get() will store the ASCII representation of the entered character (even if it is a numeral). If you entered more than one character it will store the first entered character only. The Hashemite University

Differences between cin>> and cin.get() II E.g: int x = 10; cin>>x; // the user entered character a cout <<x; //output is 0. x = cin.get(); // the user entered character a cout<<x; //output is 61 (ASCII representation of a in hexadecimal) The Hashemite University

The Hashemite University Additional Notes This lecture covers the following material from the textbook: Fourth Edition: Chapter 2: Section 2.16 The Hashemite University