Introduction to C# - slide 30 : 43 |
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
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); } }
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
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 } }