Q:

Golang goto, break, continue | Find Output Programs | Set 2

belongs to collection: Golang Find Output Programs

0

This section contains the Golang goto, break, continue find output programs (set 2) with their output and explanations.

Program 1:

package main

import "fmt"

func main() {
	for num := 1; num <= 10; num++ {
		if num == 6 {
			break
		}
		fmt.Println("Hello World")
	}
}

Program 2:

package main

import "fmt"

func main() {
	for cnt := 1; cnt <= 3; cnt++ {
		for num := 1; num <= 10; num++ {
			if num == 3 {
				break
			}
			fmt.Println("Hello World")
		}
	}
}

 

Program 3:

package main

import "fmt"

func main() {
	for cnt := 2; cnt <= 5; cnt++ {
		for num := 1; num <= 10; num++ {
			fmt.Print(cnt*num, " ")
		}
		fmt.Println()
		if cnt == 4 {
			break
		}
	}
}

 

Program 4:

package main

import "fmt"

func main() {
	for num = 1; num <= 10; num++ {
		if num == 6 {
			continue
		}
		fmt.Print(num, " ")
	}
}

 

Program 5:

package main

import "fmt"

func main() {
	var num = 0
	for num = 1; num <= 10; num++ {
		if num == 6 {
			continue
		}
		fmt.Print(num, " ")
	}
}

All Answers

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

Answer Program 1:

Output:

Hello World
Hello World
Hello World
Hello World
Hello World

Explanation:

In the above program, we created a loop to execute 10 times based on the value of the num variable. But it will print the "Hello World" message only 5 times. Because we used the break statement to exit from the loop when the value of num becomes 6.


 

Answer Program 2:

Output:

Hello World
Hello World
Hello World
Hello World
Hello World
Hello World

Explanation:

The above program will print the "Hello World" message 6 times on the console screen. As we know that, the break statement will break only the immediate loop. Here, break statement only breaks the inner loop.


 

Answer Program 3:

Output:

2 4 6 8 10 12 14 16 18 20 
3 6 9 12 15 18 21 24 27 30 
4 8 12 16 20 24 28 32 36 40 

Explanation:

In the above program, we printed tables from 2 to 4 on the console screen. Here, outer loop will be terminated when the value of the cnt variable becomes 4, That's why it will print the table till 4 instead of 5.


 

Answer Program 4:

Output:

./prog.go:6:6: undefined: num
./prog.go:7:6: undefined: num
./prog.go:10:13: undefined: num

Explanation:

The above program will generate syntax errors because here we did not declare the num variable.


 

Answer Program 5:

Output:

1 2 3 4 5 7 8 9 10 

Explanation:

 

The above program will print the numbers from 1 to 10 except 6 because we used the continue statement. The continue statement is a skipping statement, it will skip the execution loop body below the continue statement.

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 const, Type Casting | Find Output Programs ... >>
<< Golang goto, break, continue | Find Output Program...