Visual C# Guide
Nested Ifs

The block part of an If statement can itself contain another If statement. An 'if inside an if' if you will. This is called a nested if since there is an if statement 'nested' inside another. The following program is a reworking of the grade program from the previous page. This time the program executes less code if the exam is not passed.

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

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

The indentation which the C# editor will automatically apply is essential. Not only is it good practice and required for the examination but it makes the code far easier to see. It is easier to match up the curly braces with their partners as well as see which statement is inside which statement.

Having A Go

  1. Allow the user to input a single character at the keyboard. You will need to work out how to convert the input from string to the char type. Use the IsLetter(), IsLower() and IsDigit() methods of the Char class to tell the user whether they entered a letter, number or other character and, if a letter, whether the letter was uppercase or lowercase.