A loop is a concept where something continues endlessly until it is asked to stop.
Just like the sun rises in the east and sets in the west is a loop that continues endlessly.
Let us take the below example to understand loops.
Say, you are asked to print the String "Hello World", five times on the screen.
A simple solution would be,
print("Hello World") print("Hello World") print("Hello World") print("Hello World") print("Hello World")
So, we have printed "Hello World" five times on the screen.
Simple! Right?
But, what if you are asked to print "Hello World" 100 times or even 1000 times on the screen.
You can't just write 100 or 1000 'print(...)' statements.
And this is where 'loops' comes into picture.
And 'for loop' helps us achieve it.
Let us dive into the code directly and understand it eventually.
package main import "fmt" func main() { for i := 1; i <= 5; i++ { fmt.Println("Hello World") } }
Now, if we break the contents of 'for' loop,
We get :
All together forms the 'for' loop.
for i := 1; i <= 5; i++ { fmt.Println("Hello World") }
So, the above 'for loop' is a three step process :
Now, the question is, how does the String 'Hello World' gets printed '5' times. Let us look at it below :
The print statement,
Has to be repeated 5 times in order to print 'Hello World'. And this happens because the 'for loop' runs '5' times.
So, when the 'for loop' begins, initially the variable 'i' is assigned with value '1'(i.e. 'i := 1').
So in the 1st iteration,
The control gets into the 'for loop' and checks, if the value of 'x' is less than or equal to '5'(i.e. 'x <= 5').
In this case the value of 'x' is '1'. Which is less than '5'.
So, the control enters the 'for loop'.
for i := 1; i <= 5; i++ { fmt.Println("Hello World") }
and finds the print statement,
So, it prints 'Hello World'.
Next, Go finds, it has come to the end of the loop. So, it increments the value of 'i' by 1 (As mentioned in the increment part).
And the second Iteration starts.
Same way, the control comes to the 'for loop' and checks, if the value of 'x' is less than or equal to '5'(i.e. 'x <= 5').
In this case the value of 'x' is '2'. Which is less than '5'.
So, the control enters the 'for loop'.
for i := 1; i <= 5; i++ { fmt.Println("Hello World") }
And finds the print statement,
So, it prints 'Hello World' for the second time.
Similarly, the 'range(6)' function increments the value of 'x' by '1'. And the value of 'x' becomes '3'.
Similarly, the third, fourth and fifth Iteration continues incrementing the value of 'x',printing it.
Once again, the control comes to the 'for loop' and checks, if the value of 'x' is less than or equal to '5'(i.e. 'x <= 5').
In this case, the value of 'x' is '6'. Which doesn't satisfy the condition, i.e. 'x <= 5'.And the control comes out of the 'for loop'.
Now, if you see the output, 'Hello World' is exactly printed 5 times.
We can use the 'for loop' using the conditional part only(If we want to).
package main import "fmt" func main() { i := 1 for i <= 5 { fmt.Println("Hello World") i++ } }
package main import "fmt" func main() { for true { fmt.Println("Hello World") } }
So, if we see the above output, 'Hello World' is printed infinite times because we didn't ask the 'for loop' to ever end.
So, it continues endlessly.