[edit] C# IF Statements
The C# if/else statement makes decisions. An if statement can only evaluate to two results: true or false. Depending upon the result of the of the condition the if block will execute or be ignored. Lets take a look at an example:
C# Pseudocode
- If Persons Age is greater than 75
- Print "Nice retirement"
The statement above determines if the condition Age is greater than 75. If the condition is true then it will print Nice retirement. If the condition is false then the statement is ignored then the program continues on the next piece of code.
The example above can be written in pure C# code like this:
if(age > 75)
Console.WriteLine("Have a nice retirement");
The C# code directly relates to the pseudocode. Again, if age is greater than 75 the code will print "Have a nice retirement". If the condition is false then the statement will be ignored and it continues to the next part of the code.
Example
using System; class Program { static void Main(string[] args) { int age = 31; if(age > 75) Console.WriteLine("Have a nice retirement"); Console.Read(); } }
The variable age is 31. When the program reaches line 9 it will determine if age > 75 is true or false. In this case the condition evaluates to false. So, line 10 will not execute. "Have a nice retirement" will not print because line 9 evaluates to false.
Here is a table of all the allowable comparison operators for c#
C# Comparison Operators
|
Operator |
Name |
Description |
|
< |
Less than |
Determines if left side is less than right side |
|
> |
Greater than |
Determines if right side is greater than left side |
|
<= |
Less than or equal to |
Determines if left side is less than or equal to right side |
|
>= |
Greater than or equal to |
Determines if left side is greater than or equal to right side |
|
== |
Equal to |
Determines if left side is equal to right side |
|
!= |
Not equal to |
Determines if left side is not equal to right side |
[edit] C# if/else Statements
The if statement is executed when the condition is true. But what if you want the program to perform a certain task if the condition is false. The else statement comes to the rescue. Lets take the following pseudocode as a guide:
- If age is greater than 75
- Print "Have a nice retirement"
- else
- Print "You are still young"
The pseudocode prints "Have a nice retirement" if the condition evaluates to true. If the condition evaluates to false then the program prints "You are still young". So if the if statement evaluates to true then the else statement is not executed.
Here is a code snippet that relates to the pseudocode:
if (age > 75)
Console.WriteLine("Have a nice retirement");
else
Console.WriteLine("You are still young");
C# code example:
using System;
class Program
{
static void Main(string[] args)
{
int age = 31;
if (age > 75)
Console.WriteLine("Have a nice retirement");
else
Console.WriteLine("You are still young");
Console.Read();
}
}
Output:
You are still young
As you can see if age is greater than 75 it will print "Have a nice retirement", otherwise it will print "You are still young".
In our next section we will discuss nested if/else statements.
[edit] C# Nested if/else Statements
Example code:
using System;
class Program
{
static void Main(string[] args)
{
char grade = 'b';
if (grade == 'a')
Console.WriteLine("Outstanding student");
else if (grade == 'b')
Console.WriteLine("Good student");
else if (grade == 'c')
Console.WriteLine("Average student");
else if (grade == 'd')
Console.WriteLine("Need a little more practice");
else
Console.WriteLine("Need alot of practice");
Console.Read();
}
}
- Use two = "==" signs when comparing variable or literals.
- Only one if/else if block is executed. The others are skipped.
[edit] C# Ternary Operator
- The ternary operator looks like this: ?:
Ternary operator has one condition expression and can return one of possible two values. Lets take a look at an example:
using System; namespace ConsoleApplication3 { class Program { static void Main(string[] args) { int a = 1; string b = (a < 2) ? "true" : "false"; Console.WriteLine(b); Console.Read(); } } }
Line 10: Since (a < 2) evaluates to true, true is printed. If (a < 2) evaluated to false then false would be the result.
|