Skip to content

Conditional Statements

if

Description

When used alone, the if statement identifies whether a statement should be ran or not. It calculates the decision based on the result of a boolean expression:

  • If the boolean expression is true, then the statement will be executed
  • If the boolean expression is false, then the statement will be ignored, allowing the Application to move to the next statement

Syntax

An if statement can be written as:

if(condition){
    then_execute_this;
}

//alternatively, without parentheses

if condition {
    then_execute_this;
}

Example

int number = 5;
if(number == 5){
    DebugLib.Logger.WriteInfoLine("The number is 5.");
}

if(number != 5){
    DebugLib.Logger.WriteInfoLine("The number is not 5.");
}

if(number != 10){
    DebugLib.Logger.WriteInfoLine("The number is not 10.");
}

Domain.Bunny bunny = Domain.Bunny.GetAll().First();
if bunny == null {
    DebugLib.Logger.WriteInfoLine("No bunnies found..");
}

/*
Output:
    The number is 5.
    The number is not 10.
    No bunnies found..
*/

if-else

Description

The if statement, followed by an else, identifies which one of two statements* should be ran.

It calculates the decision based on the result of a boolean expression:

  • If the boolean expresson is true, then the statement of the if will be executed
  • If the boolean expresson is false, then the statement of the else will be executed

Syntax

An if-else statement can be written as:

if(condition){
    then_execute_this;
}
else{
    then_execute_that;
}

//alternatively, without parentheses

if condition {
    then_execute_this;
}
else{
    then_execute_that;
}

Example

    int number = 5;
    if(number == 5){
        DebugLib.Logger.WriteInfoLine("The number is 5.");
    }
    else{
        DebugLib.Logger.WriteInfoLine("The number is not 5.");
    }

    if number > 10 {
        DebugLib.Logger.WriteInfoLine("The number is greater than 10.");
    }
    else{
        DebugLib.Logger.WriteInfoLine("The number is less than 10.");
    }

    /*
    Output:
        The number is 5.
        The number is less than 10.
    */

if-elseif

Description

The if-elseif statement identifies which statement should be ran, based on the evaluation of a boolean expression.

The evaluations follow a top-down approach, calculating each Condition until reaching one that evaluates as true. The statement of the true-evaluated condition will be the one to be executed. If no statement is evaluated as true, then

  • If an else exists, its statement will be ran
  • Otherwise, the Application will continue to the next block

Syntax

An if-else statement can be written as:

if(condition1){
    then_execute_this_1;
}
elseif (condition2){
    then_execute_this_2;
}
else{ //optional else
    then_execute_default;
}

//alternatively, without parentheses

if condition1 {
    then_execute_this_1;
}
elseif  condition2 {
    then_execute_this_2;
}
else{ //optional else
    then_execute_default;
}

Example

    int number = 2; 

    if(number == 1){
        DebugLib.Logger.WriteInfoLine("The number is 1");
    }
    elseif (number == 2){
        DebugLib.Logger.WriteInfoLine("The number is 2");
    }

    if number > 0 {
        DebugLib.Logger.WriteInfoLine("It's a positive number");
    }
    elseif number < 0 {
        DebugLib.Logger.WriteInfoLine("It's a negative number");
    }
    else {
        DebugLib.Logger.WriteInfoLine("It's a zero");
    }

    /*
    Output: 
        The number is 2
        It's a positive number
    */

switch

Description

The switch statement performs the exact same logic as the if-elseif statement, described in the previous section. This statement loops through all the possible cases, comparing an identifier to a value, until it finds one that is true (or defaults to the default statement, if present).

Important: The identifier of a switch can be a number, a string, a char or a bool. Other types will throw a Validation error.

It is most commonly used when there are 3 or more conditions to be evaluated, making the code that much readable.

Warning

Unlike other languages (eg. C++, C#, Java)

  • You cannot use {} brackets for the statements of each condition
  • You can omit the default statement, if your logic does not have an else optional
  • You can omit the break statement. Mamba knows when it needs to break, by itself

Hint

  • You can omit the default statement, if your logic does not have an else optional
  • You can omit the break statement. Mamba knows when it needs to break, by itself

Syntax

A switch statement can be written as:

switch _identifier {
    case firstCondition:
        firstStatement;
    case _secondCondition:
        secondStatement;
    default: //optional
        defaultStatement;
}

which is evaluated exactly as

if(_identifier == firstCondition){
    firstStatement;
}
elseif(_identifier == secondCondition){
    secondStatement;
}
else{ //optional
    defaultStatement;
}

Example

string queenMemberName = "Freddie";
string queenMemberSurname = "";
string playsThe = "";

switch queenMemberName {
    case "Freddie":
        queenMemberSurname = "Mercury";
        playsThe = "piano";
    case "Brian":
        queenMemberSurname = "May";
        playsThe = "guitar";
    case "John":
        queenMemberSurname = "Deacon";
        playsThe = "bass";
    case "Roger":
        queenMemberSurname = "Taylor";
        playsThe = "drums";
    default:
        ShowMessage("Unknown name: " + queenMemberName);
}

DebugLib.Logger.WriteInfoLine(queenMemberName + " " + queenMemberSurname);

/*
Output:
    Freddie Mercury
*/

Conditional Operator ?:

Description

The ternary conditional operator can be used as an alternative if-else statement. It evaluates a condition and, depending on the outcome, returns one of two specified values.

Syntax

condition ? then_expression : else_expression; 

which is evaluated exactly as

if(condition){
    then_expression;
}
else{ 
    else_expression;
}

The condition expression can be any expression or statement that returns a bool (true or false). The then_expression is the value to be returned if the condition evaluates as true. The else_expression is the value to be returned if the condition evaluates as false. Important: The then_expression and else_expression must have the same type, or be implicitly convertible.

Tip

You can use as many then_expression and else_expression you'd like, as long as you enclose them in parentheses.

Example

function void Validate(string email)
{
    string message = "";

    message = string.IsNullOrWhiteSpace(email) ? "Please, provide an E-Mail" : "Thanx!";
    /*
        The above can be translated to:
        - Is the email null or a whitespace?
        - If yes, then return "Please, provide an E-Mail" as the value for the 'message'
        - Otherwise, return "Thanx!" as the value for the 'message'
    */
}
//Multiple ternary conditionals
int number = 5;
string message;

message = number < 0 ? "Provide a positive number" : (number > 10 ? "Provide a number less than 10" : "Correct number, thanx!");
ShowMessage(message);
/*
    Which translates to: 
    - Is the number negative?
    - If yes, message = "Provide a positive number"
    - If not, then
        - Is the number larger than 10?
        - If yes, message = "Provide a number less than 10"
        - Of not, message = "Correct number, thanx!"

    Outputs: Correct number, thanx!
*/