The CASE structure is a selection structure, however not all selection structures may be represented as CASE. Give an example of a selection problem that may be solved using the CASE structure and another selection problem that CANNOT be represented by the CASE structure.
Type your response in the space provided (after you have created a thread).
Indicate which problem CAN and CANNOT be represented by CASE
Provide an explanation of why each of your suggested problems fits the appropriate category.
Case selection structures are a substitute for long if statements that compare a variable to several integral values. This statement is used when we have to select one choice from multiple choices on the basis of the value of some variable.
Flow chart for CASE structure:-
Example of problem that can be solved using case:-
Display a message for every value in a dice . In this example variable dice is being compared to 6 different intergers values therefore it can easily be done using CASE selection structure .
switch (dice)
{
case 1: cout << "one"; break;
case 2: cout << "two"; break;
case 3: cout << "three"; break;
case 4: cout << "four"; break;
case 5: cout << "five"; break;
case 6: cout << "six"; break;
default: cout << "ERROR"; break;
}
Example of problem that cannot be solved using case:-
Display the division of student based on his percentage .
marks<33 : divison 3
marks >75 -1st division
50<marks<75 - 2nd division
33<marks<50 3rd divison
marks<33 -FAIL
This example includes comaprision of variable marks in a range of integer value(for example:- 0 to 33, 33 to 50 etc) therefore it cannot be solved using CASE selection structure.
I tried up making a similar program with just two
cases using CASE to show you the error given by the compiler.
Screenshot of that code is given below.
********************** Feel free to leave a comment in case of any doubt*************************
Get Answers For Free
Most questions answered within 1 hours.