Section 7.6
Conditionals
Learning Goals
Students will write and evaluate expressions using relational operators (AAP-2.E)
Students will write and determine the results of conditional statements (AAP-2.H)
AAP-2.H.1: Conditional statements, or “if-statements,” affect the sequential flow of control by executing different statements based on the value of a Boolean expression.
CRD-2.J.1: In the development process, testing uses defined inputs to ensure that an algorithm or program is producing the expected outcomes. Programmers use the results from testing to revise their algorithms or programs.
Objectives and Description
The objective of this lesson is to introduce students to boolean expressions, conditionals, and relational operators. All of these concepts work in tandem to allow programs to make decisions based on varied input values. Students will be using conditionals to take user input and control the outcome of a script that is being run, which in this case will result in a song tailor-made to user choices.
Activities
Activity 7.6.1 (Budget 55 minutes)
Students will implement readInput() to ask the user a series of 3-4 questions when their script is run
Questions should be followed with a list of answer choices (recommend limiting to 2)
Encourage students to be creative in the types of questions they ask
Examples of questions and the resulting choices:
What kind of mood are you in? (major vs minor key for happy or sad)
Do you want something relaxed or energetic? (changing tempo)
Do you want to hear a specific type of instrument? (give a list of choices for user to select)
Each readInput() should be assigned to a variable so that the answer is stored
Implement the conditionals that add the appropriate sounds/statements based on the user’s answers
The boolean expressions being evaluated by the conditional should use the relational operator == to check if response from readInput() stored in the variable is equal to one of the possible answer choices
The code statements that execute as a result of each conditional should reflect the answer choice
Example: a few fitMedia() statements that add some minor key, slow sounds if the user answered that they were feeling sad
Utilize at least one nested conditional statement in your script
Test the script multiple times to see if the results match expected output for each of the branches of the nested conditional statement
Have students evaluate their own conditional statements before running the script to determine the expected output