Mostly Coherent Ramblings of a Silicon Forest Software Developer

C# .Net Tutorials - How to Learn C# Part 5 - Selection Statements

In part 5 of my learning C# tutorial series we’re going to talk about selection statements. This is a crucial part of your foundation as a C# .NET coder, and it’s pretty important to know how and when to use selection statements.

What is a statement?

Statements are explicit instructions to the compiler. They’re executed in sequence from top to bottom just as they appear in your source code.

namespaceNumberGuesser{classProgram{staticvoidMain(){Console.WriteLine("Please enter a number from 1 to 5");intourNumber=Convert.ToInt16(Console.ReadLine());if(ourNumber<5){Console.WriteLine("is it 5?");if(Console.ReadLine()=="n"){Console.WriteLine("Is it 4?");if(Console.ReadLine()=="y"){Console.WriteLine("Yeah! The answer is 4");}else{Console.WriteLine("Is it 3?");if(Console.ReadLine()=="y"){Console.WriteLine("Yeah! The answer is 3!");}else{Console.WriteLine("Is it 2?");if(Console.ReadLine()=="y"){Console.WriteLine("Yeah! The answer is 2!");}else{Console.WriteLine("Yeah the answer is either 1 or you've answered no every time.");}}}}else{Console.WriteLine("Yeah! The answer is 5!");}}else{Console.WriteLine("You must give me a number from 1-10!");}}}}

Take a good look at the program and see if you can follow the flow. If it looks like a jumbled mess, that’s because we’re using a lot of nested if statements, which in the future will be something you’ll only want to do if you absolutely have to. It does however show how if statements work to check conditions and direct the program to the next spot.

If your number is 5, you will see the following output:

But if you pick the number 1, you’ll get this:

This is because the program uses a top down method to find the number, it basically asks what the number is until you answer yes.

Is it 5? no

Is it 4? no

Is it 3? no

Is it 2? no

It must be 1 as there are no other options.

If you answer yes to any of the numbers, the statement will evaluate to true and break out of the code block. But is there an better way?

If you’re looking for a challenge, rewrite the program above. It’s a horrible algorithm and could be cut down pretty drastically. Just make sure it takes the same input and outputs the same result. This is called refactoring and it’s something you’ll be doing a lot in the future.

Else-If Statements

Another form of if statement is the if-else-elseif structure.

Let’s say we want to figure out what college someone is attending from a list of 3 colleges and tell them what their mascot is going to be. Since two of them are in Oregon, this means the branching will be a little different. Here is our list:

Oregon State University

University of Oregon

University of Washington

Create the following program and run it from your console window:

1234567891011121314151617181920212223242526272829303132333435363738

namespaceMascotFinder{classProgram{staticvoidMain(){Console.WriteLine("Mascot Finder - Find your Mascot Now");Console.WriteLine("Please Choose which university you plan to attend:");Console.WriteLine("(1) Oregon State University");Console.WriteLine("(2) University of Oregon");Console.WriteLine("(3) University of Washington");stringourState=Console.ReadLine();if((string.Compare("1",ourState)==0)||(string.Compare("2",ourState)==0)||(string.Compare("3",ourState)==0)){if(string.Compare("1",ourState)==0){Console.WriteLine("You're going to be a Beaver!");}elseif(string.Compare("2",ourState)==0){Console.WriteLine("You're going to be a Duck!");}elseif(string.Compare("3",ourState)==0){Console.WriteLine("You're going to be a Huskie!");}}else{Console.WriteLine("You must select options 1-3 only");}}}}

Take a look at the code above and observe the structure. We display a menu with the options 1-3 and then read in the answer to evaluate. In line 17 we are asking what what user typed in: if it’s 1, 2, or 3 we’ll move forward, if it’s not we are dropped into the else statement which displays the message that only 1-3 can be selected.

If it is one of the numbers we allow, we check to see if the user typed “1”. If so we announce they will be a Beaver and exit the code block. The new statement “elseif” performs another check immediately after that to see if it’s a 2, and the same for 3. Once a condition is met we will display the right message. If statements, as well as if-else and else-if are valuable tools to control your program flow.

But is there a better way to do this? Naturally there is, and we’ll take a look at that next .

Switch Statements

There is a better way to write this. Switch is a control statement that works with multiple options and tries to make a match, then executes the code with that “case”. Here’s what it looks like:

123456789101112131415161718192021222324252627282930313233343536

namespaceMascotFinder{classProgram{staticvoidMain(){Console.WriteLine("Mascot Finder - Find your Mascot Now");Console.WriteLine("Please Choose which university you plan to attend:");Console.WriteLine("(1) Oregon State University");Console.WriteLine("(2) University of Oregon");Console.WriteLine("(3) University of Washington");stringourState=Console.ReadLine();switch(ourState){case"1":
Console.WriteLine("You're going to be a Beaver!");break;case"2":
Console.WriteLine("You're going to be a Duck");break;case"3":
Console.WriteLine("You're going to be a Huskie!");break;default:Console.WriteLine("You must select options 1-3 only");break;}}}}

In this example you can see what code we’ve changed. We now have this to control our flow:

123456789101112131415161718

switch(ourState){case"1":
Console.WriteLine("You're going to be a Beaver!");break;case"2":
Console.WriteLine("You're going to be a Duck");break;case"3":
Console.WriteLine("You're going to be a Huskie!");break;default:Console.WriteLine("You must select options 1-3 only");break;}

The switch statement takes ourState as a parameter and matches it against each case. If there is a match it runs the code within that case then breaks out of it. If there are no matches, it goes to a “default” case that lets you know none of the options were selected.

A couple of things to remember about switch statements:

You can optionally set a “default” case to run if none of the cases match. This is cleaner than a bunch of if statements like our previous example.

You cannot have two cases with the same value.

Unlike C++ and other languages, there is no “fall through” to the next case. Meaning you can’t leave out the break and have it automatically go to the next one.

Summary

We’ve taken a good look at selection statements, and examined how they’re used. Next we’ll be taking a look at iteration statements, another crucial part of core C# programming.

Each of my tutorials in this series build on the previous ones, and we’ll build on this one as well as we expand into operators and more flow control stuff. I hope you’ll stick with me through these and subscribe to my tutorial feed.

If you have any questions or comments, leave them below I’ll be checking them frequently. In the next tutorial we’re going to cover some iteration statements.