Conditional Operators

<< Click to Display Table of Contents >>

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

Conditional Operators

The first step in constructing an if-statement is a conditional. A conditional can be whether something equals something else, whether something is greater than something else, and so on. The operators you use to perform a comparison are very similar to the ones used in mathematics.

Operator

Description

==

Is equal (note the double equals sign)

<>

Does not equal

<=

less than or equal

<

Less than

>=

Greater than or equal

>

Greater than

Note: do not confuse the double equals sign with the single equals sign. Use the double equals sign (==) when comparing a value. Use the single equal sign (=) when assigning a value.

The following example checks if the variable age contains a value greater than or equal to 21.

if (age >= 21)

{

  //do something with code

}

The following example check if the companyName variable contains "CloudNine".

if (companyName == "CloudNine")

{

  //do something with code

}

Note the use of double quotes to encapsulate a piece of text (called a string).

Finally, CPL allows you to use numerical values as a trigger for a conditional statement, as shown in the following table

Value

Description

0

considered to be FALSE when used as a condition.

All other values (positive and negative)

Considered to be TRUE when used as a condition.

This allows you to use variables and function return values as conditional operators. For example, you may see code similar to the following:

if (someFunction())

{

   doStuff();

}

This code states "if the return value of someFunction() is non-zero, execute the following code." This allows you to create functions that check for information and perform some task; if that task was successful, you can use a conditional statement to act on it.