0% found this document useful (0 votes)
11 views6 pages

Programming C++ Secations Five

The document discusses the switch statement in C++, which allows decision-making based on multiple choices. It outlines the syntax, examples of usage with integer and character cases, and compares the switch statement with if-else statements, noting that switch cannot handle float expressions or variable expressions in cases. Additionally, it highlights that switch statements generally execute faster than equivalent if-else structures.

Uploaded by

moathjubouri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views6 pages

Programming C++ Secations Five

The document discusses the switch statement in C++, which allows decision-making based on multiple choices. It outlines the syntax, examples of usage with integer and character cases, and compares the switch statement with if-else statements, noting that switch cannot handle float expressions or variable expressions in cases. Additionally, it highlights that switch statements generally execute faster than equivalent if-else structures.

Uploaded by

moathjubouri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

‫‪Programming C++‬‬

‫م‪.‬د‪ .‬حسين أحمد علي الويس‬


‫ﺟﺎمﻌﺔ كركوك‬

‫كليﺔ علوم الحﺎسبﺎت وتكنلوﺟيﺎ المﻌلومﺎت‬

‫قسم تكنلوﺟيﺎ المﻌلومﺎت‬

‫‪63‬‬
Selection statements: Switch Statement.
The switch control statement that allows us to make a decision from the number
of choices
Switch (Expression )
Expression It could be an integer constant like
{ Switch (10.0);
(10);
1, 2 or 3, or an expression that evaluates to an
case constant 1 :
Action statements;
float
Int i ;i ; .
integer
Switch (i);
break ; Constant
Case 50: : is a specific value.
case constant 2 : switch
Case >= ( i +50;
j*k)
Action statements; Case 40 a++10;
b;
break ; switch ( 23 + 45 % 4 * k )
case constant 3 :
Action statements;
break ;
default :
Action statements;
}
64
65
int i; char ch = 'x' ;
Cin>>I;
switch ( i ) switch ( ch )
{ {
case 10 : case 'v' :
Cout << "I am in case 1 \n“ ; Cout<< "I am in case v \n“ ;
break ; break ;
case 20 : case 'a' :
Cout << "I am in case 2 \n“ ; Cout<< "I am in case a \n“ ;
break ; break ;
case 30 : case 'x' :
Cout << "I am in case 3 \n“ ; Cout<< "I am in case x \n” ;
break ; break ;
default : default :
Cout << "I am in default \n“ ; Cout<< "I am in default \n„ ;
} }

Expression Possible Forms

switch ( i + j * k )
switch ( 23 + 45 % 4 * k )

66
switch (grade)
{
if ( marks >= 90 && marks <= 100)
case 90 :
cout << "A\n“ ;
cout <<"You Got A \n"; break;
else if (marks >= 80 && marks <90 )
cout << "B\n”;
case 80 :
else if (marks >= 70 && marks <80 )
cout <<"You Got B \n"; break;
cout << "C\n”;
else if (marks >= 60 && marks <70 )
case 70 :
cout << "D\n”;
cout <<"You Got C \n"; break;
else
cout << "F\n“ ;
case 60 :
cout <<"You Got D \n"; break;

default :
cout <<" Sorry , You Got F \n";
}

67
Switch Versus IF – Else IF
• There are some things that you simply cannot do with a switch. These
are:
– A float expression cannot be tested using a switch
– Cases can never have variable expressions (for example it is wrong to
say case a +3 : )
– Multiple cases cannot use same expressions.

– switch works faster than an equivalent IF - Else ladder.

68

You might also like