4 ms·
Did John Mccarthy invent the IF statement for all languages?
by math-dev 5y ago
Did John Mccarthy invent the IF statement for all languages?
- Jtsummers 5y agoThe if-expression and if-statement are different things. if-statements result in conditional execution of different statements, but do not return values themselves. Borrowing C's syntax for if statements the following is not allowed: int next = if (odd(current)) { 3*current + 1; } else {current / 2;}; That's not a valid expression in C. if-expressions return a value, and C does have an if-expression with its ternary operator: int next = odd(current)? 3*current + 1 : current / 2; if-statements are ubiquitous to nearly every programming language. Even when they aren't done with the if keyword and more familiar syntax, even unstructured languages with conditional branching instructions and goto permit you to write something like an if-statement (just disguised by the usually more verbose unstructured form). if-expressions are less universally available (though I'm unsure of any higher-level-than-assembly language still in popular use that doesn't have an if-expression equivalent).
- codr7 5y agoGo doesn't have if-expressions, which makes propagating errors even more painful.
- ggm 5y agoHow does this differ semantically from the C ternary operator? Not syntactically, in its outcome and expression of a testable boolean clause, and a left or right side choice which is the assignment value?
- codr7 5y agoNot much. But syntax matters; a ternary expression tells me at a glance that there's no funny stuff going on, it's just making a choice between two expressions. In Go I have to scan every single branch when reading to make sure.