How to use continue, break, goto statements in Golang 1.20
In this video we are going to generate source code for the GoLang Program, How to use continue, break, goto statements using chatGPT.
After generating we will verify and run the generated code.
IMPORTANT NOTE on SOURCE CODE: only source code that are acceptable by the youtube description box will be displayed.
In this program I have replaced the angled brackets with > or <. once you copy the source code to editor replace it with greater than or less than symbols respectively.
##############SOURCE CODE#########################
package main
import "fmt"
func main() {
// Using continue statement
fmt.Println("Using continue statement")
for i := 1; i <= 10; i++ {
if i%2 == 0 {
continue
}
fmt.Println(i)
}
// Using break statement
fmt.Println("Using break statement")
for i := 1; i <= 10; i++ {
if i > 5 {
break
}
fmt.Println(i)
}
// Using goto statement
fmt.Println("Using goto statement")
i := 1
loop:
fmt.Println(i)
i++
if i <= 10 {
goto loop
}
}
Below is the explanation for the program:
In this program, we demonstrate the use of continue, break, and goto statements.
The first loop uses the continue statement to skip even numbers and only print odd numbers between 1 and 10.
The second loop uses the break statement to exit the loop when the loop counter reaches 6.
The third loop uses the goto statement to create a labeled loop and repeatedly print the loop counter until it reaches 10.
As you can see, the program uses continue, break, and goto statements to control the flow of execution in the loops.
#go #goprogramming #golang #golangtutorial #golanguage