C# Control Statements

In this lesson, we'll be covering the major control statements which are for, while, do while,switch and if. With these under your belt, you are well on the way to becoming a seasoned programmer.

The If and else-If Statement

Programs need to make decisions. Should this statement be executed or that one? Are these parameters correct and so on. This is done using the if statement. The syntax is pretty easy.

if ( conditional expression )
statement;

This can be extended with else.

if ( conditional expression )
{
statement1;
else
statement2;
}

This can be extended with else-if.

if ( conditional expression )
{
statement1;
else if ( conditional expression )
statement2;
}

So if the expression is true, statement1 is executed, otherwise statement2 is executed.

The For Loop

This is the syntax of a for statement.
for ( initial statement; conditional expression; loop statement ) main statement;
Note This is the one place where there is no need to put brackets around a conditional expression.

This code sums the array and prints the total of an array with 10 values in it.
int value =0;
for (index=0 ;index < 10;index++ )
value += numbers[ index ];
printf("Value = %i",value) ;

Break and Continue

These two words apply to all the loop types, i.e. while, for and do. Each behave differently.
Break
Break forces a loop to exit immediately. Here's an example:
int markerpos=-1;
for (index=0 ;index < 10;index++ )
{
if (values[index] ==-999 )
{
markerpos = index;
; break;
}
}
if (markerpos== 999)
printf("-1 Not located in array";
else
printf("-1 Found at index %i\n\r",markerpos) ;
That's a more complicated example. It searches the 10 element array values looking for a 999 value. The variable markerpos holds the position if a 999 is found. It is initialized to -1 and after the search this value is examined to see if a 999 was found.
Continue
This does the opposite of break; Instead of terminating the loop, it immediately loops again, skipping the rest of the code. So lets sum that integer array again, but this time we'll leave out elements with an index of 4 and 5.
int value =0;
for (index=0 ;index < 10;index++ )
{
if (index==4 || index==5)
continue;
value += numbers[index];
}
printf("Value = %i",value) ;
You probably won't use continue very often but it's useful on the odd occasion.

While and Do While

First is the while statement which has this syntax.
while (expression) statement
As long as the expression is true, the statement is executed.
int total =0;
int index =0;
while (total < 100)
{
index++;
total+=index;
printf("Sum of 1 to %i is %i",index,total) ;
}

Looping doesn't get much simpler than while. The most important thing to know with while loops is that the control expression is evaluated before the statement is run. If the expression is false, the statement is not run. So a while loop may never execute a statement.
Also because c allows a statement to be an expression it is possible to write complicated code like this
int c=10;
int a =0;
while (c--)
{
printf("Value of a is %i",a++) ;
}
c-- subtracts one from c.
Do While
Less popular than the while statement is the do while statement. This puts the expression at the end.
The syntax looks like this.
do
statement;
while (expression) ;
A while loop might never execute a statement if the expression is false but a do while will always execute the statement at least once.
Here is an example of a do while loop.
int count =0;
int index =9;
do
if (value[ index] ==999)
count++;
index--;
while ( index >= 0) ;
Break and Continue work equally well with for, while and do while loops.

About Switch Statements

When there are many choices, a switch statement may be better than an if. The syntax of a switch statement is:
switch ( expression) {

case a: statement1; break;
case b: statement2;
case d: statement3; break;
default: statement4;

}
Notes: Expression should be an int or a char type. e.g.
switch (character) {

case 'B':
case 'b': statement1; break;
case 'c': statement2;
case 'd': statement3; break;
default: statement4;
}
If the character is 'B' or 'b' then statement1 is executed, but if the character is 'c' then statement2 and statement3 are both executed. With 'd' though, only statement3 is executed. For all other characters statement4 is executed.
When a case matches, all statements from that case on, including those that follow are executed until a break is found. This is known as 'Fall Through'.
switch (value) {
case 3:statement3;
case 2:statement2;
case 1:statement1;
default: break;
}
14:13C# Tutorial