IF Statements

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 Even or odd code
This Hack program tests if the number 4 is even or odd
Output Number state
The Number is an even number so therefore it prints “This number is even”

3 thoughts on “IF Statements

  1. 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?

    Like

Leave a reply to hackprogramming Cancel reply