Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

broken for #486

Closed
gopherbot opened this issue Jan 2, 2010 · 2 comments
Closed

broken for #486

gopherbot opened this issue Jan 2, 2010 · 2 comments

Comments

@gopherbot
Copy link
Contributor

by graycardinalster:

code:

package main
import "fmt"

func main () {
    var i byte
    for i = 0; i <= 255; i++ {
        fmt.Printf ("%d ", i)
    }
}

go awhile
@peterGo
Copy link
Contributor

peterGo commented Jan 2, 2010

Comment 1:

Your code fragment doesn't compile: "11: syntax error near go".
In your example, the Go for statement executes as expected; it executes an infinite
loop, as it would in most languages.
http://golang.org/doc/go_spec.html#For_statements
In Go, byte is an alias for uint8, with a range of 0 through +255.
http://golang.org/doc/go_spec.html#Numeric_types
In Go, unsigned integer operations discard high bits upon overflow.
http://golang.org/doc/go_spec.html#Integer_overflow
In the for loop, after i reaches +255, the i++ statement increments it to +266 and
then discards the overflowing high order bits, leaving a byte (uint8) value of zero.
The for statement cycles the loop variable i through the range 0 to +255 forever.
If you change the for statement condition from i <= 255 to i < 255, the loop will
terminate.
Questions about language usage, as opposed to confirmed bugs, are best posed in the
Go Nuts mailing list.

@rsc
Copy link
Contributor

rsc commented Jan 3, 2010

Comment 2:

Assuming the bug report is about the fact that the
loop never stops, yes, that's correct behavior,
because every possible byte value is <= 255, so
i <= 255 is always true.

Status changed to WorkingAsIntended.

@golang golang locked and limited conversation to collaborators Jun 24, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants