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.
Respectfully, that task is not worthy of a ternary statement. The following is more concise: