Slide 9.1: Arithmetic expressions
Slide 9.3: An example of multiple interfaces
Home

Controlling Program Flow


Conditional Operators
To control the C# program flow, we can use various conditional operators. Basically, they resemble mathematical operators. Conditional operators let the C# program compare data values and then decide what action to take, whether to execute a program or terminate the program, etc. These operators are shown on the right:
Operator Meaning
== Equal to
> More than
< Less than
>= More than and equal
<= Less than and equal
<> Not equal to

You can also compare strings with the above operators. However, there are certain rules to follows: Upper case letters are less than lowercase letters, ‘A’<‘B’<‘C’<‘D’ ... <‘Z’ and number are less than letters.

Logical Operators
In addition to conditional operators, there are a few logical operators which offer added power to the C# programs. These operators are shown on the right:
Operator Meaning
& or && Both sides must be true
| or || One side or other must be true
^ One side or other must be true but not both
! Negates truth


Using if ... else if ... else Statements with Operators

To effectively control the C# program flow, we shall use if ... else if ... else statement together with the conditional operators and logical operators.
 if ( condition ) {
    C# statements
 }
 else if {
    C# statements
 }
 else {
    C# statements
 }

Sometime it is not necessary to use else if or else.