Introduction
A program based purely on sequential execution will, during each execution, always perform exactly the same actions; it is unable to react in response to current conditions.
The flow of control is the order in which statements of a program are executed.Other terms used are ordering and control flow.Group of statements used for the exact purpose , collectively called branching statements.A branch is a segment of a program made up of one statement or a group of statements.
A branching statement chooses to execute just one of several statements or blocks of statements .The choice made depends on a given condition.Branching statements are also called selection statements or alteration statements.
An iteration statement repeats a statement or a block of statements until a given termination condition is met.Iteration statements are also referred to as loop statements or repetition statements.
Branching With The IF Statement
The value of relational operators is that they can be used to make decisions to change the flow of the execution of your program. The if keyword can be used with the relational operators to change the program flow.
The if-else keyword is used to compare two values. The standard format of the if command is as follows:
if( val1 [operator] val2) statement(s) else statement(s);
operator is one of the relational operators; val1 and val2 are variables, constants, or liter- als; and statement(s) is a single statement or a block containing multiple statements. Remember that a block is one or more statements between brackets.
If the comparison of val1 to val2 is true, the statements are executed. If the comparison of val1 to val2 is false, the statements in the fast loop are executed.
EXAMPLE :
// ifelsetest.cs- The if statement
//----------------------------------------------------
class ifelsetest
{
public static void Main()
{
int Val1 = 1;
int Val2 = 0;
System.Console.WriteLine(“Getting ready to do the if...”);
if (Val1 == Val2)
{
System.Console.WriteLine(“If condition was true”);
}
else
{
System.Console.WriteLine("If condition was false");
}
System.Console.WriteLine(“Done with the if statement”);
}
}
OUTPUT :
Getting ready to do the if...
Done with the if statement
THE SWITCH STATEMENT :
C# provides a much easier way to modify program flow based on multiple values stored
in a variable: the switch statement. The format of the switch statement is as follows:
switch ( value )
{
case result_1 :
// do stuff for result_1
break;
case result_2 :
// do stuff for result_2
break;
... case result_n :
// do stuff for result_x
break;
default:
// do stuff for default case
break;
}
The value in the switch statement can be the result of an expression, or it can be a variable. This value is then compared to each of the values in each of the case statements until a match is found. If a match is not found, the flow goes to the default case. If there is not a default case, flow goes to the first statement following the switch statement.
When a match is found, the code within the matching case statement is executed. When the flow reaches another case statement, the switch statement ends. Only one case statement is executed at most. Flow then continues, with the first command following the switch statement.
Example :
// roll.cs- Using the switch statement.
//--------------------------------------------------------------------
class roll
{
public static void Main()
{
int roll = 0;
// The next two lines set the roll to a random number from 1 to 6
System.Random rnd = new System.Random();
roll = (int) rnd.Next(1,7);
System.Console.WriteLine(“Starting the switch... “);
switch (roll)
{
case 1:
System.Console.WriteLine(“Roll is 1”);
break;
case 2:
System.Console.WriteLine(“Roll is 2”);
break;
case 3:
System.Console.WriteLine(“Roll is 3”);
break;
case 4:
System.Console.WriteLine(“Roll is 4”);
break;
case 5:
System.Console.WriteLine(“Roll is 5”);
break;
case 6:
System.Console.WriteLine(“Roll is 6”);
break;
default:
System.Console.WriteLine(“Roll is not 1 through 6”);
break;
}
System.Console.WriteLine(“The switch statement is now over!”);
}
}
OUTPUT :
Starting the switch...
Roll is 1
The switch statement is now over!
NOTE : Your answer for the roll in the output might be a number other than 1.
EXECUTING MORE THAN ONE CASE STATEMENT :
To do this in C#, goto command is used. The goto command can be used within the switch statement to go to either a case statement or the default command. The following code snippet shows the switch statement from the previous section executed with goto statements instead of simply dropping through:
switch (roll)
{
case 1:
goto case 4;
break;
case 2:
goto case 5;
break;
case 3:
goto case 4;
break;
case 4:
System.Console.WriteLine(“Roll is Odd”);
break;
case 5:
System.Console.WriteLine(“Roll is Even”);
break;
default:
System.Console.WriteLine(“Roll is not 1 through 6”);
break;
}
ITERATION STATEMENTS :
An Iteration statement repeats a statement or a block of statements until a given termination condition is met.
C# provides a number of iteration statements.The iteration statements in C# are listed here:
- while
- do
- for
- foreach
The while loop repeats a statement or compound statement again and again, here referred as loop body, as long as its associate loop condition, consisting of a Boolean expression, is true. When the loop conditions is false while being evaluated, flow of control moves to the line immediately following the while loop.
Syntax :
while ( condition )
{
Statement(s);
}
Example :
class WhileTest
{
public static void Main()
{
int n = 1;
while (n < 5)
{
Console.WriteLine("Current value of n is {0}", n);
n++;
} }}
OUTPUT :
Current value of n is 1 Current value of n is 2 Current value of n is 3 Current value of n is 4 THE DO-WHILE LOOP STATEMENT : The do-while loop is analogous to the while loop in that the loop body is repeatedly executed over and over again as long as the Boolean expression is True.One important difference is that the loop body is located between the keywords do and while is executed before the Boolean expression is evaluated. Consequently, the loop body of the do-while loop is always executed at least once, even if the Boolean expression is false initially. Example : class Do-WhileTest { public static void Main() { int n = 1; do { Console.WriteLine("Current value of n is {0}", n); n++; }while (n < 5) } } OUTPUT : Current value of n is 1 Current value of n is 2 Current value of n is 3 Current value of n is 4 THE FOR LOOP STATEMENT : The three key components of a loop are as follows
- Loop Condition - When evaluated to true, will cause the loop body to be repeated.
- Loop Initialization - During the loop initialization the variable(s) taking part in the loop condition are assigned initial suitable values.This process only takes place once before the loop commences.
- Loop Update - Updates the variables of the loop condition. This is repeatedly done during every loop.
Example :
class For
{
public static void Main()
{
int n = 1;
for(n=1;n<5;n++)
{
Console.WriteLine("Current value of n is {0}", n); }
Console.ReadLine(); }
}
OUTPUT :
Current value of n is 1
Current value of n is 2
Current value of n is 3
Current value of n is 4
THE FOREACH LOOP STATEMENT :
The foreach loop in C# executes a block of code on each element in an array or a collection of items. When executing foreach loop it traverse items
in a collection or an array. The foreach loop is used for traversing each items in an array or a collection of items and displayed one by one.
Example :
using System;
class Program
{
static void Main()
{
// Use a string array to loop over.
string[] ferns = { "Psilotopsida", "Equisetopsida", "Marattiopsida", "Polypodiopsida" };
// Loop with the foreach keyword.
foreach (string value in ferns)
{
Console.WriteLine(value);
}
}
}
OUTPUT :
Psilotopsida
Equisetopsida
Marattiopsida
Polypodiopsida
No comments:
Post a Comment