Else Statements

<< Click to Display Table of Contents >>

Navigation:  Concordance Programming Fundamentals > Using Conditional Statements and Loops >

Else Statements

An else-statement is an addition you can place on an if-statement to branch your decision: that is, to execute one set of code if your conditional value is true, and another if the value is false. An else-statement is constructed using the following syntax:

if (conditionalValue) 

 ExecuteThisCode();

}

else

{

 ExecuteSomeOtherCode();

}

The code in the if-statement is executed if the conditional value is true. The code contained within the else-statement is executed if the conditional value is false.

For example, you may wish to execute different one set of code if a target is at least 21, and another if they are less than 21. You could create two different if-statements, as shown in the following example:

if (age >= 21) 

{

   ExecuteCodeForAdultCustomers();
}

 

if (age < 21) 

{

   ExecuteCodeForJuvenileCustomers();
}

Alternately, you could use an else-statement, instead.

if (age >= 21) 

{

   ExecuteCodeForAdultCustomers();
}

else

{

   ExecuteCodeForJuvenileCustomers();
}