Looping in go lang
Loop is a structure with the block of statements can be repeated for the finite number of times.
Till Now we have learned the programs that execute sequentially, Now we are going to learn control structures which change the normal flow of the program and helps you to create more meaningful and compact program structure.
To understand more, suppose we want to print from 1 to 10 number counting, each number should be printed in the new line.
Program to print numbers from 1 to 10
package main
import "fmt"
func main( ) {
fmt.Println(1)
fmt.Println(2)
fmt.Println(3)
fmt.Println(4)
fmt.Println(5)
fmt.Println(6)
fmt.Println(7)
fmt.Println(8)
fmt.Println(9)
fmt.Println(10)
}
Another compact way to create this program use loop. The following example creates this program using the loop.
Program
package main
import "fmt"
func main() {
i := 1 //initialization
for i <= 10 { //condition
fmt.Println(i) //statement
i = i + 1 //updates
}
}
Output
1With the above program snippet, you received the same output and numbers get printed from 1 to 10 separated in each line
2
3
4
5
6
7
8
9
10
Loop Explained
- Loop is a structure with the block of statements can be repeated for the finite number of times.
- For example, if you want to print "Hello" for 10 times then one way to print it manually or another way is to use loop construct.
- Now as in general life if you want to perform anything 10 times then you start counting from 1 and perform tasks until 10 occurs. In this way, you are tracking records by counting that how much time you have performed task.
- Similarly, In the above program, we have tracked how many times loop executed by using the variable "i", which is also called a counter.
Parts of Loop
- Initialization: It is a section, where we create a variable and start with initial value, As in the above example i:= 1 is an initialization statement. Which meaning start counting from 1.
- Condition: Most important part of the loop, which controls the loop execution, the loop executes until the condition remains true and false as soon as the condition becomes false. In the above example condition is "i<=10", which means until i is less than 10 this loop executes.
- Statement: Statement(s) can be single or block of the statement followed by the loop construct. Statement executes until condition remains true.
- Updates: Update is a part of the loop where a variable updates by incrementing or decrementing it.
Types of Loop
Unlike other programming languages, in go only one loop is there that is for a loop. The below syntax of for loop is given as below.
The syntax of for loop
The following figure demonstrates, how for loop works and what will be the execution sequence of for loop.
- As shown in the above figure, at first initialization is performed.
- After initialization, the condition is checked.
- If Condition evaluates as true, then the block of statement is executed.
- After the end of the block of statements, loop jumps to the update section and perform updates.
- After an update, loop jumps to the condition block, and if the condition is true then again block of statement is performed and the cycle continues as shown in the figure.
- If the condition evaluates false, loop jumps out of the block and program continues.
Example-1:
for i:=1 ; i<= 10 ; i++ {Explanation
fmt.Println(i)
}
- The above program snippet print the series of numbers from 1 to 10.
- Here, variable "i" is called counter because it is used to count the loop rounds.
Example-2:
i := 1
for ; i <= 10 ; i++ {
fmt.Println(i)
}
- Above program snippet works similar as in Example-1, The only difference is that in the above program snippet initialization is done out of the loop, and initialization section of loop is skipped.
- Because in for loop initialization and updates are optional so we can skip these.
Example-3:
i := 1Explanation:
for i <= 10 {
fmt.Println(i)
i++
}
- Above program snippet also works as in Example-1 and Example-1, The difference is that in this snippet, initialization and updates both are skipped, update is done as a part of loop.
- In go lang, if both initialization and updates are skipped, then there is no need to put the semicolon like in other languages C / C++ / Java.
- In this way, the for loop is also used similar to the while loop of other languages C / C++ / Java. So in go lang there is no support of while loop.
Example-4:
for i,term:=1,1 ; i <= 10; i++ {Output
fmt.Println(term)
term += 2
}
1
3
5
7
9
11
13
15
17
19
Explanation
- In the above program snippet, the first thing that you have to notice is initialization "i,term:=1,1". In this statement, two variable i and term are initialized with 1 as of go lang allowed multiple variables short declaration syntax. This is equivalent to i:=1 and term:=1
- The objective of the above program is to print the first 10 odd numbers. so in the above program loop is executed 10 times with counter variable i, which runs from 1 to 10.
- Because we want to print odd numbers and counter is not have odd values so we have created another variable term which starts from 1 and increment by 2 in each round of loop.
- In this way, loop executes for 10 times, and odd number first 10 odd numbers get printed by term variable.
Example-5
for ; ; {
fmt.Println("Hello")
}
OR
for {
fmt.Println("Hello")
}
Explanation
- Above two program snippet are similar, both are called infinite loop which executes forever and prints Hello.
- As in the for loop, if the condition is omitted, then it is treated true and becomes an infinite loop.
- To stop the infinite loop, press Ctrl + C to kill the go lang program/process, which forcefully stops your go program.
- It seems not useful in this program, but very much useful in real-world programming.
- The following example shows the uses of infinite loop.
Example-6:
i:=1
for {
if i > 10 {
break
}
fmt.Println("Hello")
i++
}
Explanation:
- Above program snippet, prints Hello for 10 times by using an infinite loop.
- To jump out from infinite loop we can use break statement as shown in the above example.
- In the above program, we run the infinite loop and print and increment variable "i " in each round of loop, but because we want to terminate the loop after printing the first 10 number, so in each round of loop we have checked the condition i > 10, whenever this condition becomes true, control jumps out of the loop and loop terminates.
- This type of methodology helps you to make a loop in the situation when the condition is too much complicated to guess or condition made with multiple cases.
Comments