Visual Basic 2010 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.

Dim grade As String
Dim score As Integer
Console.Write("Enter the score: ")
score = Console.ReadLine()
If score >= 55 Then
   If score >= 75 Then
      grade = "A"
   ElseIf score >= 65 Then
      grade = "B"
   Else
      grade = "C"
   End If
Else
   grade = "U"
End If
Console.WriteLine("Your grade for the examination is {0} ", grade)
Console.ReadLine()

The indentation which the code 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 read.

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.