Introduction
This article has been excerpted from the book "Visual C# Programmer's Guide".
Control statements give you additional means to control the processing within the applications you develop. This section explores the syntax and function of the if, switch, do-while, for, foreach, goto, break, continue, and return statements.
If-then-else
The if statement has three forms: single selection, if-then-else selection, and multicase selection. The below code contains an example of each form.
Example 1
//single selection
if (i > 0)
Console.WriteLine("The number {0} is positive", i);
//if-then-else selection
if (i > 0)
Console.WriteLine("The number {0} is positive", i);
else
Console.WriteLine("The number {0} is not positive", i);
//multicase selection
if (i == 0)
Console.WriteLine("The number is zero");
else if (i > 0)
Console.WriteLine("The number {0} is positive", i);
else
Console.WriteLine("The number {0} is negative", i);
The variable i is the object of evaluation here. The expression in an if statement must resolve to a boolean value type.
// Compiler Error
if (1)
Console.WriteLine("The if statement executed");
Console.ReadLine();
When the C# compiler compiles the preceding code, it generates the error "Constant value 1 cannot be converted to bool."
The code below shows how conditional or (||) and conditional and (&&) operators are used in the same manner.
Example 2
//Leap year
int year = 1974;
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
Console.WriteLine("The year {0} is leap year ", year);
else
Console.WriteLine("The year {0} is not leap year ", year);
Switch
From the example in the above code, you can see that the switch statement is similar to an if-else if-else if-else form of an if statement.
Example
string day = "Monday";
Console.WriteLine("enter the day :");
day = Console.ReadLine();
switch (day)
{
case "Mon":
break;
case "Monday":
Console.WriteLine("day is Monday: go to work");
break;
default:
Console.WriteLine("default");
break;
}
switch (strVal1)
{
case "reason1":
goto case "reason2"; // this is a jump to mimic fall-through
case "reason2":
intOption = 2;
break;
case "reason 3":
intOption = 3;
break;
case "reason 4":
intOption = 4;
break;
case "reason 5":
intOption = 5;
break;
default:
intOption = 9;
break;
}
Do-While
The while loop allows the user to repeat a section of code until a guard condition is met. The below code presents a simple while loop designed to find out the number of digits in a given value.
While Example
//find out the number of digits in a given number
int i = 123;
int count = 0;
int n = i;
//while loop may execute zero times
while (i > 0)
{
++count;
i = i / 10;
}
Console.WriteLine("Number {0} contains {1} digits.", n, count);
For a given number i = 123, the loop will execute three times. Hence the value of the count is three at the end of the while loop.
This example has one logical flaw. If the value of i is 0, the output of the code will be "Number 0 contains 0 digits." Actually, the number 0 contains one digit. Because the condition of the while loop i> 0 is false from the beginning for the value i = 0, the while loop does not even execute one time, and the count will be zero. The code below presents a solution.
Do Example


Mahendra Singh BishtPosted Dec 29, 2010, 10:50 AM
Can You suggest me the Comparison between While & Do While, While & For, Break & Continue, For & For Each Statements......