Page 3: Combining Conditionals

Unit 2 , Lab 3, Page 3

On this page, you will learn about nested conditional statements.

  1. Talk with Your Partner Here are four possible ways to define . Discuss what advantages each style has:

    1. Do they all work correctly?
    2. Which seem elegant because they are short?
    3. Which seem elegant because they are clear?
    4. Which seem elegant because the steps are simple?
    5. Which is closest to how you think about ≥ in math class?
    6. Which seems clearest to you?

    a ≥ b {if (a > b) {report true} else {if (a = b) {report true} else {report false}}} a ≥ b {if (a > b) {report true} else {report (a = b)}} a ≥ b {report (not (a < b))} a ≥ b {report ((a > b) or (a = b))}

A nested conditional statement is an if or if else statement inside another if else statement.

Look back at the first two examples in the previous problem. Since the predicate expression a = b will report true when they are equal and false otherwise, it’s unnecessary to use the nested conditional statement in the first example, and the second example using the predicate inside the else part is sufficient. Sometimes, however (especially when you aren’t building a predicate), it can be helpful to use nested conditional statements.

  1. Talk with Your Partner Describe what this code segment will do.
    ask (Is today Tuesday?) and wait
if (answer = 'yes')
{
say (Mary Chung's is closed today.)
}
else
{
if (answer = 'no')
{
say (Mary Chung's is open today.)
}
else
{
say (You didn't answer yes or no.)
}
}

  2. Look back at your code for writing on the stage in three colors. If it doesn’t already use a nested conditional statement, copy the code, and create a new version that does.