golang tutorial - Go Loops | Golang loops - golang - go programming language - google go - go language - go program - google language
Golang Loops
- 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.
for Loop
- The syntax of for loop is:
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.
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
output
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
Flowchart of break statement
How break statement works?
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
golang , gopro , google go , golang tutorial , google language , go language , go programming language
Flowchart of continue Statement
golang , gopro , google go , golang tutorial , google language , go language , go programming language
How continue statement works?
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.
- 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.