Go For Loop

In this tutorial you will learn about the Go For Loop and its application with practical example.

Go for loop statement

The for loop is used when we want to execute block of code known times. In Go, basic for loop is similar as it is in C or Java, except that the ( ) are gone (they are not even optional) and the { } are required.

In Go, for loop can be used in following forms –

Syntax :-

Property Description
initialization Sets a counter variable
condition It test the each loop iteration for a condition. If it returns TRUE, the loop continues. If it returns FALSE, the loop ends.
increment Increment counter variable

Example:-

Output:-

go_for_loop

Go For loop as a while loop

In GoLang, if we remove the “initialization” and “increment” statement, then the Go for loop behaves like a while loop as in other programming languages.

Example:-

Output:-

go_for_loop_as_while_loop

Go Infinite loop

In GoLang, if we further remove the “condition”, then the Go for loop turns into an infinite loop.

Recommended:-  Go Structs

Syntax:-

Example:-

In this tutorial we have learn about the Go For Loop and its application with practical example. I hope you will like this tutorial.