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;
}
}

Chapter 3: Programming Basics / C# Iteration Constructs

C# Iteration Constructs

Almost all programming languages I have worked with will have your simple for loop, foreach statments. So, this is just something to practice little on, we will not spend too much time on it:

for loop
foreach / in loop
while loop
do/while loop

We will now examine these looping construct in a new console application.

for Loop

With for loop you can iterate over a block of code with a fixed number of times. That being said, you are able to specify how many times a block of code repeats itself.

One of the short cut to get a for loop statement, without even typing any code is to right click, select "Insert Snippit", then select "for", then customize your for loop. Also instead of writing out "console.wrieline", you may type "cw" then hit tab twice.


for (int i = 0; i < 3; i++)
{

Console.Wireline("Your number is: {0} ", i);
}

foreach Loop

C#'s foreach keyword allowes you to iterate over all items within an array.


static void usingforeachloop()
{
string[] carTypes = { "Honda", "BMW", "Ford", "Mercedez" };
foreach (string a in carTypes)
Console.WriteLine(a);


int[] myIntegers = { 10, 20, 30, 40 };
foreach (int i in myIntegers)
Console.WriteLine(i);
}

While and do/While

While looping construct is useful should you wish to execute a block of statements until termination condition has been reached.


static void Whileloop()
{
string userIsFinished = "";
while (userIsFinished.ToLower() != "yes")
{
Console.Write("Are you finished? [Yes] [no]: ");
userIsFinished = Console.ReadLine();
Console.WriteLine("In while loop");
}

Now the difference between while, and do/while loop is that, do/while loop are guarteen to execute atleast one, and incontrast while loop may not execute if the termination condition is false.

do/while loop


static void doWhileloop()
{
string userIsFinished = "";
do
{
Console.WriteLine("In do/while loop");
Console.WriteLine("Are you done? [yes] [No]: ");
userIsFinished = Console.ReadLine();
}
while
(userIsFinished.ToLower() != "yes");
}