The while loop

As discussed earlier, Go does not offer the while keyword for writing while loops, but it allows you to use a for loop instead of a while loop. This section will present two examples where a for loop does the job of a while loop.

The following is the typical case where you want to write something like while(condition):

for { 
} 

It is the job of the developer to use the break keyword to exit this for loop!

However, the for loop can also emulate a do...while loop that are found in other programming languages. As an example, the following Go code is equivalent to a do...while(anExpression) loop:

for ok := true; ok; ok = anExpression { 
} 

As soon as the ok variable reaches the false value, the for loop will terminate.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.214.155