KamranCSharp - whereIstand.com

Monday, December 1, 2008

Chapter 3: Conditional & Switch Statements

The if & else if statement

Conditional statements allows us to test to see if a specific condition is met.

static void Main(string[] args)
{
Console.WriteLine("Type is a string");
string input;
input = Console.ReadLine();
if (input == "")
{
Console.WriteLine("You typed an empty string");
}
else if (input.Length < 5)
{
Console.WriteLine("The string had less then 5 characters");
}
else if (input.Length < 10)
{
Console.WriteLine("The string has at lease 5 but less" + "then 10 characters");
}
Console.WriteLine("The string was " + input);
}

Now, there is absolutely no limit as to how many times you have to use " else if" that is entirely upto you. This exam is as simple as possible, so now lets move to something more fun...

The Switch statement

Another form of selection statement is the wwitch statment. It allows you to handle the program flow. When using this also note that case values are constant, variables are not permitted. Follow the example below on the console application to get more of handon to better understand. It's simple as self explanatory.


class Program
{
static void Main(string[] args)
{
Console.WriteLine("1 [C#], 2 {VB.NET]");
Console.Write("Pick your choice: ");
string choice = Console.ReadLine();
int n = int.Parse(choice);
switch (n)
{
case 1:
Console.WriteLine("C# is a great powerfull language");
break;
case 2:
Console.WriteLine("VB.NET is a really forgiving language");
break;
}
}

0 comments:

Technology News