Home

Conditional Statements

 

Conditions Fundamentals

 

Logical Constants

A condition is a technique of comparing two values. The comparison produces a value as true or false. To display the value of a logical operation in printf or printfn, use %b in the placeholder.

 

Logical Operators

To assist you with conditional statements, the F# language supports some conditional operators. They are:

Equality =: Two values are said to be equal if they have the same value. To perform this comparison, you can use the equality operator represented with =. It would be formulated as follows:

Value1 = Value2

If both values are the same, the comparison produces a true result. If the values are different, the comparison produces false.

Inequality <>: If you have two values and you want to find out whether they are different, you can use an operator as <> to compare both values. This would be formulated as follows:

Value1 <> Value2

If the values are different, the comparison produces a true result. If the values are equal, the comparison produces false.

Less Than <: If you suspect that one value is lower than another, to compare them, use the < operator. This would be formulated as follows:

Value1 < Value2

If the value on the left of the operator is lower than the value on the right side, the operation produces a true value. Otherwise, the result is false.

Less Than Or Equal To <=: If one value is lower than or equal to another, to compare them, use the <= operator. This would be formulated as follows:

Value1 <= Value2

If the left value is lower than the value on the right side or both values are the same, the comparison produces a true result. If the value on the left is greater than the other and they are not equal, the comparison produces false.

Greater Than >: To find out if one value is greater than another value, to compare them, use an operator as >. It can be used as follows:

Value1 > Value2

If the value on the left of the operator is greater than the value on the right side, the operation produces a true value. Otherwise, it produces a false result.

Greater Than Or Equal To >=: If one value is greater than or equal to another, to compare them, use an operator as >=. It can be used as follows:

Value1 >= Value2

If the value on the left side of the operator is greater than the value on the right side or if both values are equal, the comparison produces a result as true. If the value on the left is greater than the value on the right and the values are also different, the comparison produces a result as false.

Conditional Operators

As mentioned above, logical operators are used to find the relationship between two values. Here is an example that compares the values of two variables:

let number1 = 8;
let number2 = 12;
let result  = number1 = number2;

printfn "The lower value of 8 and 12 is %b" result;

This would produce:

The lower value of 8 and 12 is false
Press any key to continue . . .

To negate the result of a logical operation, you can use an operator named not. Its formula is:

not Operation

Here is an example:

let number1 = 8;
let number2 = 12;
let result  = not (number1 = number2);

printfn "The lower value of 8 and 12 is %b" result;

This would produce:

The lower value of 8 and 12 is true
Press any key to continue . . .
 
   
 

Fundamentals of Conditions

 

If a Condition is True

The above comparison operators allow you to formulate conditions to find out if a comparison produces a true or false result. To get that result, you can use the if keyword in a formula as follows:

if VariableOrValue1 Operator VariableOrValue2 then Statement

The if and the then keywords are required. VariableOrValue1 corresponds to Value1 we used in the previous formulas. VariableOrValue2 corresponds to Value2 we used in the previous formulas. Operator is one of the operators we reviewed previously. After the then keyword, you should (must) specify what to do if the comparison produces a true result. Here is an example:

let age = 12;

if age < 18 then
    printfn "The person is less than 18 years old.";

This would produce:

The person is less than 18 years old.
Press any key to continue . . .

The Body of a Conditional Statement

The section where you define what to do about the conditional statement is referred to as its body. In the body, you can just specify what to do. Alternatively, to delimit the body of the conditional statement, start it with an indented begin and end it with the end keyword indented on the same level as begin. Here is an example:

let age = 12;

if age < 18 then
    begin
	printfn "The person is less than 18 years old.";
    end

If Something Else is True

When used by itself, the if condition does not specify what to do if its comparison produces a false result. To let you take care this, you can add the else keyword to an if condition. The formula to follow is:

if VariableOrValue1 Operator VariableOrValue2 then Statement1 else Statement2

This time, the if statement is processed by Statement1. If the condition is false, add the else. Statement2 would be processed if the comparison of the Operator produces a false resust. Here is an example:

let mutable age = 12;

if age < 18 then
    printfn "The person is less than 18 years old.";
else
    printfn "This person is not younger than 18."

age <- 36;

if age < 18 then
    printfn "The person is less than 18 years old.";
else
    printfn "This person is not younger than 18."

This would produce:

The person is less than 18 years old.
This person is not younger than 18.
Press any key to continue . . .

What Else If a Condition is True

The if and else conditions we have used above provide only two options. Sometimes, more alternatives must be considered. This is done using the elif keyword. If you have two combinations of comparisons to perform, the formula to follow is:

if VariableOrValue1 Operator1 VariableOrValue2 then
    Statement1
elif VariableOrValue3 Operator2 VariableOrValue4 then
    Statement2

Here is an example:

let mutable age = 12;

if age <= 17 then
    printfn "Teenager.";
elif age >= 65 then
    printfn "Senior Citizen."

age <- 68;

if age <= 17 then
    printfn "Teenager.";
elif age >= 65 then
    printfn "Senior Citizen."

This would produce:

Teenager.
Senior Citizen.
Press any key to continue . . .

If you have many combinations of comparisons to perform, you can use and elif comparison for each condition. The formula to follow is:

if VariableOrValue1 Operator1 VariableOrValue2 then
    Statement1
elif VariableOrValue_n1 Operator_n1 VariableOrValue_n2 then
    Statement_n1
elif VariableOrValue_n3 Operator_n2 VariableOrValue_n4 then
    Statement_n2
elif VariableOrValue_n5 Operator_n3 VariableOrValue_n6 then
    Statement_n3

Here is an example:

let mutable age = 12;

if age <= 17 then
    printfn "Teenager.";
elif age < 65 then
    printfn "Adult."
elif age >= 65 then
    printfn "Senior Citizen."

age <- 36;

if age <= 17 then
    printfn "Teenager.";
elif age < 65 then
    printfn "Adult."
elif age >= 65 then
    printfn "Senior Citizen."

age <- 68;

if age <= 17 then
    printfn "Teenager.";
elif age < 65 then
    printfn "Adult."
elif age >= 65 then
    printfn "Senior Citizen."

This would produce:

Teenager.
Adult.
Senior Citizen.
Press any key to continue . . .

If none of the elif comparisons applies, you can add a last else statement. The formula to follow is:

if VariableOrValue1 Operator1 VariableOrValue2 then
    Statement1
elif VariableOrValue_n1 Operator_n1 VariableOrValue_n2 then
    Statement_n1
elif VariableOrValue_n3 Operator_n2 VariableOrValue_n4 then
    Statement_n2
elif VariableOrValue_n5 Operator_n3 VariableOrValue_n6 then
    Statement_n3
else
    Statement_X

Here is an example:

let mutable age = 14;

if age <= 17 then
    printfn "Teenager";
elif age <= 65 then
    printfn "Adult";
else
    printfn "Senior Citizen";

age <- 68;

if age <= 17 then
    printfn "Teenager";
elif age <= 65 then
    printfn "Adult";
else
    printfn "Senior Citizen";

age <- 36;

if age <= 17 then
    printfn "Teenager";
elif age <= 65 then
    printfn "Adult";
else
    printfn "Senior Citizen";

This would produce:

Teenager
Senior Citizen
Adult
Press any key to continue . . .
 
 
   
 

Previous Copyright © 2009-2015 FunctionX Next