An if statement lets the program decide to run some code only if a condition is true. It's like choosing a path based on a yes/no question.
Click to reveal answer
beginner
How do you write a simple if statement in Go?
You write if condition { /* code to run if true */ }. The condition follows directly after if without parentheses, and the code to run is inside curly braces.
Click to reveal answer
beginner
Can you use an if statement without an else in Go?
Yes! You can run code only when the condition is true and do nothing if it is false. The else part is optional.
Click to reveal answer
beginner
What happens if the if condition is false and there is no else block?
The program skips the code inside the if block and continues running the rest of the program.
Click to reveal answer
beginner
How do you write an if-else statement in Go?
You write if condition { /* code if true */ } else { /* code if false */ }. This runs one block or the other depending on the condition.
Click to reveal answer
What does an if statement do in Go?
ARuns code only if a condition is true
BRuns code repeatedly
CDefines a function
DDeclares a variable
✗ Incorrect
An if statement checks a condition and runs code only when it is true.
Which symbol is used to start the code block after an if condition in Go?
A[ ]
B( )
C{ }
D< >
✗ Incorrect
Curly braces { } are used to group the code that runs if the condition is true.
Is the else part mandatory after an if in Go?
ANo, it's optional
BOnly inside loops
COnly if the condition is false
DYes, always
✗ Incorrect
The else block is optional; you can have just an if statement.
What happens if the if condition is false and there is no else?
AProgram crashes
BCode inside <code>if</code> runs anyway
CProgram asks for input
DCode inside <code>if</code> is skipped
✗ Incorrect
If the condition is false and no else exists, the code inside if is skipped.
How do you write an if-else statement in Go?
Aif (condition) then { } else { }
Bif condition { } else { }
Cif condition: { } else: { }
Dif condition -> { } else -> { }
✗ Incorrect
The correct syntax is if condition { } else { } without extra words or symbols.
Explain how an if statement controls the flow of a Go program.
Think about how you decide to do something only if a condition is met.
You got /4 concepts.
Write the syntax of a simple if statement in Go and describe each part.
Remember the shape of the code with braces and the condition.
You got /4 concepts.
Practice
(1/5)
1. What does an if statement do in Go?
easy
A. It runs code only if a condition is true.
B. It repeats code multiple times.
C. It stores data in a variable.
D. It defines a new function.
Solution
Step 1: Understand the purpose of an if statement
An if statement checks a condition and runs code only when that condition is true.
Step 2: Compare options with the if statement behavior
Only "It runs code only if a condition is true." describes running code based on a condition being true.
Final Answer:
It runs code only if a condition is true. -> Option A
Quick Check:
If statement = runs code if true [OK]
Hint: If checks true/false to decide running code [OK]
Common Mistakes:
Confusing if with loops
Thinking if stores data
Mixing if with function definitions
2. Which of these is the correct syntax for an if statement in Go?
easy
A. if (x > 10) then { fmt.Println("Big") }
B. if x > 10 { fmt.Println("Big") }
C. if x > 10: fmt.Println("Big")
D. if x > 10 { fmt.Println("Big") else }
Solution
Step 1: Recall Go if statement syntax
Go uses if condition { code } without parentheses or 'then'.
Step 2: Check each option
if x > 10 { fmt.Println("Big") } matches Go syntax exactly. Options A, C, and D have syntax errors.
Final Answer:
if x > 10 { fmt.Println("Big") } -> Option B
Quick Check:
Go if syntax = if condition { } [OK]
Hint: Go if uses braces, no 'then' or colons [OK]
Common Mistakes:
Adding 'then' keyword
Using parentheses around condition
Using colon instead of braces
3. What is the output of this Go code?
package main
import "fmt"
func main() {
x := 5
if x > 3 {
fmt.Println("Yes")
} else {
fmt.Println("No")
}
}
medium
A. Syntax error
B. No
C. 5
D. Yes
Solution
Step 1: Check the condition in the if statement
The variable x is 5, and the condition is x > 3, which is true.
Step 2: Determine which block runs
Since the condition is true, the code inside the if block runs, printing "Yes".
Final Answer:
Yes -> Option D
Quick Check:
x=5 > 3 is true, prints "Yes" [OK]
Hint: If condition true runs if-block, else runs else-block [OK]
Common Mistakes:
Confusing which block runs
Thinking it prints the variable
Assuming syntax error due to else
4. Find the error in this Go code:
package main
import "fmt"
func main() {
x := 10
if x > 5
fmt.Println("Big")
}
medium
A. Variable x not declared
B. Missing semicolon after x := 10
C. Missing braces { } after if condition
D. Wrong import statement
Solution
Step 1: Check if statement syntax
In Go, the if condition must be followed by braces { } to group the code.
Step 2: Identify the missing part
The code after if lacks braces, causing a syntax error.
Final Answer:
Missing braces { } after if condition -> Option C
Quick Check:
If needs braces { } after condition [OK]
Hint: Always use braces { } after if condition [OK]
Common Mistakes:
Omitting braces after if
Adding unnecessary semicolons
Misunderstanding import errors
5. You want to print "Positive", "Zero", or "Negative" based on an integer variable n. Which code correctly uses if statements to do this?
hard
A. if n > 0 { fmt.Println("Positive") } else if n == 0 { fmt.Println("Zero") } else { fmt.Println("Negative") }
B. if n > 0 { fmt.Println("Positive") } if n == 0 { fmt.Println("Zero") } else { fmt.Println("Negative") }
C. if n > 0 { fmt.Println("Positive") } else if n <= 0 { fmt.Println("Negative") } else { fmt.Println("Zero") }
D. if n >= 0 { fmt.Println("Positive") } else if n == 0 { fmt.Println("Zero") } else { fmt.Println("Negative") }
Solution
Step 1: Understand the conditions for each case
Positive means n > 0, zero means n == 0, negative means n < 0.
Step 2: Check each option's logic
if n > 0 { fmt.Println("Positive") } else if n == 0 { fmt.Println("Zero") } else { fmt.Println("Negative") } correctly checks n > 0, then n == 0, else negative. if n > 0 { fmt.Println("Positive") } if n == 0 { fmt.Println("Zero") } else { fmt.Println("Negative") } has two separate ifs causing multiple prints. if n > 0 { fmt.Println("Positive") } else if n <= 0 { fmt.Println("Negative") } else { fmt.Println("Zero") } swaps zero and negative logic. if n >= 0 { fmt.Println("Positive") } else if n == 0 { fmt.Println("Zero") } else { fmt.Println("Negative") } wrongly treats n >= 0 as positive.
Final Answer:
if n > 0 { fmt.Println("Positive") } else if n == 0 { fmt.Println("Zero") } else { fmt.Println("Negative") } -> Option A
Quick Check:
Correct if-else chain matches conditions [OK]
Hint: Use else if to check exclusive conditions in order [OK]
Common Mistakes:
Using multiple ifs causing multiple outputs
Mixing condition order
Wrong comparison operators
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.