Section Review of Making Decisions

Section Review of Making Decisions

Lesson Details:
July 10, 2020


I: Introduction

This article will cover topics related to designing programs using programming languages. It will discuss how programming languages are used, what they are used for, why they are used, and what makes one programming language better than another. The article will also discuss how to design a program using different programming languages.

II: Body

A. Section Review of Making Decisions

Making decisions is an important part of program design. It will be the job of the programmer to assign values to variables based on the decision made by the user. There are multiple ways to make decisions. Some of these methods will be discussed here.

I. If/then/else Decision Statements

An if/then/else decision statement is used when there are multiple possibilities. The programmer would first list the possibilities. Then, he or she would create an if/then/else structure that contains three parts. The first part will be “if” which will tell the computer what to do if this particular statement is true. The second part will be “then” which tells the computer what to do if this particular statement is false. Finally, the third part will be “else” which tells the computer what to do if neither of the other statements are true. All three parts must appear in order; if any one of them is left out, then no code will run. This structure is useful when the programmer needs to make multiple decisions based on different conditions. For example, if you want to make a program that asks users whether or not they want to choose their favorite number between one and ten, then you could use an if/then/else structure to do so. An example will be shown below:

1) Create a variable called “number_choice”. This variable will be used to store the number that the user chooses.

2) Create another variable called “number_choice_no”. This variable will be used to store the number that the user does not choose.

3) Make a decision that asks the user what number they want to choose between one and ten. Use a text box for each selection of numbers from one to ten and have it pop up a message box with the number written next to it after a button is clicked. If they choose one, then have a variable called “number_choice” equal 1. If they choose two, have a variable called “number_choice” equal 2. If they choose three, have “number_choice” equal 3, and so on until you reach ten. In addition, have a variable called “number_choice_no” equal 10 as well as 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 for each of those numbers as well as 10 and 11 as well as 12 and 13 as well as 14 and 15 as well as 16 and 17 as well as 18 and 19 as well as 20 and 21 as well as 22 and 23 as well as 24 and 25 as well as 26 and 27 as well as 28 and 29 as well as 30 and 31 for those numbers.

4) Create a variable called “result” which will contain either 1 or 0 depending on whether they picked the number given in the message box or not (one or zero). You can test this by using an equal sign for each one of the choices stored in the message box and comparing it to the value stored in “number_choice_no” (see comparison chart below).

5) After they select a number and click okay on the message box, check to see if they picked their favorite number or not by checking their answer with the number stored in number_choice_no. If their answer equals number_choice_no, then set result equal to zero (0). If their answer does not equal number_choice_no, then set result equal to one (1). This way you can check whether or not their answer was right or wrong. You can then display their favorite number or not favorite number with an if/then/else structure for each choice between one and ten (see chart below).

6) If they pick their favorite number (if they picked 1 and it is stored in number_choice), then set the variable “message” equal to “You chose your favorite number!” and display it in a dialog box with OK button. Set the variable “button_choice” equal to zero (0). Otherwise, set the message equal to “You did not chose your favorite number!” and display it in a dialog box with OK button. Set button_choice equal to one (1). This way you can get them back into the program without having them click anything else after they pick their favorite number or not favorite number by showing them either a positive or negative message depending on whether or not they picked their favorite number or not favorite number based on their input into the program.

7) After you have created all of these variables, you can place an if/then/else structure around all of these variables so that you can compare them all at once. Be sure to leave output clear spaces between each of these decisions so that you can see where each decision lies within your code. An example is shown below:



























































































loader
Course content