Intro to swift one line ternary statement "will never be executed"

I am an intro to swift student and I am getting the compiler message "will never be executed" for my one line ternary statement.

This is the task given by the instructor:

Write a one line ternary statement that sets your AC's isOn property to true when the temperature is greater than or equal to 75, and sets it false in all other cases.

This is the code that I'm using:

indent preformatted text by 4 spaces

      let*temperature = 76

     let isOn = true ? temperature >= 75 : temperature < 75

The order is a little mixed up. These two mean the same thing:

let isOn = true /*a*/ ? temperature >= 75 /*b*/ : temperature < 75 /*c*/
let isOn: Bool

if true /*a*/ {
  isOn = temperature >= 75 /*b*/
} else {
  isOn = temperature < 75 /*c*/
}

Thus it will always select the first of the two code paths, and the compiler is warning you that the second is unreachable.


Write a one line ternary statement that sets your AC's isOn property to true when the temperature is greater than or equal to 75, and sets it false in all other cases.

:thinking: Respectfully, that task is not worthy of a ternary statement. The following is more concise:

let isOn = temperature >= 75

I’m also amused by the idea of having AC set to engage at 75°C, long after everyone is dead. Always document what units are being used. :wink: Spacecraft have blown up because such documentation was neglected.

1 Like

thank you. For some reason I try to make things way more complicated than they need to be.