Presentation is loading. Please wait.

Presentation is loading. Please wait.

Decision Structures Case Structures.

Similar presentations


Presentation on theme: "Decision Structures Case Structures."— Presentation transcript:

1 Decision Structures Case Structures

2 Decisions As we saw before, our program may need to make decisions based on the users input. But what happens when there is more than 2 or 3 options. In this case the if structure becomes onorous.

3 Case Structure The solution to this problem is called the case structure. It allows us to consider many options at the same time. The down side is this structure only allows us to work with integers ( or things that can be represented with integers).

4 Syntax switch (_variable name_) { case _option1_: instructions; break;
default: }

5 Syntax cout << "Pick a number between 4 and 6 inclusive" << endl; cin >> number; switch (number) { case 4: cout << "Four is a terrible number" << endl; cout << " And neither should you" << endl; break; case 5: cout << "Five is an appropriate choice" << endl; case 6: cout << "Six... seriously?" << endl; default: cout << "That is not between 4 and 6" << endl; }

6 The drawback is that it works only with integers.
There are, however, ways to circumvent this problem using what are called “Structures” which we will discuss at a later time.

7 Exercise Convert your quiz program so that it uses multiple choice questions and the CASE structure to check the validity of the answers. When you are done, explore the “while” command


Download ppt "Decision Structures Case Structures."

Similar presentations


Ads by Google