Iterations Explanation Basics for app.codility.com/programmers in Go
Category of Article : Technology | Computers and Software | Algorithm
By: Chrysanthus Date Published: 1 May 2025
In programming, iterating means repeating some part of the code to obtain different results. This involves looping.
A loop is a compound statement whose body can be executed again and again. The body of the loop has a number of statements. A loop needs an initial state or initial statement, from which the loop will execute the first time, before repeating. Repeating, means all the statements in the body of the loop are re-executed, in order, again and again. In order for the loop to repeat after the first pass or any pass of the body, there has to be a statement that will cause it to repeat. In order for a loop to stop repeating, there has to be a condition that will cause the loop not to repeat.
This tutorial presents the for-loop in Go, for iterations.
This article is one of the articles written by the author, to enable the reader pass the tests at app.codility.com/programmers/. The tutorials at app.codility.com are given in Python, but this tutorial is given in Go.
for-Loop Syntax
The syntax for the for-loop is:
for (initial-state; while-condition; increment) {
//statements
}
The for-loop has a pair of parentheses and a block. The block is the body of the whole construct. The first statement in the parentheses is the initial statement. The second statement in the parentheses is the while-condition. The third statement in the parentheses is the cause-for-next-iteration (increment).
This construct should be read as follows: Considering the initial statement in the parentheses, do all the statements in the body (block), while the condition allows it. The condition is the second statement in the parentheses. The initial statement ends with a semicolon. The condition statement also ends with a semicolon. The cause-for-next-iteration (increment) statement does not end with a semicolon.
for-Loop Example
Using the above syntax, an example of a for-loop coding, is in the following program:
package main
import (
"fmt";
"strconv"; // Import the strconv package
)
func main() {
for myInt := 0; myInt < 5; myInt++ {
myIntStr := strconv.Itoa(myInt);
fmt.Print(myIntStr + " ");
}
fmt.Println();
}
The output is:
0 1 2 3 4
The parentheses begin with "myInt := 0;". The while condition is "myInt < 5;". The incrementing statement is "myInt++". There are two simple statements in the braces. The first statement in the braces, converts myInt to the string, myIntStr. The second statement prints out the value of the integer, which is now the string, myIntStr. (+" ' ) is for separating the output numbers with a single space. So, while myInt is less than 5, the compound statement is re-executed.
This loop has just one important simple statement, which is to print the value of myInt. The following for-loop has two important simple statements. The first one adds 2 to myInt, and the second one prints the result of the addition:
package main
import (
"fmt";
"strconv"; // Import the strconv package
)
func main() {
for myInt := 0; myInt < 13; myInt++ {
myInt = myInt + 2;
myIntStr := strconv.Itoa(myInt);
fmt.Print(myIntStr + " ");
}
fmt.Println();
}
The output is:
2 5 8 11 14
This output needs explanation. First of all, note that the while condition has been changed to "while (myInt < 13)".
When myInt is 0, 2 is added to it, and myInt becomes 2. Two is printed. The increment adds 1 to myInt and it becomes 3, at the beginning of the next pass. In the next iteration (pass) myInt is 3. Two is added to it again and it becomes 5. The increment adds 1 to myInt and it becomes 6. In the next iteration myInt is 6. Two is added to it again and it becomes 8. The increment adds 1 to myInt and it becomes 9. In the next iteration myInt is 9. 2 is added to it again and it becomes 11. The increment adds 1 to myInt and it becomes 12. In the next iteration myInt is 12. 2 is added to it again and it becomes 14. The increment adds 1 to myint and it becomes 15.
After each iteration, the while condition is checked. At this point, that the while condition is checked, the myInt is 15, above 13, after 14 has been printed. The condition results in false, and the repetition of the block, stops.
Conclusion
The for-loop in Go, repeats the execution of its block, as long as a while-condition is true. The for-loop needs an initial statement (state), which is the first statement in the parentheses. The for-loop needs a cause-for-next-iteration (increment) statement, which is the last statement in the parentheses.