EC-111 Algorithms & Computing Lecture #4 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.

Slides:



Advertisements
Similar presentations
EC-111 Algorithms & Computing Lecture #7 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Advertisements

EC-111 Algorithms & Computing Lecture #8 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Control Structures Outline 2.11 Assignment Operators 2.12 Increment and Decrement Operators.
1 10/16/06CS150 Introduction to Computer Science 1 switch Selection Structure.
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.
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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
1 Review C++_ the unit of programming is the class from which objects are eventually instantiated. C++ classes contain functions that implement class class.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Control Structures Outline 2.1Introduction 2.2Algorithms 2.3Pseudocode 2.4Control Structures.
CMSC 104, Version 8/061L15Switch.ppt The switch Statement Topics Multiple Selection switch Statement char Data Type and getchar( ) EOF constant Reading.
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.
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.
 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.
Assignment Operators = +=-= *= /=%= Statement Equivalent Statement a = a + 2 ;a += 2 ; a = a - 3 ;a -= 3 ; a = a * 2 ;a *= 2 ; a = a / 4 ; a /= 4 ; a =
 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.
Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
1 Lecture 3 Control Structures else/if and while.
 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,
 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.
Switch Selection Structure (L14) * General Form of the switch Statement * Details of switch Statement * Flowchart of switch Statement * cin.get * Character.
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.
 2002 Prentice Hall. All rights reserved. 1 Chapter 4 – Control Structures Part 1 Outline Counter-Controlled Repetition: Example Sentinel-Controlled Repetition:
EC-111 Algorithms & Computing Lecture #9 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
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.
 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 Chapter 4 - Control Statements: Part 1 Outline 4.1 Introduction 4.4 Control Structures 4.5 if Selection Structure 4.6 if/else Selection Structure 4.7.
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 4 - Program Control
Chapter 2.1 Control Structures (Selection)
Chapter 2 - Control Structures
مبانی برنامه‌سازی با C++ جلسه دوم
- Additional C Statements
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
Chapter 5 – Control Structures: Part 2
Dale Roberts, Lecturer IUPUI
Chapter 4 - Program Control
Chapter 2 - Control Structures
C++ Programming Lecture 7 Control Structure I (Selection) – Part II
Presentation transcript:

EC-111 Algorithms & Computing Lecture #4 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST

‘switch’ Multiple-Selection Structure  switch –Test variable for multiple values –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; }

switch Multiple-Selection Structure true false case a case a action(s)break case b case b action(s)break false case z case z action(s)break true default action(s)

A program using switch statement that allows a user to choose an item from a menu so that the user’s choice results in a unique message being printed out. #include void main(void) { int choice; cout<<"Choose one of the following"<<endl; cout<<"1 :"<<endl; cout<<"2 : Have a nice day."<<endl; cout<<"3 : Here you go."<<endl; cout<<"4 : Cheers!"<<endl; cin>>choice; switch(choice) { case 1: cout<<"Good Choice."<<endl; break; case 2: cout<<"Have a nice day."<<endl; break; case 3: cout<<"Here you go."<<endl; break; case 4: cout<<"Cheers!"<<endl; break; }

switch Multiple-Selection Structure  Example ‘switch’ –Program to read grades (A-F) –Counts and display number of each grade entered

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 char 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 cin>> grade;

22 // loop until user types end-of-file key sequence 23 while (grade != 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

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 cin.get() uses dot notation (explained chapter 6). This function gets 1 character from the keyboard, 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. Compares grade (an int ) to the numerical representations of A and a. break causes switch to end and the program continues with the first statement after the switch structure. The value of this statement is the same as the value returned by cin.get().

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.

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

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