9951 explained code solutions for 126 technologies


golangHow to break FOR loop


package main
import "fmt"

func main() {
  nums := [5]int{1,2,3,4,5}
  for i, item := range nums {
    fmt.Printf("nums[%d] = %d \n", i, item)
    if ( i >= 1 ) {
      break
    }
  }
}ctrl + c
package main

default package declaration

import "fmt"

loads fmt package to operate on strings (and print them)

nums

array with 5 numbers we plan to iterate over

i, item := range nums

iterate over nums while saving index to i and value to item on each iteration

if ( i >= 1 ) {

sample condition to break loop on

break

breaks current loop