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 :
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 :
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 :
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 :
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:
|