Do-While Loop in Golang

Create new folder named src. In src folder, create new file named main.go as below:

package main

import "fmt"

func main() {

	fmt.Println("Demo 1")
	Demo1()

	fmt.Println("\nDemo 2")
	Demo2()

	fmt.Println("\nDemo 3")
	Demo3()

	fmt.Println("\nDemo 4")
	Demo4()

}

func Demo1() {
	i := 1
	for {
		fmt.Printf("%3d", i)
		i++
		if i == 10 {
			break
		}
	}
}

func Demo2() {
	ch := 'a'
	for {
		fmt.Printf("%3c", ch)
		ch++
        if ch == 'z' {
			break
		}
	}
}

func Demo3() {
	var total int = 0
	i := 1
	for {
		total += i
		i++
        if i == 10 {
			break
		}
	}
	fmt.Println("Total: ", total)
}

func Demo4() {
	var counter int = 0
	i := 1
	for {
		if i%2 == 0 {
			counter++
		}
		i++
		if i == 10 {
			break
		}
	}
	fmt.Println("Counter: ", counter)
}




Open Terminal windows in Visual Studio Code and run command line: go run main.go

Demo 1
  1  2  3  4  5  6  7  8  9 10

Demo 2
  a  b  c  d  e  f  g  h  i  j  k  l  m  n  o  p  q  r  s  t  u  v  w  x  y  z

Demo 3
Total:  55

Demo 4
Counter:  5