Switch Statement in C++. Syntax switch (selector) { case L1: statements1; break; case L2: statements2; break; … default: statements_n; } Semantics: This.

Slides:



Advertisements
Similar presentations
1 Class Vehicle #include #define N 10../.. 2 Class Vehicle class vehicle { public: float speed; char colour[N+1]; char make[N+1];
Advertisements

Conditional Operator (?:) Conditional operator (?:) takes three arguments (ternary) Syntax for using the conditional operator:
Week 4 Selections This week shows how to use selection statements for more flexible programs. It also describes the various integral types that are available.
1 10/16/06CS150 Introduction to Computer Science 1 switch Selection Structure.
Programming Switch command. COMP102 Prog. Fundamentals: Switch command / Slide 2 Multiple Selection: The switch Statement value1 action 1 value2 action.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
C++ Typecasting. Math Library Functions.. Operator / Operands A = x + y.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
CS 1400 Chapter 5, section 2. Loops! while (rel-expression)while (rel-expression) {statements statement } if the relational-expression is true, execute.
1 CS 105 Lecture 6 While Loops Wed, Feb 16, 2011, 5:13 pm.
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++ (5) 10 September.
If () else statement, switch statement, while () loop, do…while() loop and for( ; ; ) loop 1.
Switch Statements. Switch Statement Often you want to do a series of tests –if i==0 … else if i==1 …. else if i==2 … else if i==3 …. C++ provides the.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 1 st semester King Saud University College of Applied studies and Community Service Csc 1101.
Unit 3 Lesson 8 Selection Structures Mr. Dave Clausen La Cañada High School.
CP104 Introduction to Programming Selection Structures_3 Lecture 11 __ 1 The Switch Statement The switch statement provides another means to select one.
Selection Statements in C++ If Statement in C++ Semantics: These statements have the same meaning as in the algorithmic language. 2- Two way selection:
EC-111 Algorithms & Computing Lecture #4 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Computer Science Department Relational Operators And Decisions.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Introduction to C++ // Program description #include directives int main() { constant declarations variable declarations executable statements return.
CONTROLLING PROGRAM FLOW
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Introduction to C++ Programming COMP102 Prog. Fundamentals I:Introduction to C++ / Slide 2 Introduction to C++ l C is a programming language developed.
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
Multi Way Selection You can choose statement(s) to run from many sets of choices. There are two cases for this: (a)Multi way selection by nested IF structure.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
Previously Repetition Structures While, Do-While, For.
Switch Statement Is a selection control structure for multi-way branching. SYNTAX switch ( IntegralExpression ) { case Constant1 : Statement(s); // optional.
1 Do-While Statement Is a looping control structure in which the loop condition is tested after each iteration of the loop. SYNTAX do { Statement } while.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Chapter 05 (Part III) Control Statements: Part II.
Laughing Singing 5 th Grade 2 nd Quarter Memory Song.
The switch Statement.  Occasionally, an algorithm will contain a series of decisions in which a variable or expression is tested separately for each.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
111/18/2015CS150 Introduction to Computer Science 1 Announcements  I have not graded your exam yet.
1 Original Source : and Problem and Problem Solving.ppt.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
Control Structures (B) Topics to cover here: Sequencing in C++ language.
Problems Reads a letter grade from user and displays the corresponding range for that letter grade (A  , B  80-89, C  70-79, D  60-69, otherwise.
1 CS161 Introduction to Computer Science Topic #8.
1 MODULAR DESIGN AND ABSTRACTION. 2 SPECIFYING THE DETAILS OF A PROBLEM INTO A RELATED SET OF SMALLER PROBLEMS.
Course Title Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 06 FUNCTIONS 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER) 1.
Chapter 4, cont: Control Structures if else nested if switch.
LESSON 4 Decision Control Structure. Decision Control Structures 1. if statement 2. if-else statement 3. If-else-if statement 4. nested if statement 5.
Programming Fundamentals by Dr. Nadia Y. Yousif1 Control Structures (Selections) Topics to cover here: Selection statements in the algorithmic language:
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
C++ Basics Programming. COMP104 Lecture 5 / Slide 2 Introduction to C++ l C is a programming language developed in the 1970s with the UNIX operating system.
Lecture 7 Computer Programming -1-. Conditional Statements 1- if Statement. 2- if ….. else Statement. 3- switch.
FLOW OF CONTROL In C++ the program execution starts with the first statement in the main() and ends with the last statement of main(). This is called.
Switch Statements Comparing Exact Values
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
משפטי תנאי ( לוגיקה ) קרן כליף. 2 © Keren Kalif ביחידה זו נלמד :  משפטי תנאי  משפט switch  משפט if מקוצר.
Infinite for Loop If you omit the test condition, the value is assumed to be TRUE so the loop will continue indefinitely unless you provide some other.
Example 21 #include<iostream.h> int main() { char Letter = 0;
Chapter 3 Selection Statements
LESSON 4 Decision Control Structure
A mechanism for deciding whether an action should be taken
Programming Fundamentals
Compound Assignment Operators in C++
Counting Loops.
Selection Control Structure: Switch Case Statement
Control Structures Part 3
Looping III (do … while statement)
switch Selection Structure
do/while Selection Structure
Fundamental Programming
The switch Statement When we want to compare a variable against several values to see which one it has, we can use the switch statement: switch (status)
Control Structures contd… Selection
Presentation transcript:

Switch Statement in C++

Syntax switch (selector) { case L1: statements1; break; case L2: statements2; break; … default: statements_n; } Semantics: This statement has the same meaning as in the algorithmic language.

Example 4: C++ Program // File: music.cpp /*The program corresponds to Example 5 on slide 31. It reads a letter and displays a suitable musical note (do, re, mi, etc.)*/ #include void main ( ) { char ch ; cin >> ch ; switch ( ch ) { case ‘c’ : cout << “ do “ << endl; break; case ‘d’ : cout << “ re “ << endl; break; case ‘e’ : cout << “ mi “ << endl; break; case ‘f’ : cout << “ f “ << endl; break; case ‘g’ : cout << “ sol “ << endl; break; case ‘a’ : cout << “ la “ << endl; break; case ‘b’ : cout << “ ti “ << endl; break; default : cout << “ Invalid note was read “ << endl; }

Example 6: C++ Program // File: circle.cpp /*This program corresponds to Example 6 on slide 33. It is a menu driven program. It calculates the area of the circle if letter a is read or calculates the circumference if letter c is read.*/ #include void main ( ) { char ch ; float radius, area, circum; cout << “ Enter the radius of a circle: “ ; cin >> radius; cout << “ Enter a to calculate the area of a circle or c to calculate its circumference:” cin >> ch ; switch (ch) { case ‘a’ : area = 3.14f * radius * radius; cout << “ Area = “ << area << endl; break; case‘c’ : circum = 2 * radius * 3.14f ; cout << “ Circumference = “ << circum << endl; break; default : cout << “ Invalid letter was read “ << endl; }

#include void main ( ) { char ch ; cout << “ \n Enter the grade of student: “<<endl ; cin >> ch; switch (ch) { case ‘A’ : case ‘a’ : cout<<”Excellent”; break; case ‘B’ : case ‘b’ : cout<<”Good”; break; case ‘C’ : case ‘c’ : cout<<”O.K”; break; case ‘D’ : case ‘d’ : case ‘F’ : case ‘f’ : cout<<”poor”; break; default: cout<<”invalid letter grade”; }

#include void main() { int x,y; cout << "Enter 2 integer number: "; cin >> x>>y; switch (x+y) { case 7: cout << "Too small, sorry!"; break; case 5: cout << "Good job!\n"; break; case 4: cout << "Nice Pick!\n"; case 3: cout << "Excellent!\n"; break; case 2: cout << "Masterful!\n"; break; case 1: cout << "Incredible!\n"; break; default: cout << "Too large!\n"; } cout << "\n\n"; }