The "statement; statement"

package main

import (
	"fmt"
	"math/rand"
)

// x = 40
func main() {
	x := 4

	if z := 5 * rand.Intn(x); z >= x {
		fmt.Printf("z is greater than x")
}

the above code is not working but the below code is working:

package main

import (
	"fmt"
	"math/rand"
)

// x = 40
func main() {
	x := 4

	if z := 5 * rand.Intn(x); z >= x {
		fmt.Printf("z is greater than x")
	} else {
		fmt.Printf("z is smaller than x")
	}
}

Resource: The Go Programming Language Specification - The Go Programming Language

Hello, @GauravJiSri, and welcome to the Go community!

Is there a closing } missing from your first example?

EDIT:

With an added } for ending the if block, this will compile and execute:

package main

import (
	"fmt"
	"math/rand"
)

// x = 40
func main() {
	x := 4

	if z := 5 * rand.Intn(x); z >= x {
		fmt.Printf("z is greater than x")
	} // end if
	
} // end main

Depending upon the randomized value assigned to z, this will sometimes be output:

z is greater than x%

Does that conform to what you were trying to do?

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.