












Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Chanchal Mahanthapa delivered this lecture at Chandra Shekhar Azad University of Agriculture and Technology for Programming and Computer Architecture. It includes: C , Programming, Control, Structures, Nested, Sequence, Selection, Repetition, Flowchart
Typology: Slides
1 / 20
This page cannot be seen from the preview
Don't miss anything!













if student’s grade is greater than or equal to 90 Print “A” else if student’s grade is greater than or equal to 80 Print “B” else if student’s grade is greater than or equal to 70 Print “C” else if student’s grade is greater than or equal to 60 Print “D” else Print “F”
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 == value statementsbreak; // necessary to exit switch case value2:case value3: // taken if variable == value2 or == value statementsbreak;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 false case z case z action(s) break true true default action(s)
// code9.cpp 2 // Counting letter grades. 3 #include
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 1819 cout << "Enter the letter grades." << endl 20 << "Enter the EOF character to end input." << endl; 21
// loop until user types end-of-file key sequence 23 while ( ( grade = cin.get() ) != EOF ) { 2425 // determine which grade was input 26 switch ( grade ) { // switch structure nested in while 2728 case 'A': // grade was uppercase A 29 case 'a': // or lowercase a 30 ++aCount; // increment aCount 31 break; // necessary to exit switch 3233 case 'B': // grade was uppercase B 34 case 'b': // or lowercase b 35 ++bCount; // increment bCount 36 break; // exit switch 3738 case 'C': // grade was uppercase C 39 case 'c': // or lowercase c 40 ++cCount; // increment cCount 41 break; // exit switch 42
// 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; 7576 return 0; // indicate successful termination 7778 } // 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: 3B: 2C: 3D: 2F: 1