Tip:
Highlight text to annotate it
X
In this lesson, we will discuss nested decision structures.
Here is an example of how to place decision structures within other decision structures.
Let’s say a university is trying to improve their French and chess clubs by admitting a higher percentage of students who have taken French
in high school and know how to play chess.
The university allocates points to each applicant based on their SAT score, number of years of French taken in high school, and whether they
know how to play chess.
Every student starts with an initial base score, which is their SAT score divided by 20.
If a student knows how to play chess, the student will receive 10 additional points for each year of French taken in high school, but will
have 15 points deducted if they did not take French in high school.
If a student does not know how to play chess, the student will receive 5 additional points for each year of French taken in high school, but
will have 20 points deducted for not taking French in high school.
When we run the program, we will be asked to input a SAT score, the number of years of French taken in high school, and whether or not
we play chess.
Let’s enter 1500 for the SAT score, 2 for the number of years of French, and n for whether or not we play chess.
Two message boxes will appear.
The first message box states, “Hello”.
The second message box states, "Your admission score is 85."
Let’s examine how the program calculated this value.
The Integer variable SAT stores the student’s SAT score.
The Double variable admitscore stores the student’s overall admission score calculated by this program.
The Integer variable yrsFrench stores the number of years the student has taken French in high school.
The String variable chess stores whether or not the student knows how to play chess.
The user is then prompted to enter his or her SAT score in an input box, the number of years of French taken in high school in an input box,
and whether or not he or she knows how to play chess in an input box.
admitscore is then assigned the value of SAT divided by 20.
1500 divided by 20 is 75 and that value is stored in admitscore.
Then we come to an If-Then-Else structure.
Inside of this If-Then-Else structure there are nested two If-Then-Else structures.
Each If statement must have an associated End If statement.
Here we have three Ifs and three End Ifs.
The outer If-Then-Else structure begins at the first If statement, it has an Else statement in the middle, and it is terminated by the last End If
statement.
The two inner If-Then-Else structures must begin and end within each section of the outer If-Then-Else structure.
The If-Then-Else structure in the first section begins at the first If statement of the section, it
has an Else in the middle, and it is terminated by the next End If.
The If-Then-Else structure in the second section begins at the first If statement of that section, it has an Else statement in the middle,
and it is terminated by the next End If.
Although indenting is optional, it can help you keep track of which If, Else, and End If statements belong to each structure.
We start at the outer If-Then-Else structure.
Is chess equal to y?
This is False because we input n earlier in the program.
This means we ignore all statements between the If and Else, and we execute the statements between the Else and End If.
Now we come to the inner If-Then-Else structure.
Is yrsFrench greater than 0?
This is True because we input 2 in the input box. So we execute all the statements between If and Else, and ignore the statements between
Else and End If.
The value of 5 * yrsFrench is 10, and this is added to admitscore.
The last statement in the section between the outer Else and End If creates a message box that states “Hello”.
All the statements between the Else and End If have been executed and so the outer If-Then-Else structure is terminated.
A message box then appears that displays the score to the user.
On your own, try inputting different values for SAT, yrsFrench, and chess, and then predicting the outcome.
This was a relatively simple example of how to use nested decision structures.
As your programs become more complicated, you may find it necessary to create many layers of decision structures.