0
0
Goprogramming~5 mins

If–else statement in Go - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of an if–else statement in Go?
An if–else statement lets the program choose between two paths based on a condition. If the condition is true, it runs one block of code; if false, it runs another.
Click to reveal answer
beginner
How do you write a simple if statement in Go?
Use the keyword if followed by a condition, then a block of code in curly braces. Example:<br>
if x > 10 {
    fmt.Println("x is greater than 10")
}
Click to reveal answer
beginner
What is the syntax for an if–else statement in Go?
You write if with a condition and code block, then else with another code block. Example:<br>
if x > 10 {
    fmt.Println("big")
} else {
    fmt.Println("small")
}
Click to reveal answer
intermediate
Can you use multiple conditions with if–else in Go?
Yes, you can chain conditions using else if to check many cases. Example:<br>
if x > 10 {
    fmt.Println("big")
} else if x == 10 {
    fmt.Println("equal")
} else {
    fmt.Println("small")
}
Click to reveal answer
beginner
What happens if the if condition is false and there is no else block?
If the if condition is false and there is no else, the program just skips the if block and continues running the next code.
Click to reveal answer
What keyword starts an if condition in Go?
Awhen
Bif
Ccondition
Dcheck
What does the else block do in an if–else statement?
ARepeats the if block
BRuns code if the if condition is true
CEnds the program
DRuns code if the if condition is false
Which of these is the correct way to write an if statement in Go?
Aif x > 5: fmt.Println("yes")
Bif (x > 5) then { fmt.Println("yes") }
Cif x > 5 { fmt.Println("yes") }
Dif x > 5 do { fmt.Println("yes") }
How do you check multiple conditions in Go?
AUse else if between if and else blocks
BUse multiple if statements without else
CUse switch only
DUse for loops
What happens if the if condition is true and there is an else block?
AOnly the if block runs, else block is skipped
BBoth if and else blocks run
COnly else block runs
DProgram crashes
Explain how an if–else statement controls the flow of a Go program.
Think about choosing between two paths based on a yes/no question.
You got /4 concepts.
    Write a simple Go if–else statement that prints "Even" if a number is even, and "Odd" if it is odd.
    Use x % 2 == 0 to check even numbers.
    You got /4 concepts.