Web Development With HHVM and Hack 4: Conditional Statements

Mike Abelar
3 min readFeb 22, 2020

--

In this tutorial, we will cover conditional statements in Hack. In the previous tutorial, we covered variables in Hack: https://medium.com/@mikeabelar/web-development-with-hhvm-and-hack-3-variables-eb07c33c7015

What Are Conditional Statements?

Conditional statements allow you to execute code based on a condition. For example, say we have a variable that represents a product of two numbers. Let’s call this variable, sum . Then, we want to execute one piece of code if sum is greater than 25. On the other hand, we want to execute another piece of code if sum is less than or equal to 25. How would we go about this? We need conditional statements.

Conditional Statements Example

As always, let’s create a new directory for this project and inside the directory run:

touch .hhconfig

Then we can create a file to experiment with conditional statements:

touch cond.hack

Let’s fill in the contents of the Hack file:

<<__EntryPoint>>
function main(): noreturn {
// code here
$sum = 20 + 10;
if ($sum > 25) {
print("sum is greater than 25");
} else {
print("sum is less than or equal to 25");
}
print("\n");
exit(0);
}

Let us check our code by running our type checker: hh_client cond.hack . We will see that there are no errors so we are free to run our code: hhvm cond.hack which prints “sum is greater than 25”. Indeed, this is true because sum is 30. Now, let’s change sum to be $sum = 10 + 5; . Run the code again and now it should print “sum is less than or equal to 25”.

Looking at the code, we see an if statement, followed by parentheses. Inside these parentheses is an expression that is evaluated to either be true or false. The first time we run the code, we compare if 30 is greater than 25, which is true. Therefore, the block of code surrounded by curly brackets is executed. After the curly brackets is an else statement that executes code if the statement is false. Note: the else statement is optional.

Comparators

In the example above, we checked if 30 was greater than 25 by using > . We have choices of comparators:

> : greater than

< : less than

>= : greater than or equal to

<= : less than or equal to

== : equal to

Note: these not only apply to numbers. They can also apply to strings:

<<__EntryPoint>>
function main(): noreturn {
// code here
$name = "Mike";
if ($name == "Mike") {
print("The name is Mike!");
} else {
print("The name is not Mike!");
}
print("\n");
exit(0);
}

In this case, the code prints “The name is Mike!” because name is equal to “Mike”. You can also use inequality operators with strings; however, in these cases, the underlying ASCII values of the strings get compared.

Logical Operators

We can spice up our conditional statements. Say that sum is equal to 30. We know that

($sum > 20) is true

However, we can negate statements with ! :

(!($sum > 20)) is false

We can evaluate two or more conditions at once using && which represents “and”. In an “and” statement, all conditions must be true for the expression to be true:

($sum > 20 && $sum > 1) is true

while

($sum > 20 && $sum > 100) is false

We can also use|| to represent “or”. In an “or” statement, only one condition needs to be true:

($sum > 20 || $sum > 1) is true

and

($sum > 20 || $sum > 100) is true because the first condition is true

Else If Statements

You may want to execute code given multiple scenarios. For instance, say you wanted to run one piece of code when sum is less than 10, run another piece when sum is greater than 9 and less than 20, and a final piece when sum is greater than 19. You can use else if conditions:

<<__EntryPoint>>
function main(): noreturn {
// code here
$sum = 10 + 5;
if ($sum < 10) {
print("Less than 10");
} else if ($sum > 9 && $sum < 20) {
print("greater than 9, less than 20.");
} else {
print("greater than 19");
}
print("\n");
exit(0);
}

In this case, the code will print “greater than 9, less than 20.” because the sum is 15. Think of else if statements as additional if statements, which are mutually exclusive from the other branches of conditional logic.

In the next tutorial, we cover vectors: https://medium.com/@mikeabelar/web-development-with-hhvm-and-hack-5-vectors-d982d62bf105

--

--