Вы находитесь на странице: 1из 9

# Introduction to Computing

## Faculty of Information Technology UCP Lahore Pakistan

Objectives
Understand concept of Switch- Case Structure. To be able to identify errors in Switch structure. To be able to use break statement where required. To be able to write a C++ program using switch structure. To be able to test the execution sequence of switch structure in various cases.

## The Case Control Structure (Switch):

The control statement that allows us to make a decision from the number of choices is called aswitch, or more correctly a switchcase-default, since these three keywords go together to make up the control statement. They most often appear as follows: switch ( integer expression ) { case constant 1 : do this ; case constant 2 : do this ; case constant 3 : do this ; default : do this ; } The integer expression following the keyword switch is any Cexpression that will yield an integer value. It could be an integer constant like 1, 2 or 3, or an expression that evaluates to an integer. The keyword case is followed by an integer or a characterconstant. Each constant in each case must be different from all the others. The default statements are executed if no case constant matches the switch expression.

## Switch versus If-Else:

a) Switch cant perform like this;case i<= 20: b) All that we can have after the case is that an integer constant or character constant or an expression that evaluates to one of these constants. c) Even float is not allowed.

d) We can check the value of any expression in a switch like; switch (i+j*k) switch (23+45%4*k) switch (a<44 && b > 7) e) Constant expressions can be performed like;3 + 7 is correct but a + b is incorrect f) A switch may occur within another, but in practice it is rarely done. g) The switch is very useful while writing menu driven programs. h) Case can never have variable expression like;a + 3: i) Multiple cases cant have same expressions. j) The switch works faster than if else clause.

Example 1:
# include<iostream.h> int main() { inti = 2; switch(i) { case 1: cout one.\n"; break; case 2: cout "Two.\n"; break; case 3: cout"Three.\n"; break; default: cout"Default.\n"; break; } return 0; } The breakstatement in the program stops the execution of code within a switch and transfers the control to the statement immediately after the switch.

Example 2:
# include<iostream.h> int main() { inti = 2; switch(i) { case 1: cout "one.\n"; case 2: cout "Two.\n"; case 3: cout "Three.\n"; default: cout "Default.\n"; break; } return 0; }

Write a program using SWITCH structure to determine the weight of chocolates being sold. The program takes as input, the number of chocolates being sold, weight of one chocolate in ounces and the choice of weighing i.e. ounces, pounds, grams or kilograms. User enters O as a choice to calculate weight in ounces, P for pounds, G for grams and K for kilograms. Depending on the choice entered by the user the program calculates the weight of chocolates. Display an appropriate message if invalid choice is entered.Use following formulas to calculate total weight of chocolates. For weighing in Ounces total_weight = number_of_chocolates * weight_of_Chocolate For weighing in Pounds total_weight = number_of_chocolates * weight_of_Chocolate / 16 For weighing in Grams total_weight = number_of_chocolates*weight_of_Chocolate * 28.349 For weighing in Kilograms total_weight = number_of_chocolates*weight_of_Chocolate*28.349/1000; Expected Output: Enter the number of chocolates being sold 20 Enter weight of one chocolate in ounces 2 Enter the choice for weighing Enter O to calculate in ounces P for pounds G for grams Kfor kilograms Enter your Choice O Weight in Ounces is 40

Following is the program, which takes the grade of the student and determines the CGPA assigned for that grade. Test the program and identify the error in the program. void main() { char grade; doublegpa = 0.0; cout<<"Enter your grade"<<endl; cin>>grade; switch(grade) { case 'A': gpa = 4.0; case 'B': gpa = 3.0; case 'C': gpa = 2.0; case 'D': gpa = 1.0; case 'F': gpa = 0.0; cout<<"Failed"; break; default: cout<<"Invalid grade entered"; } cout<<"Your gpa is "<<gpa<<endl; }

In the Chinese calendar, every year is associated with a particular animal. The 12-year animal cycle is rat, ox, tiger, rabbit, dragon, snake, horse, goat, monkey, rooster, dog, and boar. The year 1900 is a year of the rat; thus 1901 is a year of the ox and 1912 is another year of the rat. If you know in what year a person was born, you can compute the offset from 1900 and determine the animal associated with that persons year of birth. Create a program using SWITCH structure that determines the animal corresponding to an input year of birth. Assume that the input year is 1900 or later. The program should take as input the year of birth in a variable of type int. The program should then display the corresponding animal. Expected Output: Enter the year 1942 Animal of the year is Horse