Go programming – For loop

As part of the go programming series we will now see for loop programming. For loop in GO is similar to for loop in C. The for loop combines the usage of while loop like in C and there is no do-while.

The above code is a C style for loop and also a only for loop type that uses semmi colon.

Here The for loop is used like a while loop in C language.  The syntax is as follows

for condition { }

Here the condition nothing but a boolean which will continue running till its true. Here the loop will run as long as the variable condition_to_run  is true. The condition_to_run is set to false after it turns 5. I have added a sleep function to add some slowness to the code to get a feel of it.  You can refer to time package reference manual for more detail about the sleep, I will add a new post for covering the time alone.

The next for loop is like a C for(;;) with a syntax like shown below.

for { }

This loop continues infinitely until a break or return statement occurs. Here a break is called when the variable breakpoint goes above 10.

You can download the source code from https://github.com/codelectron/GoLangSeries .

Leave a Reply

Your email address will not be published. Required fields are marked *