The if-else
statement is a flow-control statement for all programming languages. It is used for decision making during the execution of a code flow. Although the working of the if-else
statement is common to all programming languages, it has different syntax depending on the programming language used. In this shot, we will learn how to use an if-else
statement in Ocaml.
This is the syntax for writing an if-else
statement in Ocaml:
if condition then code else code
let b = 6;;let a = 7;;if a > b then print_int(a) else print_int(b);;
if-else
statement to print a
if it is greater than b
and vice versa. Notice how we follow the exact description of the syntax. After the if
condition, we use the then
keyword to wrap the code we want to execute.