golang tutorial - Go Loops | Golang loops - golang - go programming language - google go - go language - go program - google language



Golang Loops

 for loop
  • Loops are used in programming to repeat a specific block until some end condition is met.
  • Loops are handy, if you want to run the same code over and over again, each time with a different value.
 if statement

for Loop

  • The syntax of for loop is:
for (initializationStatement; testExpression; updateStatement)
{
       // codes 
}
click below button to copy the code. By - golang tutorial - team

How for loop works?

  • The initialization statement is executed only once.
  • Then, the test expression is evaluated. If the test expression is false (0), for loop is terminated. But if the test expression is true (nonzero), codes inside the body of for loop is executed and the update expression is updated.
  • This process repeats until the test expression is false.
  • The for loop is commonly used when the number of iterations is known.
 for loop

Different Kinds of Loops

  • Go language supports different kinds of loops:
Loop Type Description
for loop loops through a block of code a number of times
nested loops You can use one or more for loop inside any for loop.

Here are three basic types of for loops.

  • Go language supports different kinds of loops:
    • The most basic type, with a single condition.
    • A classic declaration of for loop will have -> initialization (int i=0)/condition(i<10)/after values(i++)
    • Never ending for loop will get executed in case of-> If the condition is not satisfied and the loop become infinite.

Example

package main

	import "fmt"

	func main() {

	    i := 1
	// The most basic type, with a single condition.

    for i <= 3 {
        fmt.Println(i)
        i = i + 1
    }

	// A classic initial/condition/after for loop.
	    for j := 7; j <= 9; j++ {
        fmt.Println(j)
    }
// for without a condition will loop repeatedly until you break out of the loop or return from the enclosing function.
	    for {
        fmt.Println("loop")
        break
    }
// You can also continue to the next iteration of the loop.
	    for n := 0; n <= 5; n++ {
        if n%2 == 0 {
            continue
        }
        fmt.Println(n)
    }
}
click below button to copy the code. By - golang tutorial - team

output

$ go run for.go
1
2
3
7
8
9
loop
1
3
5

Loop Control Statements

  • Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed.
  • C supports the following control statements. Click the following links to check their detail.
Control Statement Description
break statement The break statement "jumps out" of a loop.
continue statement The continue statement "jumps over" one iteration in the loop

break Statement

  • The break statement terminates the loop (for, while and do...while loop) immediately when it is encountered.
  • The break statement is used with decision making statement such as if...else.

Syntax of break statement

break;
click below button to copy the code. By - golang tutorial - team

Flowchart of break statement

 for loop

How break statement works?

 for loop

continue Statement

  • The continue statement skips some statements inside the loop. The continue statement is used with decision making statement such as if...else.
golang , gopro , google go , golang tutorial , google language , go language , go programming language

Syntax of continue Statement

continue;

click below button to copy the code. By - golang tutorial - team
golang , gopro , google go , golang tutorial , google language , go language , go programming language

Flowchart of continue Statement

 flowchart continue statment
golang , gopro , google go , golang tutorial , google language , go language , go programming language

How continue statement works?

 continue statment work
golang , gopro , google go , golang tutorial , google language , go language , go programming language

The Infinite Loop:

  • A loop becomes infinite loop if a condition never becomes false or never get satisfied. for loop is mostly used for this purpose.
  • Since none of the three expressions that form the for loop are required, you can make an endless loop by leaving
  • A loop becomes infinite loop if a condition never becomes false or never get satisfied. for loop is mostly used for this purpose. Since none of the three expressions that form the for loop are required, you can make an endless loop by leaving
    • conditional expression empty
    • pass true to it.
package main

import "fmt"

func main() {
   for true  {   // make it infinite
       fmt.Printf("This loop will run forever.\n");
   }
}
click below button to copy the code. By - golang tutorial - team
  • When the conditional expression is absent, it is assumed to be true. You may have an initialization and increment expression, but C programmers more commonly use the for(;;) construct to signify an infinite loop.
    • conditional expression empty
    • pass true to it.
  • NOTE: You can terminate an infinite loop by pressing Ctrl + C or Ctrl + k
    • conditional expression empty
    • pass true to it.

Related Searches to Go loops | Golang Loops