One of the key elements of almost every computer program is taking different execution paths (branches) depending on some criterion. Take, for example, a program that parses a bank account registration form for a customer. If one of the input fields isn’t entered correctly, such as a Social Security number having letters or a name having numbers, the program should alert the user and ask for correct input rather than proceeding. You can define this program behavior using an if block. In our tsunami simulator, for now we’ll use an if block to check the values of the input grid and physics parameters, as shown in the following listing.
Listing 2.6 Checking for values of input parameters
if (grid_size <= 0) stop 'grid_size must be > 0'
if (dt <= 0) stop 'time step dt must be > 0'
if (dx <= 0) stop 'grid spacing dx must be > 0'
if (c <= 0) stop 'background flow speed c must be > 0'
Here, we check the values of the parameters to make sure the program can carry out a meaningful simulation. Specifically, we need a grid with at least one element, although this won’t make for a particularly interesting simulation. We also need time step, grid spacing, and background flow speed to all have positive values. The conditions are stated in parentheses, immediately after if. On the right side, we specify the statement to be executed if the condition in parentheses evaluates as true. In this case, we use the stop statement to abort the program execution and print a helpful message for the user.
This is only the simplest kind of use case for an if statement. Here’s its general syntax:
if (condition) ...
You can use a more verbose if block if you need to execute multiple statements on a condition, as shown in the following listing.
Listing 2.7 General syntax of an if block with one condition and one branch
if (condition) then
... ❶
end if
❶ This branch will execute if condition is true.
So far, the statements inside this if block execute only on a condition that evaluates as true, and nothing happens otherwise. If we need our program to do something in either case, we can use a more general if/else/end if block, as shown in the following listing.
Listing 2.8 General syntax of an if block with one condition and two branches
if (condition) then
... ❶
else
... ❷
end if
❶ This branch will execute if condition is true.
❷ This branch will execute otherwise.
Unlike the single-liner if and the if/end if block, this one allows for two branches of execution: one if condition is true, and another if it’s false. It’s also possible to test for multiple specific conditions in a single if block, as shown in the following listing.
Listing 2.9 The most general syntax of an if block
if (condition) then
...
else if (other_condition) then ❶
...
else
...
end if
❶ You can have as many of these as you want.
The conditions are expressions of the logical type. The comparison operators, like the ones we used to check the values of the input parameters, work just like the comparison operators in general arithmetic we learned in elementary school. There are a few other edge cases and logical operators that I’ll put on the back burner for now, and that we’ll explore later as we encounter them.
In summary, we have a few different forms of an if-block:
ifsingle-liner –Useful for simple checks and statements that fit on a single line; for example, zeroing a variable if negative:if(a<0)a=0if/endif–A more verbose form of the single-lineif; useful when you have a single condition but multiple statements to executeif/else/endif–Allows executing a statement for either the true or false value of the conditionif/elseif/else/endif–Likeif/else/endif, but allows checking the values of multiple specific conditions
That’s all you need to know about branching for now. We’ll apply these more complex if blocks in the following chapters.