Conditionals
What are Conditionals?
Conditionals are fundamental in OneStream development for controlling the flow of a business rule based on a specific condition. They allow the business rule to make decisions and execute different blocks of code based on that conditional evaluation.
if
Statement
The if
statement is used to execute a block of code if a specified condition evaluates to true. The syntax is as follows:
- C#
- VB.NET
if (condition)
{
// Code to execute if condition is true
}
If condition Then
' Code to execute if condition is true
End If
Example:
- C#
- VB.NET
int age = 20;
if (age >= 18)
{
Console.WriteLine("You are an adult.");
}
Dim age As Integer = 20
If age >= 18 Then
Console.WriteLine("You are an adult.")
End If
if-else
Statement
The if-else
statement allows you to execute one block of code if the condition is true and another block if it is false. The syntax is as follows:
- C#
- VB.NET
if (condition)
{
// Code to execute if condition is true
}
else
{
// Code to execute if condition is false
}
If condition Then
' Code to execute if condition is true
Else
' Code to execute if condition is false
End If
Example:
- C#
- VB.NET
int age = 16;
if (age >= 18)
{
Console.WriteLine("You are an adult.");
}
else
{
Console.WriteLine("You are not an adult.");
}
Dim age As Integer = 16
If age >= 18 Then
Console.WriteLine("You are an adult.")
Else
Console.WriteLine("You are not an adult.")
End If
else if
Ladder
The else if
ladder allows you to check multiple conditions sequentially. If the first condition is false, it checks the next condition, and so on. The syntax is as follows:
- C#
- VB.NET
if (condition1)
{
// Code to execute if condition1 is true
}
else if (condition2)
{
// Code to execute if condition2 is true
}
else
{
// Code to execute if none of the conditions are true
}
If condition1 Then
' Code to execute if condition1 is true
ElseIf condition2 Then
' Code to execute if condition2 is true
Else
' Code to execute if none of the conditions are true
End If
Example:
- C#
- VB.NET
int age = 20;
if (age < 13)
{
Console.WriteLine("You are a child.");
}
else if (age < 18)
{
Console.WriteLine("You are a teenager.");
}
else
{
Console.WriteLine("You are an adult.");
}
Dim age As Integer = 20
If age < 13 Then
Console.WriteLine("You are a child.")
ElseIf age < 18 Then
Console.WriteLine("You are a teenager.")
Else
Console.WriteLine("You are an adult.")
End If
switch
/ Select Case
Statement
The switch
statement (C#) or Select Case
statement (VB.NET) allows you to execute different blocks of code based on the value of a variable. This is useful when you have multiple conditions to check against a single variable. The syntax is as follows:
- C#
- VB.NET
switch (variable)
{
case value1:
// Code to execute if variable equals value1
break;
case value2:
// Code to execute if variable equals value2
break;
default:
// Code to execute if variable does not match any case
break;
}
Select Case variable
Case value1
' Code to execute if variable equals value1
Case value2
' Code to execute if variable equals value2
Case Else
' Code to execute if variable does not match any case
End Select
Example:
- C#
- VB.NET
string dayOfWeek = "Monday";
switch (dayOfWeek)
{
case "Monday":
Console.WriteLine("It's Monday!");
break;
case "Friday":
Console.WriteLine("It's Friday!");
break;
default:
Console.WriteLine("It's another day.");
break;
}
Dim dayOfWeek As String = "Monday"
Select Case dayOfWeek
Case "Monday"
Console.WriteLine("It's Monday!")
Case "Friday"
Console.WriteLine("It's Friday!")
Case Else
Console.WriteLine("It's another day.")
End Select
Logical Operators in Conditionals
Logical operators can be used to combine multiple conditions in a single conditional statement. The most common logical operators are AND
, OR
, and NOT
.
Operator | Description | Evaluates | C# Example | VB.Net Example |
---|---|---|---|---|
&&, And, AndAlso | Logical And | True if both conditions are true | if (a && b) | If a AndAlso b |
||, Or, OrElse | Logical Or | True if at least one codition is true | if (a || b) | If a OrElse b |
!, Not | Condition reversal | Reverses the condition | if !(a == b) | If NOT a = b |
Example:
- C#
- VB.NET
int age = 20;
bool hasLicense = true;
if (age >= 18 && hasLicense)
{
Console.WriteLine("You can drive.");
}
else
{
Console.WriteLine("You cannot drive.");
}
Dim age As Integer = 20
Dim hasLicense As Boolean = True
If age >= 18 AndAlso hasLicense Then
Console.WriteLine("You can drive.")
Else
Console.WriteLine("You cannot drive.")
End If
Conditional Operators: Ternary (C# only) If (VB.NET Only)
The ternary operator is a shorthand method for the if..else
statement, i.e.
variable = (condition) ? valueIfTrue : valueIfFalse;
Example:
int age = 20;
string result = (age >= 18) ? "You are an adult." : "You are not an adult.";
Console.WriteLine(result);
If
Operator (VB.NET Only)
The If Operator is again short hand for the If...Else condition. This was formally IIF function, but the If operator was introduced in VB.Net 2008 for it’s performance gains. For instance:
variable = If(condition, valueIfTrue, valueIfFalse)
Example:
Dim age As Integer = 20
Dim result As String = If(age >= 18, "You are an adult.", "You are not an adult.")
Console.WriteLine(result)
Best Practices for Conditionals
- Keep conditions readable – Use descriptive variable names and key the conditions self-explanatory
- Avoid deeply nested conditionals – The more layers that are used in a condition, the harder it is to read.
- Use short-circuiting operators (
&&
,||
in C#;AndAlso
,OrElse
in VB.Net) - improves performance when stopping the evaluation when the result is determined as opposed to evaluating both conditions before determining a result.