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




GO - FOR RANGE LOOP


Say, you have a String 'x' that has value 'Hello' in it.


x := "Hello"

Now, you need to iterate all the values of the String.


For range Loop helps to achieve the above scenario.


Example :



package main
import ("fmt")
    
func main() {
    
    x := "Hello"
        
    for i,letter := range x {
        fmt.Printf("Index : %d Value : %c \n", i, letter)
    }
}


Output :



 Index : 0 Value : H
 Index : 1 Value : e
 Index : 2 Value : l
 Index : 3 Value : l
 Index : 4 Value : o

So, if you look at the above code, 'for range loop' has a different syntax compared to a 'for loop'.


for i,letter := range x {
    fmt.Printf("Index : %d Value : %c \n", i, letter)
}

There are two parts in a 'for range loop'.


for i,letter := range x

The right part has,


range x

Where 'x' is the actual string. And 'range' keyword says that the loop is a 'for range loop'.


And the left part has,


i,letter

And what happens is, at every iteration, letters are picked from the string and kept in the 'letter' variable.


Tough to understand?


Let's simplify explaining what happens at each iteration..


So, in the first line, we have stored 'Hello' inside 'x'.


x := "Hello"

java_Collections

Then used the 'for range loop' to iterate it.


for i,letter := range x {
    fmt.Printf("Index : %d Value : %c \n", i, letter)
}

And the iteration starts.


Just remember that the variable 'x', is stored in the below way.


java_Collections

Where, 0, 1, 2, 3, 4 and 5 are indexes. That can be used to refer each letter.


1st Iteration


In the 1st iteration, Go finds the 'range' keyword and understands that it is a 'for range loop'.


And it finds the variable 'x', just next to 'range' keyword.


java_Collections

So, it takes the first letter from the 'x' variable(i.e. 'H') along with its index (i.e. '0').


java_Collections

And places the first letter (i.e. 'H') in the letter variable and the index (i.e. '0') in the 'i' variable.


java_Collections

java_Collections

Now, if you look at the print statement,


fmt.Printf("Index : %d Value : %c \n", i, letter)

We have printed the index stored in variable 'i' and the first letter in variable 'letter'.


And we get the below output.


Output :



 Index : 0 Value : H

Next, we come to the second iteration.


2nd Iteration


Again the 2nd iteration, it takes the second letter from the 'x' variable(i.e. 'e') along with its index (i.e. '1').


java_Collections

And places the first letter (i.e. 'e') in the letter variable and the index (i.e. '1') in the 'i' variable.


java_Collections

java_Collections

Now, if you look at the print statement,


fmt.Printf("Index : %d Value : %c \n", i, letter)

We have printed the index stored in variable 'i' and the first letter in variable 'letter'.


And we get the below output.


Output :



 Index : 0 Value : H
 Index : 1 Value : e

Similarly, in the next iterations, all the values are printed eventually.


Output :



 Index : 0 Value : H
 Index : 1 Value : e
 Index : 2 Value : l
 Index : 3 Value : l
 Index : 4 Value : o

Note : 'for range loop' is also used to Iterate Array and some other data types which we would be learning in the coming tutorials.