Q:

Golang for Loop | Find Output Programs | Set 3

belongs to collection: Golang Find Output Programs

0

This section contains the for loop find output programs (set 3) with their output and explanations.

Program 1:

package main

import "fmt"

func main() {
	arr := []int{1, 2, 3, 4, 5}

	for index, value := range arr {
		fmt.Println("arr[", index, "]: ", value)
	}
}

Program 2:

package main

import "fmt"

func main() {
	arr := []int{1, 2, 3, 4, 5}

	for _, value = range arr {
		fmt.Print(value, " ")
	}
}

Program 3:

package main

import "fmt"

func main() {
	students := map[string]string{"101": "Amit", "102": "Arun", "103": "Anup"}

	fmt.Println("Student detail:")
	for roll, name := range students {
		fmt.Printf("%s : %s\n", roll, name)
	}
}

Program 4:

package main

import "fmt"

func main() {
	students := map[string]string{"101": "Amit", "102": "Arun", "103": "Anup"}

	fmt.Println("Student detail:")
	for roll := range students {
		fmt.Printf("%s\n", roll)
	}
}

Program 5:

package main

import "fmt"

func main() {
	students := map[string]string{"101": "Amit", "102": "Arun", "103": "Anup"}

	fmt.Println("Student detail:")
	for _, name := range students {
		fmt.Printf("%s\n", name)
	}
}

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer Program 1:

Output:

arr[ 0 ]:  1
arr[ 1 ]:  2
arr[ 2 ]:  3
arr[ 3 ]:  4
arr[ 4 ]:  5

Explanation:

In the above program, we created an array arr of integers. Then we used range to access index and value of array using for loop and printed the result on the console screen.


 

Answer Program 2:

Output:

./prog.go:8:9: undefined: value
./prog.go:9:13: undefined: value

Explanation:

The above program will generate errors because the variable value is not defined. The correct program is given below,

package main

import "fmt"

func main() {
	arr := []int{1, 2, 3, 4, 5}

	for _, value := range arr {
		fmt.Print(value, " ")
	}
}

// Output: 1 2 3 4 5 

 

Answer Program 3:

Output:

Student detail:
101 : Amit
102 : Arun
103 : Anup

Explanation:

In the above program, we created a map of students that contains roll-number and names. Then we accessed the student details from the map using the range in the for loop and printed the result on the console screen.


 

Answer Program 4:

Output:

Student detail:
101
102
103

Explanation:

In the above program, we created a map of students that contains roll-number and names. Then we accessed only keys from the map using the range in the for loop and printed the result on the console screen.


 

Answer Program 5:

Output:

Student detail:
Amit
Arun
Anup

Explanation:

 

In the above program, we created a map of students that contains roll-number and names. Then we accessed only values from the map using the range in the for loop and printed the result on the console screen.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Golang Find Output Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Golang Functions | Find Output Programs | Set 1... >>
<< Golang for Loop | Find Output Programs | Set 2...