Question: Topics Switch statement Description Write a program that determines a users personality type from knowing the users favorite color. The program displays a menu of

Topics

Switch statement

Description

Write a program that determines a users personality type from knowing the users favorite color. The program displays a menu of a list of colors with a number associated with each color and asks the user to select his/her favorite color from the list by entering the number next to it as below:

1 My favorite color is Red

2 My favorite color is Green

3 My favorite color is Blue

4 My favorite color is Violet

Select your favorite color from the list by entering the number next to it:

After the user makes its selection above, the program determines the users personality type from users favorite color as below:

Favorite Color Personality Type

Red Feeling Type

Green Thinking Type

Blue Intuitive Type

Violet Sensing Type

At the end, the program displays the users favorite color and the personality type. See test section below.

Requirements

Do this assignment using a Switch statement

Test Runs

For doing the assignment, perform the following test runs using the data below. User input is shown in bold.

Test Run 1

1 My favorite color is Red

2 My favorite color is Green

3 My favorite color is Blue

4 My favorite color is Violet

Select your favorite color by entering the number next to it:

3

Your Favorite Color: Blue

Your Personality Type: Intuitive Type

Test Run 2

Select your favorite color by entering the number next to it:

2

Your Favorite Color: Green

Your Personality Type: Thinking Type

Sample Code

//Code snippet below displays the menu and asks for user selection.

int option;

string menu = "";

menu = menu + "1 My favorite color is Red ";

menu = menu + "2 My favorite color is Green ";

menu = menu + "3 My favorite color is Blue ";

menu = menu + "4 My favorite color is Violet ";

menu = menu + " ";

menu = menu + "Select your favorite color by entering the number next to it: ";

cout << menu;

cin >> option;

switch (option) {

case 1:

cout << "Your Favorite Color: Red" << endl;

cout << " Your Personality Type: Feeling Type" << endl;

break;

case 2:

cout << "Your Favorite Color: Green" << endl;

cout << " Your Personality Type: Thinking Type" << endl;

break;

//add more cases here

default:

cout << "Invalid Input" << endl;

break;

}

Discussion Switch Statement

Just like if/else if statement, a Switch statement is used to create multiple paths in the program. It does so by jumping to different jump points within it when executed.

A Switch statement starts with the word switch. After that there are a pair of parentheses containing an expression called the Switch expression. After that there are a pair of braces which may contain some code. This code has one or more jump points. Each jump point is marked with a jump label ending with colon such as case 1:, case 2: default: etc. as shown in above switch statement.

When a Switch statement is executed, it evaluates the switch expression in the pair of parentheses next to it and jumps to the case label inside its braces that has the same value as the expression. If there is no matching case label, the switch statement ends and the control goes to the instruction past the switch statement. To avoid this from happening, an option label 'default' may be used where the control goes when there is no matching case label as shown in the above Switch statement.

In the above Switch statement, the variable option is the Switch expression. So, its value decides where to jump to. When its value is 1, Switch statement jumps to case 1 and when its value is 2, it jumps to case 2 etc. When its value does not match any of the case labels, it jumps to the jump label 'default'. The optional jump label 'default' is provided to handle the case when there will be no matching case label.

After the jump, the program continues executing instructions in a sequence within the switch statement ignoring any case labels along the way. A separate instruction 'break' is used to terminate execution inside the switch statement and go to the instruction past the switch statement. In the above Switch statements, there is a break statement at the end of the code under each case label. So, after executing the code under a case label, the code goes to the instructions past the switch statement. In this above Switch statement and in most cases, the Switch statement is written to execute the code only under a single case.

In the above switch statement, if the break instruction for the code under case 1 was missing, the code, after jumping to case 1, will keep executing till it encounters the break at the end of case 2 and only then it will break out of the switch statement. If it encounters no break statement in its path of execution, it will keep executing till it reaches the end of the Switch statement and then it will continue to the instruction past the Switch statement.

Unlike If/Else If statement that checks conditions in a sequence one after the other to decide upon a path to execute, the Switch statement does not compare case labels in a sequence to determine which one to jump to. Instead, it uses a much faster scheme called Hashing for that purpose. However, this limits the type of expressions for which Switch statement can be used. For example, Switch statement cannot be used with decimal Switch expressions. It is frequently used when Switch expression evaluates to an int or a char. Because of its faster speed, the programmer use Switch statement instead of If/Else If statement whenever easily workable.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!