Lecture overview -- Keyboard shortcut: 'u'  Previous page: Expressions and Operators -- Keyboard shortcut: 'p'  Next page: Control structures for Selection -- Keyboard shortcut: 'n'  Lecture notes - all slides and notes together  slide -- Keyboard shortcut: 't'  Textbook -- Keyboard shortcut: 'v'  Help page about these notes  Alphabetic index  Course home  Page 30 : 43
Object-oriented Programming in C#
Introduction to C#
Control Structures for Selection

Module IfDemo

    Sub Main()
      Dim i as Integer, res as Integer
      i = Cint(InputBox("Type a number"))

      If i < 0 Then
        res = -1
        Console.WriteLine("i is negative")
      Elseif i = 0
        res = 0
        Console.WriteLine("i is zero")
      Else 
        res = 1
        Console.WriteLine("i is positive")
      End If
      Console.WriteLine(res)

    End Sub

End Module

A Visual Basic Program with an If Then Else control structure.

using System;

class IfDemo{

  public static void Main(){
    int i, res;
    i = Int32.Parse(Console.ReadLine());

    if (i < 0){
      res = -1;
      Console.WriteLine("i is negative");
    } 
    else if (i == 0) {
      res = 0;
      Console.WriteLine("i is zero");
    } 
    else { 
       res = 1;
       Console.WriteLine("i is positive");       
    }
    Console.WriteLine(res);
  }

}

The similar C# program with an if else control structure.

Module IfDemo

    Sub Main()
      Dim month as Integer = 2
      Dim numberOfDays as Integer

      Select Case month
        Case 1, 3, 5, 7, 8, 10, 12 
          numberOfDays = 31
        Case 4, 6, 9, 11
          numberOfDays = 30
        Case 2
          numberOfDays = 28             ' or 29
        Case Else 
          Throw New Exception("Problems")
      End Select

      Console.WriteLine(numberOfDays)  ' prints 28
    End Sub
End Module

A Visual Basic Program with a Select control structure.

using System;

class CaseDemo{

  public static void Main(){
    int month = 2,
        numberOfDays;

    switch(month){
      case 1: case 3: case 5: case 7: case 8: case 10: case 12:
        numberOfDays = 31; break;
      case 4: case 6: case 9: case 11:
        numberOfDays = 30; break;        
      case 2: 
        numberOfDays = 28; break;     // or 29
      default: 
        throw new Exception("Problems");
    }

    Console.WriteLine(numberOfDays);  // prints 28
  }

}

The similar C# program with a switch control structure.