0% found this document useful (0 votes)
5 views12 pages

Lecture 4 Switch If Else Statement

The document provides an overview of the switch statement in C++, including its syntax, semantics, and examples of usage for different scenarios such as character input and grade evaluation. It also discusses alternatives to the switch statement, such as multiple if and if-else statements, and outlines important points to remember when using switch statements. Additionally, it includes examples of programs that utilize the switch statement for various purposes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views12 pages

Lecture 4 Switch If Else Statement

The document provides an overview of the switch statement in C++, including its syntax, semantics, and examples of usage for different scenarios such as character input and grade evaluation. It also discusses alternatives to the switch statement, such as multiple if and if-else statements, and outlines important points to remember when using switch statements. Additionally, it includes examples of programs that utilize the switch statement for various purposes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Computer Programming

Switch Statement (Selection-


Statements)

Week 04 Sp 25

Acknowledgement – Ms. Nabia Khalid worked on the slides


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
char character; char character;
cout << "Enter a character : "; cout << "Enter a character : ";
cin >> character;
cin >> character; switch (character)
switch (character) {
{ case 'a’:
case 'a’: case ‘A’:
cout << " Australia " << endl; cout << " Australia " << endl;
cout << " Afaghanistan " << endl;
break; break;
case 'b': case 'b’:
cout << " Bangladesh " << endl; case ‘B’:
break; cout << " Bangladesh " << endl;
case 'c': break;
case 'c’:
cout << " Chille " << endl; case ‘C’:
break; cout << " Chille " << endl;
case 'd': break;
cout << " Denmark " << endl; case 'd’:
break; case ‘D’:
cout << " Denmark " << endl;
case 'e': break;
cout << " England " << endl; case 'e’:
break; case ‘E’:
case 'f': cout << " England " << endl;
cout << " France " << endl; break;
case 'f’:
break; case ‘F’:
case 'g': cout << " France " << endl;
cout << " Gana " << endl; break;
break; case 'g’:
default: case ‘G’:
cout << " Gana " << endl;
cout << " I dont know more countries " << endl; break;
} default:
cout << " I dont know more countries " << endl;
}
Example

switch (character)
{
case 'a':
area = 3.14 * radius * radius;
cout << " Area = " << area << endl;
break;

case 'c':
circum = 2 * radius * 3.14;
cout << " Circumference = " << circum << endl;
break;

default: cout << " Invalid letter was read " << endl;
}
cout << "Enter the grade of student : "; cin >> character;
switch (character)
{
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";
}
Example
Alternatives for switch
Multiple if Multiple if-else

If ( grade >=90)
cout<<“A”;
If ( grade >=80)
cout<<“B”;
If ( grade >=70)
cout<<“C”;
If ( grade > =60)
cout<<“D”;
If ( grade<60)
cout<<“F”;
Points to Remember – switch
statement
• The expression followed by each case label must be a constant
expression.
• No two case labels may have the same value (Compile-time Error).
• Two case labels may be associated with the same statements.
• The default label is not mandatory.
• There can be only one default label, and it is usually last.
Example
Write a program that takes a character from user
and prints if the character is vowel or consonant.
Example
char myChar;
cout << "Enter an Alphabet : ";
cin >> myChar;

if (myChar >= '0' && myChar <= '9’)


cout << "ERROR!!\nYou have entered a digit";

else if ((myChar >= 97 && myChar <= 122) || (myChar >= 65 && myChar <= 90))
{
if (myChar == 'a' || myChar == 'e' || myChar == 'i' || myChar == 'o' ||
myChar == 'u' || myChar == 'A' || myChar == 'E' || myChar == 'I' || myChar ==
'O' || myChar == 'U’)
cout << "You have entered a VOWEL";
else
cout << "You have entered a CONSONANT";
}
else
cout << "ERROR!!\nYou did not enter an Alphabet";
Example
int myNumber;
cout << "Enter a Postive Number : ";
cin >> myNumber;

if (myNumber > 0)
{
if (myNumber % 2 == 0)
{
cout << "Number is EVEN number " << endl;
}
else
cout << "Number is ODD number " << endl;
}
else
{
cout << "You have entered a -ve number " << endl;
}

You might also like