Skip to main content

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:

if (condition)
{
// Code to execute if condition is true
}

Example:

int age = 20;
if (age >= 18)
{
Console.WriteLine("You are an adult.");
}

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:

if (condition)
{
// Code to execute if condition is true
}
else
{
// Code to execute if condition is false
}

Example:

int age = 16;
if (age >= 18)
{
Console.WriteLine("You are an adult.");
}
else
{
Console.WriteLine("You are not an adult.");
}

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:

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
}

Example:

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.");
}

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:

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;
}

Example:

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;
}

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.

OperatorDescriptionEvaluatesC# ExampleVB.Net Example
&&, And, AndAlsoLogical AndTrue if both conditions are trueif (a && b)If a AndAlso b
||, Or, OrElseLogical OrTrue if at least one codition is trueif (a || b)If a OrElse b
!, NotCondition reversalReverses the conditionif !(a == b)If NOT a = b

Example:

int age = 20;
bool hasLicense = true;

if (age >= 18 && hasLicense)
{
Console.WriteLine("You can drive.");
}
else
{
Console.WriteLine("You cannot drive.");
}

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.

Was this page helpful?