Learning C# Part 3: Control Structures

Written By: Kevin Jordan

- 02 Sep 2006 -
















Description: This series aims to take a beginning programmer to an intermediate level in C#. Topics covered in Part 3 include: if/else statements, switch/case, and for, while, and do loops.

  1. If, Else Condition Statements
  2. Switch Statements
  3. for Loops
  4. do and while Loops
  5. Aww Man... Not Homework

while Loops

The while loop is another pretest loop, but is used when you don't really know how many times you want the code block to be executed.

while (readingFile == true)
{
        GetNextLine();
}

The above example is oversimplified, but you get the idea. We don't usually know how big a file is, so we keep reading until we're through.

Say I'm creating a css optimizer, and I want to ignore some the commented lines and just read in the important stuff. I could do one of two things (probably a lot more actually, there's always a million ways to code anything). I could create an if condition that checks to see if the next line is a comment, but then all the important stuff is nested in the if block. Another option is using the continue keyword.

// First option using an if statements
while (readingFile == true)
{
        string currentLine = GetNextLine();
 
        // Make sure the current line is not a comment
        if (!IsComment(currentLine))
        {
                // Save off to perform CSS optimization               
        }
}
 
// Second option using continue
while (readingFile == true)
{
        string currentLine = GetNextLine();
 
        // If the current line is a comment continue from beginning of loop
        if (IsComment(currentLine))
        {
                continue;
        }
 
        // Save off to perform CSS optimization
}

This example introduced a couple things. On line 4 you see GetNextLine(), this is a method which we'll cover a lot more in depth when we get to objects. What you need to know now is that it performs code that's located somewhere else and will return a value to us (most of the time). GetNextLine hypothetically goes to a file and returns the next line of text for us. IsComment on line 5 is very similar except that it passes a parameter. In our case, the current line of text is passed and either a true or false value is returned based on whether the IsComment method decided that line was formatted line a comment.

Also on line 5 you see an exclamation point. This means NOT in C#. So we're basically saying if the current line is not a comment then perform the CSS optimization stuff.

The second option is a little different because instead we're saying if the line is a comment there is no need to process the remaining lines, so go back and start the loop again.

Finally there's a way to just break out of the loop all together using, you guessed it, break.

while (readingFile == true)
{
        string currentLine = GetNextLine();
 
        // Exit on file errors
        if (FileError())
                break;
}

Since there is only one statement to execute after the if condition, the curly braces aren't required. But more importantly, if there is a error in the file then we don't want to read in any more, so we can use break to escape the while loop.

do Loops

A do loop is extremely similar to a while loop except it is a post-test loop. The code is executed first, then the condition is tested, ensuring that the code is executed at least once.

do
{
        // Code to be executed
} while (expression is true)

<< Previous

Next >>