From the course: ReasonML: First Look

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Decisions with if and switch expressions

Decisions with if and switch expressions - ReasonML Tutorial

From the course: ReasonML: First Look

Start my 1-month free trial

Decisions with if and switch expressions

- [Instructor] Every non trivial program you write has to do some sort of decision making. In Javascript, you use if and switch statements. In Reason, you use if and switch expressions. Let's investigate how they work. Here's a calculation we'd like to do. Given the unit price and quantity, calculate the total for a purchase with a 10% discount if the total is a thousand dollars or more, and add five percent tax. To do this, we need an if expression. Let me emphasize that, an expression. In Reason, if is not a statement, it's an expression whose value can be bound to a variable. Here's the code and here's our conditional expression. The if, the condition, the value if true, the value if false, which will get bound to subtotal. In order for an if expression to be valid, it must yield a value for the true condition and the false condition. Additionally, the values for the if and else clauses can't have different types…

Contents