A few lessons so far dealt with conditional processing, displaying a result when a condition is true, and another when it is false. When working with conditional processing, it is important to understand the representation of true and false values in C#.
C# checks by default a conditional statement if it returns a true value. Many inexperienced programmers write their test conditions as follows:
|
1
|
if (expression != false)
// check if the expression is not false
|
or like this:
|
1
|
if (expression == true)
// check if the expression is true
|
Since the program checks the default value as being true, you can write you test conditions by simply placing the condition in parenthesis:
|
1
|
if (expression)
|
When the expression is evaluated as a value different than false, C# executes the instructions that follows immediately after the condition. When it is evaluated as false, C# will simply ignore the instructions that follow after the condition.
The concepts explained in this lesson are also shown visually as part of the following video: