by usrbinbash · 4y ago · 0 comments
What is sometimes confusing, is that some statements also double as expressions, that is, also return values; and that it also depends on the language. eg. the assignment statement returns a value in C and Javascript, but not in Go or Python;
printf("%d\n", a = 42);
// out: 42
console.log(a = 42)
// out: 42
fmt.Printf("%d\n", a = 42)
// out: syntax error: unexpected =, expecting comma or )
print("%d" % a = 42)
# out: SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
Also depending on the language, expressions themselves may be valid statements. For example, this compiles in C; int main() {
12 + 42;
}
;this is a valid python program: 12 + 42
;but if I try this in Go: package main
func main() {
42
}
// ./main.go:3:5: 12 + 42 (untyped int constant 54) is not used