Challenge - 5 Problems
If Statement Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ Predict Output
intermediate2:00remaining
Output of nested if statements
What is the output of this Go program?
Go
package main import "fmt" func main() { x := 10 if x > 5 { if x < 15 { fmt.Println("Inside nested if") } } else { fmt.Println("Outside if") } }
Attempts:
2 left
๐ก Hint
Check the conditions in the nested if statements carefully.
โ Incorrect
The variable x is 10, which is greater than 5 and less than 15, so the nested if condition is true and prints "Inside nested if".
โ Predict Output
intermediate2:00remaining
If statement with else if
What will this Go program print?
Go
package main import "fmt" func main() { score := 75 if score >= 90 { fmt.Println("Grade A") } else if score >= 80 { fmt.Println("Grade B") } else if score >= 70 { fmt.Println("Grade C") } else { fmt.Println("Grade F") } }
Attempts:
2 left
๐ก Hint
Check which condition matches the score value first.
โ Incorrect
The score is 75, which is not >= 90 or >= 80 but is >= 70, so it prints "Grade C".
โ Predict Output
advanced2:00remaining
If statement with short statement
What is the output of this Go code?
Go
package main import "fmt" func main() { if x := 5; x > 3 { fmt.Println(x) } else { fmt.Println("No") } }
Attempts:
2 left
๐ก Hint
Look at how the variable x is declared inside the if statement.
โ Incorrect
The variable x is declared and initialized to 5 in the if statement. Since 5 > 3 is true, it prints 5.
โ Predict Output
advanced2:00remaining
If statement with multiple conditions
What will this Go program output?
Go
package main import "fmt" func main() { a, b := 4, 7 if a > 3 && b < 10 { fmt.Println("Condition met") } else { fmt.Println("Condition not met") } }
Attempts:
2 left
๐ก Hint
Both conditions must be true for the if block to run.
โ Incorrect
a is 4 which is > 3 and b is 7 which is < 10, so both conditions are true and it prints "Condition met".
โ Predict Output
expert2:00remaining
If statement with variable shadowing
What is the output of this Go program?
Go
package main import "fmt" func main() { x := 10 if x := 5; x > 3 { fmt.Println(x) } fmt.Println(x) }
Attempts:
2 left
๐ก Hint
Notice the variable x declared inside the if statement shadows the outer x.
โ Incorrect
Inside the if statement, x is 5 and prints 5. Outside, the original x is still 10 and prints 10.