Question

In: Computer Science

The CASE structure is a selection structure, however not all selection structures may be represented as...

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 why each of your suggested problem fits the appropriate category.

Solutions

Expert Solution

solution:

CASE Structure(Switch Case) is one of the selection control approach we can use instead of if...else statement

Both Switch and if..else can bes used for making decisions

It will execute the program according to the values

General Form

switch(expression)

{

case x:

//statements

Break;

case y:

//statements

break;

default:

//statements

}

Switch case statement reducing the complexities of if else statement

Switch case structure should be used where the same code needs to be used for more than one condition therefore several if else statements are needed

For example we want to print the day of the week for a given number entered by the user


import java.util.*;

public class Main
{
public static void main(String[] args)
{
int day;
Scanner sc = new Scanner (System.in);
System.out.println("Enter the number:");
day = sc.nextInt();
switch (day)
{
case 1:
System.out.println("Sunday");
break;
case 2:
System.out.println("Monday");
break;
case 3:
System.out.println("Tuesday");
break;
case 4:
System.out.println("Wednesday");
break;
case 5:
System.out.println("Thursday");
break;
case 6:
System.out.println("Friday");
break;
case 7:
System.out.println("Saturday");
break;
  
default:
System.out.println("Please enter theInvalid input");
break;
}
}
}

Screenshot

Output

If the same program written using if else statement 7 comparisons are needed that will create complexity in the code

Therefore switch case is the perfect choice

The drawback of case structure is

It will operate only on integer values

It cannot be used with float or string values

It can be used only with fixed value

Example

For example consider the program to find the grade of subjects when the user entered

The program will be using the logic

average>=80 then print Grade A

average<80 and average>=60 print Grade B

average<60 and average>=40 print Grade C

Else

Print Grade F

These kind of problems we can’t use switch the reason is average is using float value and in some ranges there are grades determined

Therefore if else will be appropriate for this situation

please give me thumb up


Related Solutions

In C++, the concept of a "bag" may be represented as (click all that apply): a...
In C++, the concept of a "bag" may be represented as (click all that apply): a vector of pointers of a specific object type a vector of objects a vector of generic pointers an array of whole numbers The C++ keywords used to implement the principle of least privilege are (click all that apply): public #include const struct ifndef class If a class Employee has a data member Date hireDate , and Employee's member functions need to access private data...
In C++, the concept of a "bag" may be represented as (click all that apply): a...
In C++, the concept of a "bag" may be represented as (click all that apply): a vector of pointers of a specific object type a vector of objects a vector of generic pointers an array of whole numbers The C++ keywords used to implement the principle of least privilege are (click all that apply): public #include const struct ifndef class If a class Employee has a data member Date hireDate , and Employee's member functions need to access private data...
Strategy Selection Companies with well-established brands may find it commonplace to define their strategies. However, this...
Strategy Selection Companies with well-established brands may find it commonplace to define their strategies. However, this view may change when companies decide to diversify offerings or expand into new markets. Careful evaluation and analysis of new market data, competitor information, target customers, and related factor data may assist companies when making these strategic decisions. How would you develop a strategy for a company that is established in a field and plans to venture into another? What are the key criteria...
Draw the Lewis Structures and name all the isomers of C4H9Cl. Name Lewis Structure Draw the...
Draw the Lewis Structures and name all the isomers of C4H9Cl. Name Lewis Structure Draw the Lewis Structures and name all the isomers of   C2H2Cl2. Name Lewis Structure Cycloalkanes Would you predict that cyclopropane and cyclobutane are highly stable molecules?
Draw the lewis dot structure and all resonance structures for the dithiorcarbonate molecule (S2CO -2). Which...
Draw the lewis dot structure and all resonance structures for the dithiorcarbonate molecule (S2CO -2). Which lewis structure is the best for this structure?(formal charge)
what is body segmentation? in what structures is segmentation represented in the bodies of annelids, arthropods...
what is body segmentation? in what structures is segmentation represented in the bodies of annelids, arthropods and vertebrates?
1) draw the most appropriate Lewis structure, 2) all resonance structures, and 3) formal charges for...
1) draw the most appropriate Lewis structure, 2) all resonance structures, and 3) formal charges for all atoms having any. Then, determine the electron and molecular geometries for each structure. ????? VAL:                       STAB:                                  BOND:                                LONE:          BP:                                        LP: Lewis Structure:                                                    VESPR:                steric number (# e-­‐ pairs): electron pair geometry: # lone pairs: molecular geometry: Lewis Structure Drawn with Molecular Geometry: Be STAB: Rule Exception: Formal Charges
1. N2O2 is an unstable compound. Draw Lewis structure and show all resonance structures. b) Rank...
1. N2O2 is an unstable compound. Draw Lewis structure and show all resonance structures. b) Rank of the following ions in order of increasing nitrogen-oxygen bond energy: NO2-,NO+,and NO3- C)Rank the following bonds from nonpolar to most polar: H--H, H--F, H--Cl, H--Br, H--I.
Provide the correct Lewis structure and ALL legitimate resonance structures for Sulfuric acid. Alo indicate which...
Provide the correct Lewis structure and ALL legitimate resonance structures for Sulfuric acid. Alo indicate which resonance structures contributes most/best to the behavior of this molecule. Justify your answer.
You will be traversing structures as part of the project. However, as practice, we will traverse...
You will be traversing structures as part of the project. However, as practice, we will traverse some structures, for which the source code is already known to us, to remove some of the ambiguity inherent in dealing with undocumented or under-documented Windows structures. Here we know the structures and what their members are. The task for this assignment is to “fill in the blanks” in the in-line Assembly, so that the requested data is populated with the appropriate information. To...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT