.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> C# >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Explaining Control Structures

Posted By:Shashi Ray       Posted Date: March 30, 2009    Points: 25    Category: C#    URL: http://www.dotnetspark.com  
 

Explaining Control Structures

The C# language supports all of the flow-of-control statements you would normally expect.This section gives you a very brief look at them.We point out a few of the problem areas (especially for C/C++ programmers).

  

Using the if Statement

The if statement executes a series of statements if a test Boolean expression evaluates to true.The test expression to evaluate must be Boolean.You cannot use a test numeric expression as in C/C++:

int i = 3;

int j = 0;

if ( i > 2 )

{

j = 3;

}

 

 Using the if-else Statement

The if-else statement adds a path for the false evaluation of the Boolean expression.

int i = 3;

int j = 0;

int k = 0;

if ( i > 2 )

{

j = 3;

}

else

{

j = 4;

k = 5;

}

 

Using the switch case Statement

The switch statement chooses flow of control based on the evaluation of a numeric or string comparison.The switch statement does not allow control to fall through to the next case as in C/C++ unless the case statement is followed immediately by another case statement. In other words, you must use a break statement with every case statement.You can also use a goto statement, although most programmers frown on using them. Here are two examples:

 

int j = 0;

int i = 1;

switch ( i )

{

case 1:

j = 7;

break;

case 2:

case 3:

j = 22;

break;

default:

j = 33;

break;

}

 

string lastName = "";

string text = "shashi";

switch ( text )

{

case "shashi":

lastName = "ray";

break;

case "satyam":

lastName = "kumar";

break;

default:

lastName = "abhishak";

break;

}

 

Using the for Statement

The for statement is used to loop through a series of statements until a test Boolean expression evaluated at the beginning of the loop is false. In the following example, the WriteLine method will execute five times:

 

for ( int i = 0; i < 5; i++ )

{

Console.WriteLine( "Hello Dear" );

}

 

Using the while Statement

The while statement is also used to loop through a series of statements until a test Boolean expression evaluated at the beginning of the loop is false.The following code has the same result as the previous for statement example:

 

int i = 0;

while ( i < 5 )

{

Console.WriteLine( "Hello Dear" );

i++;

}

 

Using the do while Statement

The do while statement is also used to loop through a series of until a test Boolean expression evaluated at the end of the loop is false.Therefore, the series of statements contained within the do while loop will always execute at least once:

 

int i = 6;

do

{

Console.WriteLine( "Hello Dear" );

i++;

}

while ( i < 5 );

 

Using the break Statement

The break statement exits the loop of a for, while, or do while statement regardless of value of the test Boolean expression. In each of the following examples, the WriteLine method will execute two times:

int j = 0;

for ( int i = 0; i < 5; i++ )

{

Console.WriteLine( " Hello Dear " );

j++;

if ( j == 2 )

break;

}

int i = 0;

int j = 0;

while ( i < 5 )

{

Console.WriteLine( " Hello Dear " );

i++;

j++;

if ( j == 2 )

break;

}

int i = 0;

int j = 0;

do

{

Console.WriteLine( " Hello Dear " );

i++;

j++;

if ( j == 2 )

break;

}

while ( i < 5 );

 

Using the continue Statement

The continue statement will pass flow of control immediately to the start of a loop when encountered. In the following example, "I will not talk in class" will display twice and "At least I'll try not to talk in class" will display three times:

int j = 0;

for ( int i = 0; i < 5; i++ )

{

j++;

if ( j > 2 )

{

Console.WriteLine( " Hello Dear " );

continue;

}

Console.WriteLine( " Hello Dear " );

}

 

 

Using the return Statement

The return statement returns flow of control from a method to the caller, optionally passing back a return value. Here is a complete example:

 

using System;

class TestDivision

{

static void Main(string[] args)

{

int dividend = 2;

int divisor = 0;

Divider divider = new Divider();

bool ret = divider.divide( dividend, divisor );

if ( ret == true )

Console.WriteLine( " Hello Dear" );

else

Console.WriteLine( " Hello Dear" );

}

}

class Divider

{

public bool divide( int dividend, int divisor )

{

if ( divisor == 0 )

return false;

int result = dividend / divisor;

return true;

}

}

 

Using the goto Statement

The goto statement has been the bain of structured programming for many years. C# supports the goto statement, although as previously stated, we wouldn't recommend using it.The goto statement immediately transfers flow of control to the statement following a label. If you must use goto, here is an example:

 

int i = 0;

int j = 0;

while ( i < 5 )

{

Console.WriteLine( " Hello Dear Jump you" );

i++;

j++;

if ( j == 2 )

goto jumpeddoutofloop;

}

jumpeddoutofloop:

Console.WriteLine( " Hello Dear Jump you" );

 

Shashi Ray


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend