SlideShare a Scribd company logo
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
false
case z case z action(s) break
true
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<iostream.h>
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 <iostream>
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
18
19 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 ) {
24
25 // determine which grade was input
26 switch ( grade ) { // switch structure nested in while
27
28 case 'A': // grade was uppercase A
29 case 'a': // or lowercase a
30 ++aCount; // increment aCount
31 break; // necessary to exit switch
32
33 case 'B': // grade was uppercase B
34 case 'b': // or lowercase b
35 ++bCount; // increment bCount
36 break; // exit switch
37
38 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 ) {
24
25 // determine which grade was input
26 switch ( grade ) { // switch structure nested in while
27
28 case 'A': // grade was uppercase A
29 case 'a': // or lowercase a
30 ++aCount; // increment aCount
31 break; // necessary to exit switch
32
33 case 'B': // grade was uppercase B
34 case 'b': // or lowercase b
35 ++bCount; // increment bCount
36 break; // exit switch
37
38 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
47
48 case 'F': // grade was uppercase F
49 case 'f': // or lowercase f
50 ++fCount; // increment fCount
51 break; // exit switch
52
53 case 'n': // ignore newlines,
54 case 't': // tabs,
55 case ' ': // and spaces in input
56 break; // exit switch
57
58 default: // catch all other characters
59 cout << "Incorrect letter grade entered."
60 << " Enter a new grade." << endl;
61 break; // optional; will exit switch anyway
62
63 } // end switch
64
65 } // end while
66
Notice the default statement, which
catches all other cases.
67 // output summary of results
68 cout << "nnTotals 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;
75
76 return 0; // indicate successful termination
77
78 } // 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

More Related Content

What's hot

Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c languagetanmaymodi4
 
Mesics lecture 5 input – output in ‘c’
Mesics lecture 5   input – output in ‘c’Mesics lecture 5   input – output in ‘c’
Mesics lecture 5 input – output in ‘c’eShikshak
 
Decision making and branching in c programming
Decision making and branching in c programmingDecision making and branching in c programming
Decision making and branching in c programmingPriyansh Thakar
 
Conditional statements in vb script
Conditional statements in vb scriptConditional statements in vb script
Conditional statements in vb scriptNilanjan Saha
 
Decision making statements in C programming
Decision making statements in C programmingDecision making statements in C programming
Decision making statements in C programmingRabin BK
 
Control structure of c
Control structure of cControl structure of c
Control structure of cKomal Kotak
 
Programming C Language
Programming C LanguageProgramming C Language
Programming C Languagenatarafonseca
 
Loops in c language
Loops in c languageLoops in c language
Loops in c languagetanmaymodi4
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control StructureSokngim Sa
 
Spf Chapter5 Conditional Logics
Spf Chapter5 Conditional LogicsSpf Chapter5 Conditional Logics
Spf Chapter5 Conditional LogicsHock Leng PUAH
 
Claguage 110226222227-phpapp02
Claguage 110226222227-phpapp02Claguage 110226222227-phpapp02
Claguage 110226222227-phpapp02CIMAP
 
Conditional Statement in C Language
Conditional Statement in C LanguageConditional Statement in C Language
Conditional Statement in C LanguageShaina Arora
 

What's hot (20)

Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c language
 
Pl sql programme
Pl sql programmePl sql programme
Pl sql programme
 
Mesics lecture 5 input – output in ‘c’
Mesics lecture 5   input – output in ‘c’Mesics lecture 5   input – output in ‘c’
Mesics lecture 5 input – output in ‘c’
 
Decision making and branching in c programming
Decision making and branching in c programmingDecision making and branching in c programming
Decision making and branching in c programming
 
Conditional statements in vb script
Conditional statements in vb scriptConditional statements in vb script
Conditional statements in vb script
 
Assignment 3
Assignment 3Assignment 3
Assignment 3
 
Decision making statements in C programming
Decision making statements in C programmingDecision making statements in C programming
Decision making statements in C programming
 
Control structures in C
Control structures in CControl structures in C
Control structures in C
 
Control structure of c
Control structure of cControl structure of c
Control structure of c
 
Programming C Language
Programming C LanguageProgramming C Language
Programming C Language
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
 
Control structure
Control structureControl structure
Control structure
 
