Compound If-Statements

<< Click to Display Table of Contents >>

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

Compound If-Statements

A compound if-statement occurs when two or more conditions must exist before you want to execute your code. For example,you may wish to know if a person was between the ages of 18 and 21 (inclusive). Perhaps you want to target that age group for some marketing campaign. You could write a nested if-statement that looks like the following:

if (age >= 18) 

{

   if (age <= 21) 

      {

    SendMarketingMaterials();

 }

}

Alternately, you could write both statements in compound if-statement. Compound if-statements use the keywords, and and or, to combine conditional statements. For example, the previous nested if-statement is equivalent to the following compound if-statement:

if ((age >= 18) and (age <= 21)) 

{

 SendMarketingMaterials();

}

Both conditional statements must hold true for the function, SendMarketingMaterials(), to execute. Note the use of parentheses. As good practice, make sure all your conditional statements in your compound if-statements are enclosed within parentheses.