Overview

A case or switch statement is a type of selection manage device used to allow the value of a variable or exjiyuushikan.orgion to adjust the regulate flow of routine execution using a multiway branch.

You are watching: The case structure is a variation of the ____ structure

<1>

Discussion

One of the drawbacks of two-method selection is that we deserve to only consider two selections. But what execute you perform if you have actually even more than two choices? Consider the complying with which has actually 4 choices:

if age equal to 18 you can voteelse if age equal to 39 you"re middle-agedelse if age equal to 65 take into consideration retirementelse age is unimportantYou get an correct message depending upon the worth of age. The last item is referred to as the default. If the age is not equal to 18, 39 or 65 you obtain the default message. In some instances tright here is no default action. Consider this flowchart example:

*

This flowchart is of the instance regulate framework and also is supplied for multimeans selection. The decision box holds the variable age. The logic of the situation is one of etop quality wherein the value in the variable age is compared to the provided values in order from left to best. Hence, the worth stored in age is compared to 18 or is “age equal to 18”. If it is true, the logic flows dvery own with the action and drops out at the bottom of the situation framework. If the value of the test exjiyuushikan.orgion is false, it moves to the next listed value to the best and provides one more compariboy. It functions specifically the exact same as our nested if then else structure.

Code to Accomplish Multiway Selection

Python does not assistance a situation control framework. But using the exact same instance as above, here is C++ / C# / Java / JavaScript / Swift code to achieve the situation manage framework.

switch (age) instance 18: message = "You have the right to vote."; break; case 39: message = "You"re middle-aged."; break; situation 65: message = "Consider retirement."; break; default: message = "Era is unnecessary."; break;The worth in the variable age is compared to the initially “case”, which is the value 18 (additionally dubbed the detailed value) utilizing an etop quality comparison or is “age equal to 18”. If it is true, the message is assigned the value “You deserve to vote.” and also the following line of code (the break) is done (which jumps us to the finish of the manage structure). If it is false, it moves on to the following situation for comparikid.

Many programming languages need the listed worths for the case manage framework be of the integer family of data varieties. This basically means either an integer or character data type. Consider this example that offers character data type (option is a character variable):

switch (choice) situation "A": message = "You are an A student."; break; situation "B": message = "You are a B student."; break; instance "C": message = "You are a C student."; break; default: message = "Maybe you have to examine harder."; break;

Limitations of the Case Control Structure

Most programming languages perform not enable arrays of worths for case-like frameworks. Consider this flowcharting example that used ranges:

*

Consider likewise the complying with pseudocode for the exact same logic:

Case of age 0 to 17 Display "You can not vote." 18 to 64 Display "You’re in your functioning years." 65 + Display "You should be retired."EndUsing the case control structure when making use of non-integer family members or arrays of worths is enabled once developing a program and also documenting that design through pseudocode or flowcharting. However before, the implementation in the majority of languperiods would follow a nested if then else method through facility Boolean exjiyuushikan.orgions. The logic of the above examples would look like this:

if age > 0 and age = 18 and also age

Good Structured Programming Methods

Most textbook authors confirm that great structured programming techniques and also actions are even more essential than concentrating on the technological possibilities and capabilities of the language that you are utilizing to learn programming abilities. Remember, this module is concentrating on programming fundamentals and concepts to develop our initial programming skills. It is not a developed via the intent to cover programming languperiods in information, despite the fact that at times we need to cover language mechanics.

See more: What Can I Say This House Is Falling Apart, Walk The Moon

Key Terms

caseA control framework that does multiway selection.switchA regulate framework that deserve to be made to act prefer a instance manage structure.

References