Golang Continue Does It Continue the Loop or Goes to Next Iteration

Golang Continue

Introduction to Golang Continue Statement

In go language continue statement allow the developer to skip the particular condition and start from the next value, for example, if we have a list of students and we want to add some extra marks to each student except for the students belongs to a particular section, in that case, we can put continue statement in the loop and the execution will start from the next section, we can say continue is little similar to break of other programing language and it allows us to skip the iteration of the loop at any time inside the loop, with the help of the continue statement we are able to avoid unnecessary execution of some codes.

Syntax:

Below is a simple syntax for the continue statement, here we have written continue inside the loop, this loop can be for a loop. Generally, we put this continue on any condition where we want to skip the execution and go to the next iteration of the execution of the program. Mostly we use the continue statement to skip the execution of some specific conditions or simply move to the next iteration of the loop.

Loop{
continue; //Here we have used the continue statement inside the loop
}

How Does Continuation Statement Work in Go language?

To explain the working of the continue statement in go language we have taken the diagram below .If we have used break of other languages then it is very much similar to those breaks. In case if we do not want to execute code for some specific iteration of the code then we go with the continue statement. we can say continue statement allow us to avoid execution of peace of the code for some specific iteration, hence we can understand it saves our program of extra execution of the codes. Let me explain working with the help of a given diagram.

  • Once the continue statement is read by go language it will simply go to the next statement, by skipping the current iteration, you can even consider it like breaking of the current iteration. Remember iteration will be continued with the next iteration with skipping the current one.
  • It may be confusing for many people with names continuing, as it is breaking iteration. Actual it is stopping execution of current item of iteration and going to the next item for iteration which means it allows loop iteration to be continued with the next item.
  • In the below statement the first condition will be checked, if the condition is successful or true then the statement will execute with a new or next value.
  • Let us take an example, suppose university decided to pass all the students of the school except the students belongs to any particular section, then that case we will run a loop over all students of the school and we will put a condition where we will check for the section of students. We will keep passing the all students, but once we found that particular section we will use the continue statement which will skip the processing of the students of that section.
  • In Go, language continuation is similar to the break of other languages.
  • In memory, it keeps track of the next iteration and once we put continue inside the loop, it will go to the next iteration by skipping the current iteration.

Please see the below diagram for some understanding of the continue in go language.

golang continue

Examples to Implement Golang Continue

In the below, we have given some examples where we can understand the working and uses of the continue statement. in case if we want to execute these examples we can create a file like continue.go and paste the codes of the examples on this file and we can run the command go run continue.go and we can get the output of the program.

Example #1

In the below example we are printing the number from 15 to 25, we have used a condition where we are using number== 20 and if the condition matches, the continue will execute hence 20 will not be printed.

Code:

package main
import "fmt"
funcmain() {
var number int = 15
for number < 25 {
if number == 20 {
number = number + 1;
continue;
}
fmt.Printf("The value of the number is: %d\n", number);
number++;
}
}

Output:

Golang-Continue-Example-1

Example #2

In the below example we are simply printing all the odd numbers between 15 to 25, so we have used the condition oddNumber%2==0.

Code:

package main
import "fmt"
funcmain() {
varoddNumberint = 15
for oddNumber< 25 {
if oddNumber%2==0 {
oddNumber = oddNumber + 1;
continue;
}
fmt.Printf("The odd numbers are: %d\n", oddNumber);
oddNumber++;
}
}

Output:

Golang Continue Example 2

Example #3

In the below example we are simply printing all the even numbers between 15 to 25, so we have used the condition! (evenNumber%2==0).

Code:

package main
import "fmt"
funcmain() {
varevenNumberint = 15
for evenNumber< 25 {
if !(evenNumber%2==0) {
evenNumber = evenNumber + 1;
continue;
}
fmt.Printf("The Even numbers are: %d\n", evenNumber);
evenNumber++;
}
}

Output:

Even Number Example 3

Example #4

In the below example we are printing the number from three to nine, we have used a condition where we are using number== 8 and if the condition matches the continue will execute the hence 8 will not be printed, 8 iterations will be skipped.

Code:

package main
import "fmt"
funcmain() {
for number := 3; number < 10; number++ {
if number == 8 {
fmt.Println("Here loop will continue for the next iteration")
continue
}
fmt.Println("The values of the variable number is", number)
}
fmt.Println("End of the program happens")
}

Output:

Variable Number Example 4

Conclusion

From this tutorial, we learned the basics of the continue statement in the go language and we learned the concept with the help of the various examples. We learned the working of the continue with the help of a diagram and focus on a simple syntax.

Recommended Article

This is a guide to Golang Continue. Here we discuss the basic concept of the Golang Continue Statement and how Does Statement Work in Go language along with the help of some useful examples and Code Implementation. You can also go through our other suggested articles to learn more –

  1. Rust vs Golang – Top Differences
  2. Perl vs Ruby – Top Differences
  3. Guide to Ruby Tools

ramireztallech.blogspot.com

Source: https://www.educba.com/golang-continue/

0 Response to "Golang Continue Does It Continue the Loop or Goes to Next Iteration"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel