German Wear Discount Shop - Click Here Write for Dotnet-friends and earn for your each submission [Dot]Net-Friends
Skip Navigation Links
Home
Latest
Fast Code
Articles
Tutorials
Online Resources
Forums
Login   | Hi, Guest


Basics of Loops and Loops Syntex

by Omerkamal Jun 20, 2006
This Tutorials covers Basics of C# Loops Syntex- Reader level: Biginner.

This article will show you the basic statements in C# language and language syntax.

Loop Statments:


while

int i = 0;
while ( i < 5 ) {
Console.WriteLine ( i );
++i;
}

The above loop repeates 5 times and prints the value of i.


The output of above code would be like this :

0 1 2 3 4



for

int i = 0;
for ( int i = 0; i < 5; i++ ) {
Console.WriteLine ( i );
}

The above loop repeates 5 times just like the while loop and prints the value of i.
The output of above code would be like this :

0 1 2 3 4



do ... while

int i = 0;
do {
Console.WriteLine ( i );
i++;
} while ( i < 5 );

The above loop is pretty much same as the while loop. The only difference is, the condition is checked only after executing the code inside the loop.



foreach

string [] names = new string[] { "Little John", "Pete", "Jim", "Bill" };
foreach ( string name in names ) {
Console.WriteLine ( name );
}

foreach loop can be used to iterate through a collection like array, ArrayList etc.
The above code displays the following output:

Little john Pete Jim Bill

Conditional Operators


if ... else

This is the conditional operator, used to selectively execute portions of code, based on some conditions.

string name = "Little John"; if ( name == "Jim" ) {
Console.WriteLine( "you are in 'if' block" );
} else
{
Console.WriteLine( "you are in 'else' block" );
}

in the above case, it prints :

you are in 'else' block

Flow Control Statements


break

'break' statement is used to break out of loops ('while', 'for', switch' etc).

string [] names = new string[] { "Little John", "Pete", "Jim", "Bill" };
foreach ( string name in names ) {
Console.WriteLine ( name );
if ( name == "Pete" )
break;
}

In the above sample, it iterates through the array of 4 items, but when it encounters the name "Pete", it exits the loop and will not continue in the loop anymore.
The output of above sample would be :

Little John Pete



continue

'continue' statement is also used to in the loops ('while', 'for' etc). When executed, 'continue' statement will move the exection to the next iteration in the loop, without continuing the lines of code after the 'continue'inside the loop.

string [] names = new string[] { "Little John", "Pete", "Jim", "Bill" };
foreach ( string name in names ) {
if ( name == "Pete" )
continue;
Console.WriteLine ( name );
}

In the above sample, when the value of name is "Pete", it executes the 'continue' which will change the execution to the next iteration, without executing the lines below it. So, it will not print the name, if the name is "Pete".


The output of above sample would be :

Little John Jim Bill



switch

if you have to write several if...else conditions in your code, switch statement is a better way of doing it.

The following sample is self explanatory:

int i = 3;
switch ( i ) {
case 5: Console.WriteLine( "Value of i is : " + 5 );
break;
case 6: Console.WriteLine( "Value of i is : " + 6 );
break;
case 3: Console.WriteLine( "Value of i is : " + 3 );
break;
case 4: Console.WriteLine( "Value of i is : " + 4 );
break;
default: Console.WriteLine( "Value of i is : " + i );
break;
}

In the above sample, depending on the value of the conditional item, it executes appripriate case. In our code, since the value of i is 3, it executes the third case. The output will be as shown below:

Value of i is : 3

Visitors/Readers Comments
(for questions please use The Forum)



nawaz

as u have given all the loops explanation.let me know where these are going to be used in practically?

i mean, as u have shown for loop executes and print 0.........5

and same with while loop,so where we have to use for and while?

differnce why we are using both and the show the examples for all the remaining loops.

switch,break.do..while?why?

 

04/10/2007 18:20:45 UTC

dragon



For Loop 9 X 10










16/04/2008 20:36:26 UTC




Add your Comments

Name:  
Message:
Note: For faster response please use Forums >> for your questions instead of the comments area! (Admin)