Control structures in c
Control structures in cControl structures in c
Control structures in c
 
Control structure in c
Control structure in cControl structure in c
Control structure in c
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
 
Spf Chapter5 Conditional Logics
Spf Chapter5 Conditional LogicsSpf Chapter5 Conditional Logics
Spf Chapter5 Conditional Logics
 
Decision making and branching
Decision making and branchingDecision making and branching
Decision making and branching
 
Claguage 110226222227-phpapp02
Claguage 110226222227-phpapp02Claguage 110226222227-phpapp02
Claguage 110226222227-phpapp02
 
Coding verilog
Coding verilogCoding verilog
Coding verilog
 
Conditional Statement in C Language
Conditional Statement in C LanguageConditional Statement in C Language
Conditional Statement in C Language
 

Viewers also liked

Presentatie Nalco - Green Water meets Green IT, 10 maart 2011 - hoe en waar e...
Presentatie Nalco - Green Water meets Green IT, 10 maart 2011 - hoe en waar e...Presentatie Nalco - Green Water meets Green IT, 10 maart 2011 - hoe en waar e...
Presentatie Nalco - Green Water meets Green IT, 10 maart 2011 - hoe en waar e...Jaak Vlasveld
 
The Advantage February 2016
The Advantage February 2016The Advantage February 2016
The Advantage February 2016Daniel Michels
 
Optimize Product Concepts - Larry McManis
Optimize Product Concepts - Larry McManisOptimize Product Concepts - Larry McManis
Optimize Product Concepts - Larry McManisAudrey Perelshtein
 
Gillian Roth - Resume 2
Gillian Roth - Resume 2Gillian Roth - Resume 2
Gillian Roth - Resume 2Gillian Roth
 
From act-to-impact-katherine
From act-to-impact-katherineFrom act-to-impact-katherine
From act-to-impact-katherineEducopia
 
Prudential regualtion for Banks in Bangladesh
Prudential regualtion for Banks in BangladeshPrudential regualtion for Banks in Bangladesh
Prudential regualtion for Banks in BangladeshShah Naoaj Shahed
 
MTBL (Affiliation) ppt.
MTBL (Affiliation) ppt.MTBL (Affiliation) ppt.
MTBL (Affiliation) ppt.Sudip Ganguly
 
Ericsson ConsumerLab: Family Communication
Ericsson ConsumerLab: Family CommunicationEricsson ConsumerLab: Family Communication
Ericsson ConsumerLab: Family CommunicationEricsson
 
Reservoirology#4 or the representation of PDEs with TC Petri nets
Reservoirology#4 or the representation of PDEs with TC Petri netsReservoirology#4 or the representation of PDEs with TC Petri nets
Reservoirology#4 or the representation of PDEs with TC Petri netsRiccardo Rigon
 

Viewers also liked (14)

Intestino
IntestinoIntestino
Intestino
 
Presentatie Nalco - Green Water meets Green IT, 10 maart 2011 - hoe en waar e...
Presentatie Nalco - Green Water meets Green IT, 10 maart 2011 - hoe en waar e...Presentatie Nalco - Green Water meets Green IT, 10 maart 2011 - hoe en waar e...
Presentatie Nalco - Green Water meets Green IT, 10 maart 2011 - hoe en waar e...
 
Odisha Media Award 2016
Odisha Media Award 2016Odisha Media Award 2016
Odisha Media Award 2016
 
The Advantage February 2016
The Advantage February 2016The Advantage February 2016
The Advantage February 2016
 
Optimize Product Concepts - Larry McManis
Optimize Product Concepts - Larry McManisOptimize Product Concepts - Larry McManis
Optimize Product Concepts - Larry McManis
 
Gillian Roth - Resume 2
Gillian Roth - Resume 2Gillian Roth - Resume 2
Gillian Roth - Resume 2
 
From act-to-impact-katherine
From act-to-impact-katherineFrom act-to-impact-katherine
From act-to-impact-katherine
 
Prudential regualtion for Banks in Bangladesh
Prudential regualtion for Banks in BangladeshPrudential regualtion for Banks in Bangladesh
Prudential regualtion for Banks in Bangladesh
 
MTBL (Affiliation) ppt.
MTBL (Affiliation) ppt.MTBL (Affiliation) ppt.
MTBL (Affiliation) ppt.
 
Ericsson ConsumerLab: Family Communication
Ericsson ConsumerLab: Family CommunicationEricsson ConsumerLab: Family Communication
Ericsson ConsumerLab: Family Communication
 
Calendario 2017 para imprimir de T2O media
Calendario 2017 para imprimir de T2O mediaCalendario 2017 para imprimir de T2O media
Calendario 2017 para imprimir de T2O media
 
MTBiz July 2016
MTBiz July 2016MTBiz July 2016
MTBiz July 2016
 
MTBiz Sep-Oct 2015
MTBiz Sep-Oct 2015MTBiz Sep-Oct 2015
MTBiz Sep-Oct 2015
 
Reservoirology#4 or the representation of PDEs with TC Petri nets
Reservoirology#4 or the representation of PDEs with TC Petri netsReservoirology#4 or the representation of PDEs with TC Petri nets
Reservoirology#4 or the representation of PDEs with TC Petri nets
 

Similar to Lecture#4 Algorithm and computing

Control Statement.ppt
Control Statement.pptControl Statement.ppt
Control Statement.pptsanjay
 
Chapter 3 Control structures.ppt
Chapter 3 Control structures.pptChapter 3 Control structures.ppt
Chapter 3 Control structures.pptRahulBorate10
 
Csphtp1 05
Csphtp1 05Csphtp1 05
Csphtp1 05HUST
 
Powerpoint presentation final requirement in fnd prg
Powerpoint presentation final requirement in fnd prgPowerpoint presentation final requirement in fnd prg
Powerpoint presentation final requirement in fnd prgalyssa-castro2326
 
Visula C# Programming Lecture 3
Visula C# Programming Lecture 3Visula C# Programming Lecture 3
Visula C# Programming Lecture 3Abou Bakr Ashraf
 
Jangsehyun final requirement
Jangsehyun final requirementJangsehyun final requirement
Jangsehyun final requirementSehyun Jang
 
Task #1 Correcting Logic Errors in FormulasCopy and compile the so.pdf
Task #1 Correcting Logic Errors in FormulasCopy and compile the so.pdfTask #1 Correcting Logic Errors in FormulasCopy and compile the so.pdf
Task #1 Correcting Logic Errors in FormulasCopy and compile the so.pdfinfo706022
 
My programming final proj. (1)
My programming final proj. (1)My programming final proj. (1)
My programming final proj. (1)aeden_brines
 
Object Oriented Programming using C++: Ch09 Inheritance.pptx
Object Oriented Programming using C++: Ch09 Inheritance.pptxObject Oriented Programming using C++: Ch09 Inheritance.pptx
Object Oriented Programming using C++: Ch09 Inheritance.pptxRashidFaridChishti
 
Basics of Control Statement in C Languages
Basics of Control Statement in C LanguagesBasics of Control Statement in C Languages
Basics of Control Statement in C LanguagesChandrakantDivate1
 
Decision Making Statements, Arrays, Strings
Decision Making Statements, Arrays, StringsDecision Making Statements, Arrays, Strings
Decision Making Statements, Arrays, StringsPrabu U
 
week3_srcDoWhileLoopFactorial.javaweek3_srcDoWhileLoopFactoria.docx
week3_srcDoWhileLoopFactorial.javaweek3_srcDoWhileLoopFactoria.docxweek3_srcDoWhileLoopFactorial.javaweek3_srcDoWhileLoopFactoria.docx
week3_srcDoWhileLoopFactorial.javaweek3_srcDoWhileLoopFactoria.docxalanfhall8953
 
control statements of clangauge (ii unit)
control statements of clangauge (ii unit)control statements of clangauge (ii unit)
control statements of clangauge (ii unit)Prashant Sharma
 

Similar to Lecture#4 Algorithm and computing (20)

Control Statement.ppt
Control Statement.pptControl Statement.ppt
Control Statement.ppt
 
Chapter 3 Control structures.ppt
Chapter 3 Control structures.pptChapter 3 Control structures.ppt
Chapter 3 Control structures.ppt
 
Csphtp1 05
Csphtp1 05Csphtp1 05
Csphtp1 05
 
Powerpoint presentation final requirement in fnd prg
Powerpoint presentation final requirement in fnd prgPowerpoint presentation final requirement in fnd prg
Powerpoint presentation final requirement in fnd prg
 
Visula C# Programming Lecture 3
Visula C# Programming Lecture 3Visula C# Programming Lecture 3
Visula C# Programming Lecture 3
 
BLM101_2.pptx
BLM101_2.pptxBLM101_2.pptx
BLM101_2.pptx
 
Ppt on java basics
Ppt on java basicsPpt on java basics
Ppt on java basics
 
Macaraeg
MacaraegMacaraeg
Macaraeg
 
Jangsehyun final requirement
Jangsehyun final requirementJangsehyun final requirement
Jangsehyun final requirement
 
chapter-4 slide.pdf
chapter-4 slide.pdfchapter-4 slide.pdf
chapter-4 slide.pdf
 
Task #1 Correcting Logic Errors in FormulasCopy and compile the so.pdf
Task #1 Correcting Logic Errors in FormulasCopy and compile the so.pdfTask #1 Correcting Logic Errors in FormulasCopy and compile the so.pdf
Task #1 Correcting Logic Errors in FormulasCopy and compile the so.pdf
 
Castro
CastroCastro
Castro
 
My programming final proj. (1)
My programming final proj. (1)My programming final proj. (1)
My programming final proj. (1)
 
Final requirement
Final requirementFinal requirement
Final requirement
 
Object Oriented Programming using C++: Ch09 Inheritance.pptx
Object Oriented Programming using C++: Ch09 Inheritance.pptxObject Oriented Programming using C++: Ch09 Inheritance.pptx
Object Oriented Programming using C++: Ch09 Inheritance.pptx
 
Basics of Control Statement in C Languages
Basics of Control Statement in C LanguagesBasics of Control Statement in C Languages
Basics of Control Statement in C Languages
 
Decision Making Statements, Arrays, Strings
Decision Making Statements, Arrays, StringsDecision Making Statements, Arrays, Strings
Decision Making Statements, Arrays, Strings
 
Pl sql programme
Pl sql programmePl sql programme
Pl sql programme
 
week3_srcDoWhileLoopFactorial.javaweek3_srcDoWhileLoopFactoria.docx
week3_srcDoWhileLoopFactorial.javaweek3_srcDoWhileLoopFactoria.docxweek3_srcDoWhileLoopFactorial.javaweek3_srcDoWhileLoopFactoria.docx
week3_srcDoWhileLoopFactorial.javaweek3_srcDoWhileLoopFactoria.docx
 
control statements of clangauge (ii unit)
control statements of clangauge (ii unit)control statements of clangauge (ii unit)
control statements of clangauge (ii unit)
 

More from NUST Stuff

Drag force on a sphere in yield stress fluid
Drag force on a sphere in yield stress fluidDrag force on a sphere in yield stress fluid
Drag force on a sphere in yield stress fluidNUST Stuff
 
Nums entry test 2017 paper
Nums entry test 2017 paperNums entry test 2017 paper
Nums entry test 2017 paperNUST Stuff
 
Nums entry-test-new-syllabus-mbbsc-bds
Nums entry-test-new-syllabus-mbbsc-bdsNums entry-test-new-syllabus-mbbsc-bds
Nums entry-test-new-syllabus-mbbsc-bdsNUST Stuff
 
MCAT Full length paper 8-student_copy_
MCAT Full length paper  8-student_copy_MCAT Full length paper  8-student_copy_
MCAT Full length paper 8-student_copy_NUST Stuff
 
MCAT Full length paper 7-student_copy_
MCAT Full length paper 7-student_copy_MCAT Full length paper 7-student_copy_
MCAT Full length paper 7-student_copy_NUST Stuff
 
MCAT Full length paper 6-student_copy_
MCAT Full length paper  6-student_copy_MCAT Full length paper  6-student_copy_
MCAT Full length paper 6-student_copy_NUST Stuff
 
MCAT Full length paper 5-student_copy_
MCAT Full length paper 5-student_copy_MCAT Full length paper 5-student_copy_
MCAT Full length paper 5-student_copy_NUST Stuff
 
Mcat (original paper 2014)
Mcat (original paper 2014)Mcat (original paper 2014)
Mcat (original paper 2014)NUST Stuff
 
MCAT Full length paper 4-student_copy
MCAT Full length paper  4-student_copyMCAT Full length paper  4-student_copy
MCAT Full length paper 4-student_copyNUST Stuff
 
mcat (original paper 2013)
mcat (original paper 2013)mcat (original paper 2013)
mcat (original paper 2013)NUST Stuff
 
mcat (original paper 2012)
mcat (original paper 2012)mcat (original paper 2012)
mcat (original paper 2012)NUST Stuff
 
MCAT Full length paper 3 final
MCAT Full length paper 3 finalMCAT Full length paper 3 final
MCAT Full length paper 3 finalNUST Stuff
 
MCAT (original paper 2011)
MCAT (original paper 2011)MCAT (original paper 2011)
MCAT (original paper 2011)NUST Stuff
 
mcat (original paper 2010)
 mcat (original paper 2010) mcat (original paper 2010)
mcat (original paper 2010)NUST Stuff
 
MCAT Full length paper 1 (student copy)
MCAT Full length paper 1 (student copy)MCAT Full length paper 1 (student copy)
MCAT Full length paper 1 (student copy)NUST Stuff
 

More from NUST Stuff (20)

Me211 1
Me211 1Me211 1
Me211 1
 
Lab LCA 1 7
Lab LCA 1 7Lab LCA 1 7
Lab LCA 1 7
 
Me211 2
Me211 2Me211 2
Me211 2
 
Me211 4
Me211 4Me211 4
Me211 4
 
Me211 3
Me211 3Me211 3
Me211 3
 
Drag force on a sphere in yield stress fluid
Drag force on a sphere in yield stress fluidDrag force on a sphere in yield stress fluid
Drag force on a sphere in yield stress fluid
 
Nums entry test 2017 paper
Nums entry test 2017 paperNums entry test 2017 paper
Nums entry test 2017 paper
 
Nums entry-test-new-syllabus-mbbsc-bds
Nums entry-test-new-syllabus-mbbsc-bdsNums entry-test-new-syllabus-mbbsc-bds
Nums entry-test-new-syllabus-mbbsc-bds
 
MCAT Full length paper 8-student_copy_
MCAT Full length paper  8-student_copy_MCAT Full length paper  8-student_copy_
MCAT Full length paper 8-student_copy_
 
MCAT Full length paper 7-student_copy_
MCAT Full length paper 7-student_copy_MCAT Full length paper 7-student_copy_
MCAT Full length paper 7-student_copy_
 
MCAT Full length paper 6-student_copy_
MCAT Full length paper  6-student_copy_MCAT Full length paper  6-student_copy_
MCAT Full length paper 6-student_copy_
 
MCAT Full length paper 5-student_copy_
MCAT Full length paper 5-student_copy_MCAT Full length paper 5-student_copy_
MCAT Full length paper 5-student_copy_
 
Mcat (original paper 2014)
Mcat (original paper 2014)Mcat (original paper 2014)
Mcat (original paper 2014)
 
MCAT Full length paper 4-student_copy
MCAT Full length paper  4-student_copyMCAT Full length paper  4-student_copy
MCAT Full length paper 4-student_copy
 
mcat (original paper 2013)
mcat (original paper 2013)mcat (original paper 2013)
mcat (original paper 2013)
 
mcat (original paper 2012)
mcat (original paper 2012)mcat (original paper 2012)
mcat (original paper 2012)
 
MCAT Full length paper 3 final
MCAT Full length paper 3 finalMCAT Full length paper 3 final
MCAT Full length paper 3 final
 
MCAT (original paper 2011)
MCAT (original paper 2011)MCAT (original paper 2011)
MCAT (original paper 2011)
 
mcat (original paper 2010)
 mcat (original paper 2010) mcat (original paper 2010)
mcat (original paper 2010)
 
MCAT Full length paper 1 (student copy)
MCAT Full length paper 1 (student copy)MCAT Full length paper 1 (student copy)
MCAT Full length paper 1 (student copy)
 

Recently uploaded

Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfVivekanand Anglo Vedic Academy
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...Nguyen Thanh Tu Collection
 
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdfDanh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdfQucHHunhnh
 
Forest and Wildlife Resources Class 10 Free Study Material PDF
Forest and Wildlife Resources Class 10 Free Study Material PDFForest and Wildlife Resources Class 10 Free Study Material PDF
Forest and Wildlife Resources Class 10 Free Study Material PDFVivekanand Anglo Vedic Academy
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersPedroFerreira53928
 
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptBasic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptSourabh Kumar
 
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...Nguyen Thanh Tu Collection
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxRaedMohamed3
 
Industrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportIndustrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportAvinash Rai
 
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptxSolid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptxDenish Jangid
 
2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptx2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptxmansk2
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPCeline George
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxShajedul Islam Pavel
 
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptxJose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptxricssacare
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chipsGeoBlogs
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsCol Mukteshwar Prasad
 
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptxslides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptxCapitolTechU
 

Recently uploaded (20)

Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdfDanh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
 
Operations Management - Book1.p - Dr. Abdulfatah A. Salem
Operations Management - Book1.p  - Dr. Abdulfatah A. SalemOperations Management - Book1.p  - Dr. Abdulfatah A. Salem
Operations Management - Book1.p - Dr. Abdulfatah A. Salem
 
Forest and Wildlife Resources Class 10 Free Study Material PDF
Forest and Wildlife Resources Class 10 Free Study Material PDFForest and Wildlife Resources Class 10 Free Study Material PDF
Forest and Wildlife Resources Class 10 Free Study Material PDF
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptBasic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
 
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
Industrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportIndustrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training Report
 
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptxSolid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
 
2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptx2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptx
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptxJose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptxslides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
 

Lecture#4 Algorithm and computing

  • 1. EC-111 Algorithms & Computing Lecture #4 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST
  • 2. ‘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; }
  • 3. 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)
  • 4. 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<iostream.h> 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; } }
  • 5. switch Multiple-Selection Structure  Example ‘switch’ – Program to read grades (A-F) – Counts and display number of each grade entered
  • 6. 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 // 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 18 19 cout << "Enter the letter grades." << endl 20 << "Enter the EOF character to end input." << endl; 21 cin>> grade;
  • 7. 22 // loop until user types end-of-file key sequence 23 while (grade != EOF ) { 24 25 // determine which grade was input 26 switch ( grade ) { // switch structure nested in while 27 28 case 'A': // grade was uppercase A 29 case 'a': // or lowercase a 30 ++aCount; // increment aCount 31 break; // necessary to exit switch 32 33 case 'B': // grade was uppercase B 34 case 'b': // or lowercase b 35 ++bCount; // increment bCount 36 break; // exit switch 37 38 case 'C': // grade was uppercase C 39 case 'c': // or lowercase c 40 ++cCount; // increment cCount 41 break; // exit switch 42
  • 8. 22 // loop until user types end-of-file key sequence 23 while ( ( grade = cin.get() ) != EOF ) { 24 25 // determine which grade was input 26 switch ( grade ) { // switch structure nested in while 27 28 case 'A': // grade was uppercase A 29 case 'a': // or lowercase a 30 ++aCount; // increment aCount 31 break; // necessary to exit switch 32 33 case 'B': // grade was uppercase B 34 case 'b': // or lowercase b 35 ++bCount; // increment bCount 36 break; // exit switch 37 38 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().
  • 9. 43 case 'D': // grade was uppercase D 44 case 'd': // or lowercase d 45 ++dCount; // increment dCount 46 break; // exit switch 47 48 case 'F': // grade was uppercase F 49 case 'f': // or lowercase f 50 ++fCount; // increment fCount 51 break; // exit switch 52 53 case 'n': // ignore newlines, 54 case 't': // tabs, 55 case ' ': // and spaces in input 56 break; // exit switch 57 58 default: // catch all other characters 59 cout << "Incorrect letter grade entered." 60 << " Enter a new grade." << endl; 61 break; // optional; will exit switch anyway 62 63 } // end switch 64 65 } // end while 66 Notice the default statement, which catches all other cases.
  • 10. 67 // output summary of results 68 cout << "nnTotals 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; 75 76 return 0; // indicate successful termination 77 78 } // end function main
  • 11. 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