Now we know how to construct variables and functions in a Hack program. It would be nice to have some logical controls such as IF statements.
In Hack an if statement is syntactically the same as one in C++ or Java. The keyword “if” followed by parentheses that have the condition inside and open /closing braces which contain instructions to do if the the condition succeeds.
Chaining if statements is also the same as in Java and C++ “else if(x > 1)
do this;. Below is a program that uses to a small chain of if statements to determine if a number is even or odd. Java developers may really like Hack because of its similarities but also appears to be more ideal for web content retrieval.


Is the “if ($x % 2 == 1)” required? Would the program do the same thing if we just had an “else” there? What are the logical operators in Hack?
LikeLike
I was going to ask the same thing as Dr Palmer, except i was going to ask if you could embed if and else statements?
LikeLike
Yes you can embed if statements in Hack
LikeLike