Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

ocaml
if-else
communitycreator

How can we use the if-else statement in Ocaml?

Chinweuba Elijah Azubuike

Overview

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.

Syntax

This is the syntax for writing an if-else statement in Ocaml:

if condition then code else code
Syntax for writing an if-else statement in Ocaml

Example

let b = 6;;
let a = 7;;
    if a > b then print_int(a) else print_int(b);;
An illustration of the use of the if-else statement in Ocaml

Explanation

  • Lines 1–2: We declare and initialize our variables.
  • Line 3: We use the 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.

RELATED TAGS

ocaml
if-else
communitycreator

CONTRIBUTOR

Chinweuba Elijah Azubuike
RELATED COURSES

View all Courses

Keep Exploring