Conditionals
A set of problems that help you to learn to add decisions into a program. You will use if statements to accomplish this.The If Block
In this program, start building a program that decides if a number is even or odd. First, determine where the if block should be located and what blocks should be inside the if scope. Then, run the program. You should expect the program to output 34 is an even number.
Hold off on changing the if block. In the next problem, you will focus on constructing the boolean expression.
If Expressions
Continue the program from the previous problem by coming up with a boolean expression that determines if the value is even. Remember, the mod can determine if a number is even or odd.
The Else Block
Continue from the last problem by adding an else to the if block to handle odd values. When this program is finished it should output, 35 is an odd number.
Nesting IF Blocks
In this problem, you will use two if-else blocks to decide if a number is positive, negative, or zero. Add a nested if-else block and each of the output blocks to the program. Then, test your program by changing the variable value to a negative number, positive number, and zero. You should expect the output to be the following when value is -2.
-2 is a negative number.
Adding an elseif Block
Like the last problem, decide if a number is positive, negative, or zero. Only this time you won't use a nested if-else block, you will use the elseif block with a boolean expression instead.
if value > 0
elseif true
else
end
Leap Year Expressions
In this problem, use an if-else block to decide if 1988 is a leap year. To build the boolean expression for this, the year must be divisible by four and the year must not be divisible by one hundred or is divisible by four hundred. Use this information to add to the basic expression already in the program.
Use mod to determine if the year is divisible by an number. For example, the first expression to determine if year is divisible by four would be year mod 4 = 0
. Aslo, for the second part of the expression, use not=
to determine if the year is not divisible by 100.
Practice Conditionals
Build a program that uses an if block with multiple elseif blocks to output a letter grade for a given score that is between 1 and 100. Then use the following information to determine the letter grade for the score.
90-100: A
80-89: B
70-79: C
60-69: D
Below 60: F
In this problem, a score of 78 should output the letter grade, C. Change the score to test all of the different program outputs.