Visual C# 2005 Guide
If...Else

Simple If

This is our first example of a control statement. The If statement allows you to specify conditions under which some lines of your code will be run. Copy and run the following program,

int score;
Console.Write("Enter the score: ");
score = System.Convert.ToInt32(Console.ReadLine());
if (score > 60) score += 10;
Console.WriteLine("Total score is {0}", score);
Console.ReadLine();

Study the line with the If statement carefully. When there is only one line of code to run if the statement is true, it can be written in a single line as shown above.

Simple Block If

It is more likely that you will want to execute several lines of code with your if statements. In such cases, you should write your statement as shown in the next sample. Overtime is paid at double time for hours worked over 40.

int overtime=0;
double pay;
Console.Write("Enter the number of hours worked: ");
int hours = System.Convert.ToInt32(Console.ReadLine());
pay = hours * 3.75;
if (hours > 40)
{
   overtime = hours - 40;
   pay += overtime * 3.75;
}

Console.WriteLine("Total pay is £{0}",pay);
Console.ReadLine();

Notice the use of curly braces to enclose the code that is run if the condition is true.

If...Else

We will sometimes want to be able to execute one line of code if a condition is true and something else if it is not. In the following example we determine whether someone has passed or failed an examination.

Console.Write("Enter the score: ");
int score = System.Convert.ToInt32(Console.ReadLine());
if (score > 40)
{
   Console.WriteLine("You have passed the exam.");
}
else
{
   Console.WriteLine("You have failed the exam.");
}

Console.ReadLine();

The keyword, else is used to introduce the code to run when the condition is not met.

If..Else If...Else

We can also use the If statement to check a series of conditions. The following program is used to calculate a grade from an exam score.

string grade;
Console.Write("Enter the score: ");
int score = System.Convert.ToInt32(Console.ReadLine());
if (score >= 75)
{
   grade = "A";
}
else if (score >= 65)
{
   grade = "B";
}
else if (score >= 55)
{
   grade = "C";
}
else
{
   grade = "U";
}

Console.WriteLine("Your grade for the examination is {0} ", grade);
Console.ReadLine();

Consider here that, if you enter an A grade score, it will be greater than 65 and 55 as well as being greater or equal to 75. The first condition that is met will be executed. Notice also that we can use the else statement to add a clause to cover the situation when none of the other conditions are met.

Having A Go

  1. Write a program that reads an employee's number, their hourly rate and the number of hours worked in that week. Hours over 40 are paid at time and a half. Calculate and display the wages due to this person.
  2. Write a program that reads two numbers and examines the relationship between them. Output one of the following messages,

    A is greater than B
    B is greater than A
    A and B are equal
  3. Write a program to find the two roots of the quadratic equation ax2 + bx + c = 0 using the formula,
    quadratic formula
    The user inputs values for a, b and c. If b2 - 4ac is negative, then the equation has no roots. If b2 - 4ac=0, the equation has only one root.