Tutorial6
Learning Objectives:
- ADVANCE Decision making
- if-else statement VS chain-if-else
- Nested if, AND operator, OR operator
- Exercises
Try some sample code
1. ADVANCE Decision making
Very often it is desirable to make a decision based on multiple outcomes. For example your parents may encourage you to do better at school by making a deal with you. They might offer a reward if you can get a GPA of 3.0 with no more than 3 lates. This means in order for you to get your reward of say $1000 HKD you need to satisfy both the GPA and the late requirement.
Also it is often desirable to make related decisions (often multiple) with different outcomes based on the result.
2. IF-ELSE statement, chain-IF-ELSE
IF-ELSE statement
So far we have just made use of an IF statement by itself. Often times we want to program both parts of the outcome (when the IF is true and when the IF results as false). To simplify the code of having to write two ifs we can make use of the IF-ELSE statement. Please note that an else can only follow an IF and only one single ELSE can be used with any one IF.
E.g. 1
if (grade >= 60)
{
text("You passed!",100,50);
}
else
{
text("Oh no, you failed!",100,50);
}
// same functional code as above with two ifs
if (grade >= 60)
{
text("You passed!",100,50);
}
if (grade < 60)
{
text("Oh no, you failed!",100,50);
}
Functionally both code behaves the same and produce the same result. However computationally there is an added advantage to using IF-ELSE. The IF-ELSE only requires a single evaluation where as the IF-IF will always require two evaluations. See the diaggrams below
Chain-IF-ELSE
A IF-ELSE can be made into a chain where multiple decisions are made. It is still computationally better compared to a chain of IF-IF-IF because during and IF-ELSE chain once a result is valid the entire chain stops!
// E.g. 1
if (color == "red")
{
text("The wavelength of red color waves is between 700-635nm",100,50);
}
else if (color == "orange")
{
text("The wavelength of orange color waves is between 635-590nm",100,50);
}
else if (color == "yellow")
{
text("The wavelength of yellow color waves is between 590-560",100,50);
}
else if (color == "green")
{
text("The wavelength of green color waves is between 560-520",100,50);
}
else if (color == "cyan")
{
text("The wavelength of cyan color waves is between 520-490",100,50);
}
else if (color == "blue")
{
text("The wavelength of blue color waves is between 490-450",100,50);
}
else if (color == "violet")
{
text("The wavelength of violet color waves is between 450-400",100,50);
}
else
{
text("The color you choose does not match",100,50);
}
In the above example of all colors of the rainbow, the computer will check for a matching color until one is found from the 7 choices. If one of the color matches earlier on then the program will not check for more matches later in the chain. Thus a color of "red" will computationally be faster than a color of "blue". If none of the 7 choice match then a final default of "The color you choose does not match" will show. Note: the final else does not need to be there, in that case there is no default for a non-matching color.
3. Nested IF, AND operator, OR operator
A nested IF is used to satisfy multiple decisions before an action is taken. In the example below the first decision needs to be satisfied (gpa greater than or equal to 3.0) followed by a second decision (lates less than 3) before the reward message is shown.
// e.g. 1
if (gpa >= 3.0)
{
if (lates < 3)
{
text("Your reward is $1000",100,50);
}
}
AND Operator
A nested if can also be combined together using the AND operator (&&). The AND operator behaves the same as nested IFs in that any condition chained using an AND must result in true for the resulting overall result to be true.
// e.g. 1 alternative
if (gpa >= 3.0 && lates < 3)
{
text("Your reward is $1000",100,50);
}
Multiple if chains can also be combined using multiple ANDs. All 3 decisions must result in true for the reward message to be shown.
// e.g. 2
if (gpa >= 3.0)
{
if (lates < 3)
{
if (dailyFitbitSteps > 10000)
{
text("Your reward is $2000!",100,50);
}
}
}
Below is the AND alternative which behaves exactly the same.
// e.g. 2
if (gpa >= 3.0 && lates < 3 && dailyFitbitSteps > 10000)
{
text("Your reward is $2000!",100,50);
}
OR Operator
Sometimes it is desirable to have a decision resulting to be true if any one of the decisions happens to be true. Decisions can be combined in this case using an OR operator. In the example below when any one of the decisions gpa >= 3.0 OR lates < 3 are satisfied then the resulting decision also becomes true.
// e.g. 1
// result is true if gpa > 3.0 or lates < 3
// result is also true if both gpa > 3.0 and lates < 3
if (gpa >= 3.0 || lates < 3)
{
text("Your reward is $100!",100,50);
}
Just like the AND operator, multiple OR operators can be combined.
// e.g. 2
// result is true if gpa > 3.0 or lates < 3 or dailyFitbitSteps > 10000
// result is also true if both gpa > 3.0 and lates < 3
// result is also true if both lates < 3 and dailyFitbitSteps > 10000
// result is also true if both gpa > 3.0 and dailyFitbitSteps > 10000
// result is also true if both gpa > 3.0 and lates < 3 and dailyFitbitSteps > 10000
if (gpa >= 3.0 || lates < 3 || dailyFitbitSteps > 10000)
{
text("Your reward is $100!",100,50);
}
4. Exercises
Exercise 1: Write a program with 3 input boxes. One input box is for the studentID, one input box for the student last name, and one input box for the student password. When all 3 matches a specific student id, last name and password then your program should show "Access granted". The program should not show any result otherwise.
Exercise 1 marking guide | |
---|---|
3 input boxes and labels | 1 mark |
Output messages (access granted and denied) | 1 mark |
Correct evaluation when all 3 inputs match | 1 mark |
Exercise 2: Write a program with 14 input boxes. Seven of the input boxes are to take in 7 grades out of 100%. Seven of the input boxes are to take in your behavior grade out of 100%. Your program should calculate the GPA and BPA based on the table below
GPA | GPA and BPA Chart |
---|---|
4.0 | grade average >= 94% |
3.7 | grade average >= 90% |
3.3 | grade average >= 87% |
3.0 | grade average >= 84% |
2.7 | grade average >= 80% |
2.3 | grade average >= 77% |
2.0 | grade average >= 74% |
1.7 | grade average >= 70% |
1.3 | grade average >= 67% |
1.0 | grade average >= 64% |
0.7 | grade average >= 60% |
0.0 | grade average < 60% |
Scoring a GPA and BPA 3.0 or above will allow the students to earn an honor roll. Show the message "Congratulations, you earned an honor roll certificate!" on the screen. A GPA and BPA score equal to or above 3.5 will earn the student a principal honor roll. Your program should show "Congratulations, you earned a principal honor roll certificate!". A GPA or BPA below 3.0 should show the default message "Hope you work harder next trimester!".
Exercise 2 marking guide | |
---|---|
14 input boxes with correct labels | 2 marks |
Correct calculation of GPA and BPA | 2 marks |
Correct messages showing honor roll and principal honor roll | 2 marks |
Exercise 3: Write a program that will take in 2 pieces of information, points and whether the user is a member or not. A member who earns over 500 points will be eligible for a free iphone 7. A non-member earning over 1000 points will be eligible for a free iphone 7.
Should the user not earn enough points for a reward your program should show how many points they have to go to earn a reward. A difference of 100 points should have a more encouraging message. A difference between 100 points and 500 points should have a "see you more often" message. A difference of 500 - 1000 points should welcome the person making them aware of their prizes.
Exercise 3 marking guide | |
---|---|
appropriate use of input methods | 2 marks |
correct control structures and output for members and non-members satisfying point requirements | 2 marks |
Exercise 4: Write a program that report the winner and loser given the input of two players for a game of ROCK-PAPER-SCISSORS-LIZARD-SPOCK. The two players must select one of 5 choices (rock, paper, scissors, lizards, spock). If both player selects the same result then it is a tie. The rules of the game are:
Rock Paper Scissors Lizard Spock |
---|
Scissors cut Paper |
Paper covers Rock |
Rock crushes Lizard |
Lizard poisons Spock |
Spock smashes Scissors |
Scissors decapitates Lizard |
Lizard eats Paper |
Paper disproves Spock |
Spock vaporizes Rock |
Rock crushes Scissors |
Exercise 4 marking guide | |
---|---|
2 input choices | 1 mark |
Correct use of input choices | 1 mark |
correct output of all results based on user inputs | 3 marks |
Exercise 5: Write a program with a slider which controls the X and Y movements of a farm animal (one of your previous ones will do). Create a large size canvas (800x600) or larger. On the border of the canvas create a fence (line). The farm animal should start in the center of the screen. Any movement of the farm animal beyond the fence should display a shepherd dog on the canvas.
Exercise 5 marking guide | |
---|---|
2 sliders created | 1 mark |
sliders control the X and Y movement of the farm animal | 1 mark |
farm animal (all of it) moves together | 1 mark |
correct canvas size | 1 mark |
fence created | 1 mark |
farm animal starts at center of screen | 1 mark |
moving farm animal outside of fence will show a shepherd dog | 1 mark |
Exercise 6: As an addition to Exercise 5, create a small patch of green grass for your farm animal within the fence area. When your farm animal moves over the patch of grass change the color of the grass to yellow. When the farm animal moves off of the grass it should change back to green as it was.
HINTS: Imagine the grass as a rectangular or square object and treat the farm animal as a single reference point of (x,y) coordinates. Apply mathematical inequalities to determine if the point (farm animal) is in the area (grass).
Exercise 6 marking guide | |
---|---|
green "grass" created | 1 mark |
grass changes to yellow | 1 mark |
Additional Coding Requirements
Ongoing Assessment | Below Standard (1) | Slightly Below Standard (3) | Meets Standard (5) |
---|---|---|---|
Proper Use of Functions | Code fails to demonstrate proper use of functions or parameters. | Code demonstrates proper use of function but not the use of parameters. Objects are drawn in a fixed location. | Code demonstrates proper use of functions and parameters such that the object can be easily duplicated. |
Proper Use of Comments | Comments do not exist | Comments are either too long or vague and does not clearly encapsulate what is happening in the code ahead. | Comments are short concise and describe the code ahead. A comment appears at the beginning of each block of code and in additional places where necessary. |