ing 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.
C# Tutorials: How to Learn C#
- C# Introduction
- C# Namespaces
- C# Coding Basics
- C# Variables and Types
- C# Selection Statements
- C# Iteration Statements
This will print out the following:
Hello, is it me you’re looking for?
This is how all statements are evaluated in C#. There are several types of statements you use, which we’ll examine individually. Today’s tutorial will cover selection statements.
Selection statements control program flow through the result of evaluating boolean (true and false) values. An if statement only executes if the conditions are true. Here are some examples:
1 2 3 4
This statement simply checks that the conditions are true and does something based on that. A real world example looks like this:
1 2 3 4 5 6
If you set the integer ourValue to 10, you’ll see some output. If ourValue is not equal to 10, it will exit the section and move on to do nothing.
A more common use is the if-else statement. This will check the conditions you present and do one thing if it’s true, and another if it’s false. Both code paths lead to an action of some kind.
1 2 3 4 5 6 7 8 9 10
This is very useful because you can nest “if” statements to control program flow in powerful ways. Let’s build a silly little program that tries to guess the number you’re thinking of in a few steps.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
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.
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
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 .
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
In this example you can see what code we’ve changed. We now have this to control our flow:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
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.
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.