Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
In this article, we will be covering the Go Loop Control Statement. Therefore before moving on to the main topic, we shall be covering what loops are. So loops are used when we want to do a particular action for a specific amount of time. A loop is a set of instructions that can be used to perform a repetitive task until a condition is reached.
What is a Loop?
A loop is a set of instructions that can perform a repetitive task until a condition is reached. You can learn more from here Go loops.
Break Statement
In Go, the break statement is used to terminate the execution of the current loop. A break statement is always paired with a conditional if statement.
When a break statement is encountered inside a loop, the loop gets immediately terminated, and the program control resumes.
It can be used to terminate a program in a switch statement.
If you are using nested loops, the break statement will stop the execution of the innermost loop and begin the execution of the following line of code after the block.
Syntax
break;
Flow Diagram
Example
package main
import "fmt"
func main() {
var a int = 10
for a < 20 {
fmt.Printf("value of a: %d\n", a);
a++;
if a > 15 {
break;
}
}
}
Output
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
In this example, we have used the break statement. We printed specific values, and after checking the condition, we exited the code.
Break with Nested Loops
If you have a nested set of loops, you will need a break for each loop if required. The break statement will only halt the execution of the innermost loop.
Example
package main
import "fmt"
func main() {
for i := 1; i <= 3; i++ {
for j := 1; j <= 3; j++ {
if i==2 {
break
}
fmt.Println("i=", i, "j=",j )
}
}
}
In this example, we used the nested loops with the continue statement. We checked the condition, and after we printed the output.
FAQs
What is a control statement in a loop? A loop consists of a loop's body and a control statement. The control statement consists of specific conditions that direct the body of the loop to execute.
What is the use of loop control in the Go language? The Go language comes with the support of these types of loop control statements. These are break statement which is used to terminate the loop. The continued statement skips the current iteration and shifts to the next iteration.
What is the function of the goto statement? The goto statement transfers the control to the labeled statement in the program. The label can be defined as the valid identifier and placed just before the statement from where the control is transferred.
Key Takeaways
In this blog, we have covered the Go Loop Control Statement and its implementation in GoLang. We gave a brief introduction to the topic. We also explained the different loop control statements, their syntax, and the example.We hope that this blog has helped you enhance your knowledge regarding go loop control statements and if you would like to learn more, check out our articles onGo Loops. Do upvote our blog to help other ninjas grow. Happy Coding!”