Thursday, December 7, 2023
HomeSoftware EngineeringUnderstanding For Loops in Golang

Understanding For Loops in Golang


In Golang a for loop is a approach to loop via an iterable.

Essentially the most primary For Loop

i := 0
for i <= 3 {
  i = i + 1
}

A basic For Loop

for i := 7; i <= 9; i++ {
  // do one thing
}

For Loop with out Circumstances

A for with no situation will loop perpetually, till both a break or return is hit.

for {
  // do one thing
  break // kill the loop
}

When to make use of proceed

A proceed will transfer to the following iteration of the loop

for i := 0; i <= 5; i++ {
  if ipercent2 == 0 {
    // skip if even quantity
    proceed // transfer to subsequent iteration
  }
  // do one thing with odd quantity
}
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments