Problem overview
The task is to print every whole number from 3 through 10, one per line, using a for loop. The first line of output is 3, the last is 10, and both ends are included. That makes eight lines in total.
Approach: choose the start, the end and the step
A three-part loop is controlled by an initialization, a condition and a post statement, and all three are yours to tune. For this task:
- the loop variable starts at the first number to print, 3;
- the condition must stay true up to and including 10;
- each pass adds 1.
The inclusive end is where beginners slip. With i < 10 the loop stops once i reaches 10, so 10 is never printed. Either i <= 10 or i < 11 works, because for whole numbers they mean the same thing. The first one reads more directly from the task ("through 10"), so that is the one used here.
Tracing the loop
It helps to follow the variable through the first and last passes:
i := 3runs once. Is3 <= 10? Yes, so 3 is printed, and theni++makesiequal to 4.- The same check and print repeat for 4, 5, 6, 7, 8 and 9.
- When
iis 10, is10 <= 10true? Yes, so 10 is printed, andibecomes 11. - Is
11 <= 10? No, so the loop ends. The body ran eight times: 10 - 3 + 1.
That count is a useful check. For a loop that goes from a through b in steps of 1, the body runs b - a + 1 times when a <= b. If your output has a different number of lines, one of the bounds is off.
Step by step
- Write the loop header. Declare
iwith:=and the value 3, usei <= 10as the condition andi++as the post statement. Go takes no parentheses around these parts, but the body needs braces. - Print inside the body.
fmt.Println(i)writes the current value and ends the line, so each number appears on its own line.
The solution in Go
for i := 3; i <= 10; i++ {
fmt.Println(i)
}
A few Go details:
:=declares and assigns.i := 3createsiwith typeint, inferred from the literal. It only exists inside the loop.fmt.Printlnprints any value. Given anintit prints the digits, and it always adds a newline.fmt.Printwould run the numbers together on one line.i++is a statement, not an expression. You can write it as the post statement, but not inside another expression such asfmt.Println(i++).
Edge cases and common mistakes
- Stopping one short. With
i < 10the output ends at 9. - Starting one off. With
i := 0, the output begins with 0, 1 and 2, which the task does not want. - Forgetting the post statement. Without
i++,istays at 3, the condition is always true, and the program prints 3 forever. - Printing text instead of the variable.
fmt.Println("i")prints the letter i eight times. Without the quotes, Go uses the variable's value.
Writing the same loop differently
Other headers print the same numbers. for i := 3; i < 11; i++ has the end written as "less than 11". You could also count from 0 and print i + 3, but that hides the real range inside the body, so the plain version is easier to read and to change later.
Testing
The expected output is the eight lines 3, 4, 5, 6, 7, 8, 9 and 10. If you change the bounds in the editor above to i := 2 and i < 7, you get 2 through 6.