Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

For loop Increment with a step not working

Tags:

for-loop

go

The following Go program fails to compile

package main

import (
"fmt"
)

func main() {
var celcius int
for i := 0; i <= 300; i + 20 {
    celcius = 5 * (i - 32) / 9
    fmt.Printf("%d \t %d\t \n", i, celcius)
}
}

The Error message is "i + 20 evaluated but not used" . How to give a step increment in golang for loop

like image 564
Cheral Irumborai Avatar asked Sep 20 '25 15:09

Cheral Irumborai


1 Answers

The compiler is complaining that the result of the expression i + 20 is not used. One fix is to assign result to i:

for i := 0; i <= 300; i = i + 20 {

A shorter and more idiomatic approach is to use +=:

for i := 0; i <= 300; i += 20 {

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!