Microsoft Visual C Sharp: Lesson Two

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

The second topic that I would like to discuss is Program Flow. In Microsoft Visual C#, there are mechanisms by which you can successfully control your logic and the overall flow of the program. It is important to select the right method to control the direction of your program.

Method #1: If - Else Statements

Essentially, the else component of the if-else statement gives you the capability of having code that executes when the if statement’s conditions fail or are false. Also, only one of the two aspects of this statement actually execute. Both never execute, if set up correctly. So, this the beauty of this particular program flow is that if you have a simple fork in the logic, if-else allows the program to go via route A or route B quite easily.

Note: If-Else Statements can be increased to include else if statements as well. Nonetheless, when all listed paths for the program logic prove false, the final else is where the program continues on ahead.

Method #2: Switch Statements

Microsoft Visual C# provides yet another much easier way to modify program flow based on multiple values that are stored in a variable: with the switch statement. The format for this statement is as follows:

switch (value)
{
case result 1:
case result 2:
case result n:
default:
}

In this program flow, there is no condition. Rather, a value is used. This value 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, then the flow goes to the default case. If there is no default set-up, then the first statement following the switch statement is where the flow goes.

Method #3: Iteration Statements

When you want to repeat a segment of code in your application, Microsoft Visual C# provides a number of iteration (e.g. repetition) techniques. They are as follows: while, do, for, and foreach.

The basic logic of a while statement is as follows:

while (condition)
{
statement(s)
}

A while statement uses a conditional statement. If the condition is met (true), then the statement is executed. Otherwise, the statement(s) are not executed and the program flow goes to the next command following the while statement.

A do statement, however, first executes its statements. Then a while is presented with a condition. If the while condition is true, the program returns to the statements, otherwise, the program flow goes to the next line after the do….while. The syntax for a do statement is as follows:

Do
{
statement(s)
} while (condition);

The for statement, the third of the four iteration methods, consolidates steps into a much simpler format, as illustrated below:

for (initializer; condition; incrementor)
{
statement(s);
}

The initializer is executed when the for statement begins. Then, the condition statement is evaluated. Finally, the incrementor is generally used to increment a counter. After the incrementor is executed, the condition is again evaluated, and this happens as long as the incrementor is true and not maxed out. As long as the incrementor is not maxed out, the statement(s) in this program flow are read by the compiler and executed accordingly.

Lastly, the foreach statement iterates very much like the for statement. However, the foreach statement has a special purpose - it can loop through collections such as arrays.

Inspired Source: Learn C# in 21 Days by Bradley Jones

Please bookmark and share this post: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • StumbleUpon
  • Technorati
  • DZone
  • PlugIM
  • Reddit

Share your thoughts, leave a comment!