Switch - Case - Default Statement
Switch - Case - Default Statement
C++ ﻟﻐـــــــــــﺔ
Dr. khaled Alwesabi
1
Quiz 2021 Quiz C++ Programing
C++ Programing
Assignment 7
Fill in the blanks to declare variable a of type int and then assign 7as its value ?
____ a;
a_____7
bool
2
Quiz 2021 Quiz C++ Programing
C++ Programing
Assignment 7
3
Lecture 5
Switch - Case - default statement
switch (Expression)
{ Expression It could be an integer constant
case constant 1: 1, 2, or 3, or an expression that evaluation to
Action statements; integer.
break; Constant : is a specific value.
case constant 2:
Action statements;
break;
case constant 1:
Action statements;
break;
default:
Action statements;
}
4 ﻋﻣﻠﻲ
Lecture 5
Switch - Case - default statement
5 ﻋﻣﻠﻲ
Lecture 5
Switch - Case - default statement
switch (Expression)
{ Expression It could be an integer constant
case constant 1: 1, 2, or 3, or an expression that evaluation to
Action statements; integer.
Switch (10.0);
(10);
break; Constant : is a specific value.
case constant 2:
Action statements; int i;
Case 50
>= :50 :
break; Swich (i);
case constant 1:
Case 40
a ++b;10:
Action statements; Swich (i +j * k);
break;
default: Swich (23 + 45 % 4 * k)
Action statements;
}
6 ﻋﻣﻠﻲ
Lecture 5
Switch - Case - default statement
7 ﻋﻣﻠﻲ
Lecture 5
Switch - Case - default statement
8 ﻋﻣﻠﻲ
Lecture 5
Switch - Case - default statement
9 ﻋﻣﻠﻲ
Lecture 5
Switch - Case - default statement
Result
10 ﻋﻣﻠﻲ
Lecture 5
Switch - Case - default statement
Example :
Write a C++ program that reads an integer number x and
compute, print value of y where : use switch
3𝑥𝑥 − 7 𝑖𝑖𝑖𝑖 𝑥𝑥 = −3
y = �5𝑥𝑥 2 𝑖𝑖𝑖𝑖 𝑥𝑥 = 2 𝑜𝑜𝑜𝑜 5
𝑥𝑥 − 4 𝑥𝑥 3 𝑖𝑖𝑖𝑖 𝑥𝑥 = −4 𝑜𝑜𝑜𝑜 4
11 ﻋﻣﻠﻲ
Lecture 5
Switch - Case - default statement
12 ﻋﻣﻠﻲ
Quiz 2021 Quiz C++ Programing
C++ Programing
Assignment 7
Fill in the blanks to print ’you can drive a car’ if the variable ‘age’ equals 16, print ”
buy a lottery ticket” if the ‘age’ is equal to 18.
int age;
cin
18
13
Quiz 2021 Quiz C++ Programing
C++ Programing
Assignment 7
Which choice shows the correct syntax for the switch statement?
14
Quiz 2021 Quiz C++ Programing
C++ Programing
Assignment 7
Fill in the blanks to test age variable against 16, 18, and 21 values and print
corresponding texts to the screen ?
switch
case
break
15
Quiz 2021 Quiz C++ Programing
C++ Programing
Assignment 7
What would occur if we forget to insert ‘break’ after a ‘case’?
16
C++ Language
C++ ﻟﻐـــــــــــﺔ
Dr. khaled Alwesabi
17