Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




GO - FOR LOOP


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,


Example :



print("Hello World")
print("Hello World")
print("Hello World")
print("Hello World")
print("Hello World")


Output :



 Hello World
 Hello World
 Hello World
 Hello World
 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.


Example of 'for loop'


Let us dive into the code directly and understand it eventually.


Example :



package main
import "fmt"
    
func main() {
        
    for i := 1; i <= 5; i++ {
        fmt.Println("Hello World")
    }
}


Output :



 Hello World
 Hello World
 Hello World
 Hello World
 Hello World

Now, if we break the contents of 'for' loop,


for i := 1; i <= 5; i++

We get :

  1. Initialisation part : It is run before the loop starts.

    i := 1


    'i' is getting initialized with value '1'.
  2. Conditional part : It states the condition for running the loop.

    i <= 5


    It says, continue to be in the loop until the value of 'i' is less than or equal to '5'.
  3. Increment part : It is run each time after the loop.

    i++


    With each Iteration(We will explain iteration) the value of 'i' gets incremented by '1'.

All together forms the 'for' loop.


for i := 1; i <= 5; i++ {
    fmt.Println("Hello World")
}

java_Collections

So, the above 'for loop' is a three step process :

  1. In the initialisation part, 'i := 1', the value of 'i' is assigned with '1'.
  2. With each iteration of for loop, the value of 'i' is incremented by 1 (i.e. 'i++').
  3. And, due to the conditional part (i.e. 'i <= 5'), the 'for loop' continues until the value of 'i' is less than or equal to 5.

Now, the question is, how does the String 'Hello World' gets printed '5' times. Let us look at it below :


The print statement,


fmt.Println("Hello World")

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').


java_Collections

java_Collections

So in the 1st iteration,


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').


java_Collections

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,


fmt.Println("Hello World")

So, it prints 'Hello World'.


Output :



 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).


java_Collections

java_Collections

And the second Iteration starts.


2nd Iteration


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,


fmt.Println("Hello World")

So, it prints 'Hello World' for the second time.


Output :



 Hello World
 Hello World

Similarly, the 'range(6)' function increments the value of 'x' by '1'. And the value of 'x' becomes '3'.


java_Collections

Similarly, the third, fourth and fifth Iteration continues incrementing the value of 'x',printing it.


3rd Iteration

Output :



 Hello World
 Hello World
 Hello World

java_Collections

4th Iteration

Output :



 Hello World
 Hello World
 Hello World
 Hello World

java_Collections

5th Iteration

Output :



 Hello World
 Hello World
 Hello World
 Hello World
 Hello World

java_Collections

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.


Rewriting the for loop using the conditional part only


We can use the 'for loop' using the conditional part only(If we want to).


Example :



package main
import "fmt"
    
func main() {
        
    i := 1
    for i <= 5 {
        fmt.Println("Hello World")
        i++
    }
}


Output :



 Hello World
 Hello World
 Hello World
 Hello World
 Hello World

Infinite loop


Example :



package main
import "fmt"
    
func main() {
        
    for true {
        fmt.Println("Hello World")
    }
}


Output :



 Hello World
 Hello World
 Hello World
 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.


for true {

So, it continues endlessly.