Go Break and Continue
The Java 'break' statement terminates loops or switches prematurely, resuming execution after the loop. Specifically, within nested loops, it ends only the innermost loop. Similarly, the 'continue' statement skips specific loop iterations, moving to the next iteration without executing subsequent statements. Both are integral jump statements applicable in for, while, and do-while loops.
We have 3 types of control statements in golang :
- Break statement in golang
- Continue in golang
- Go-to statement in golang
Break Statement
The main purpose of the break statement is to "exit the loop for a particular case" when we don't want an unnecessary loop to be executed.
In a more concise way :
- When a loop encounters a break statement, the loop is instantly stopped or terminated, and program control goes on to the statement that follows the loop.
- It can also be used with a switch statement to terminate a case, and we mostly use it to check if one case becomes true. We will simply return the output from there only, no need to check the next statement.
- If you're using nested loops, the break statement can be used to halt the operation of the innermost loop and begin execution from the next line or next statement after the block of code.
The syntax for a break statement in Go is as follows :
break example :
Output :
Explanation of the code :
In the above example, we have used the for loop to print the value of i. with a break statement,
Here, when i is equal to 1, the break statement terminates the loop. Hence, the output doesn't include values after 1. Hence, we are saved from unnecessary iteration by using the break statement.
Another Example :
Output :
Explanation of the code :
In the above code, the loop will execute till 5 and then it will terminate as we have put the condition to stop the execution if we found our statement to be true, no need to execute unnecessary iteration after that.
Continue in Golang
In Go, Continue in Golang statement is used when we want to move the control of the program to the beginning. It will skip instructions of code inside the loop and continue with the next iteration.
For simple scenarios, Continue in Golang can be readily replaced with if-else conditions, however using continue statements makes our code more readable when we have several if-else conditions.
The syntax for a Continue in Golang is as follows :
The "continue statement" in this case skips the current loop iteration, regardless of the for loop's condition.
For Example :
Output :
Explanation of the code :
In the above example, we have used the for loop to print the value of i. Notice the use of the continue statement,
Here, the continue statement is carried out when i is equal to 6. As a result, it skips the current iteration and begins the following one. The result does not include the value 6.
Go Go-to Statement
Many programming languages have "go-to statements" it is a way of jumping code into a certain position which is used to control the flow of execution.
Its purpose is to direct execution to the specified statement.
The syntax for a go statement in Go is as follows :
For Example :
Output :
Explanation of the code :
The above code will skip 11 and start to execute again from the next iteration. Take into consideration that you cannot jump to labels that are not part of the same function. This is an excellent safety measure that clears up a lot of ambiguity.
Note :
The usage of the goto statement is strongly discouraged in any programming language since it makes identifying the control flow of a program difficult, making the program challenging to understand and difficult to alter. Any program that uses a goto can be rebuilt with another structure.
Here are a few examples of situations where we can utilize the use of the go-to statement when standard flow management procedures are insufficient to keep things under control without getting too complicated :
- We can use it when we have complex logic and require nested loops it would be a lot easier to use go-to in place of nested loops which improves readability and make the code understandable.
- It can be beneficial with a switch statement also.
Conclusion
- In this article, we will get to know a piece of detailed information about different types of control flow in golang and when to use which one.
- It is very handy to use with the program and saves us from unnecessary execution.
- We can use it with loop iteration or switch statements for several conditions.