Lesson 1.8 – The if Decision Statement

C# Course for Absolute Beginners – Lesson 1.8
Next Lesson

Introduction

In this lab you’ll begin to learn to program more complex code. In the last lab you learned to accept user input through the ReadLine method. Now you’ll make decisions based on that input, and change how the program executes based on the user input.

 

Step 1 – Creating a Simple if Statement

Step 1 - Creating a Simple if Statement

To begin, we’ll type some code. Before we discuss what the code means or does, you should take a moment to read it. Try to understand what the outcome of this code will be. Open a new project or delete the code from the previous project, and retype the code in the above image.

Looking at the code, try to imagine what will happen if the user enters 1. Also, what happens if the user enters 2 or 3, or a random character like M.

 

Step 2 – Testing the Simple if Statement

Step 2 - Testing the Simple if Statement

When we run the program, we see our initial message. If the user enters 1 our response is printed to the screen.

 

If we run the program again and enter the number 2, we see that nothing seems to happen. So if that’s your guess, you are correct.

 

Step 3 – Understanding the Simple if Statement

Step 3 - Understanding the Simple if Statement

In our code, all we did was introduce an if statement. An if statement tests a condition to see if it’s true.

(1) In our code, we’re testing to see if the condition is true – if the user value equals the string 1. If the condition is true and the user value equals the string 1, then the code executes everything within the code block below.

(2) The curly braces represent a code block, so the code is executed when our if statement evaluates as true.

 

Step 4 – Creating a Complex if Statement

Step 4 - Creating a Complex if Statement

We’ll continue now by expanding this example and add else conditions. Retype the new lines of code in your window. Again, you should study the C# code yourself to try and imagine what it does. This will also show you that it’s easy to read the code, even if you’ve never seen it before.

Looking at the code, we se that if line 15 is true, lines 17 and 18 will execute. If line 15 is NOT true, our program will continue to the next condition. If line 20 is true, the code block below in lines 22 and 23 will execute. If line 20 is NOT true our program will continue to the third condition in line 25. Finally if none of the previous conditions are true, our program will execute the last code block in lines 32 and 33.

 

Step 5 – Testing the Complex if Statement

Step 5 - Testing the Complex if Statement

Let’s test our code to be sure it works correctly. We already know that the code works when we enter 1. So we’ll run the program a couple more times and see what happens. If we enter 2, we see that our code works.

 

If we enter 3, our program ignores the first two sections because the conditions weren’t true – the user did not enter 1 or 2. Instead, the program displays a different line of code.

 

Finally we’ll test the last condition. If we enter the letter "a" the program sees that none of the previous conditions were true, so it executes the last code block instead. We see that our program is working perfectly.

 

Step 6 – Understanding the Complex if Statement

Step 6 - Understanding the Complex if Statement

It’s important for you to understand that this is the basic format for a conditional statement – an if statement. Later there will be another conditional statement we’ll review. For now, study and memorize this format:

An if statement tests a condition. If the condition is true, the code block beneath is executed.

If the condition is not true, the program moves to the next condition – the next "else if" statement.

This process continues until the program locates a statement is true.

If no statement is true, the code will execute the final statement – the "else" statement. The "else" statement is used to capture every other condition or possibility that might occur.

 

Step 7 – Refactoring the Conditional Code

Step 7 - Refactoring the Conditional Code

Before we continue, we want to make the code more compact. We see that we have multiple Console.WriteLine lines throughout our program. We’re going to refactor this code. Refactoring is the process of looking through the code and changing things about its readability. We aren’t necessarily changing how the code functions but only how the code appears and its structure. We’ll discuss refactoring in greater detail in the future, including more about what refactoring is, different types of refactoring, and the tools that are built in to the Visual Studio IDE to help you refactor. We’ll also discuss some third party tools that help make refactoring very simple.

For now, we’ll simply rewrite some of the code to simplify it. We will not change how the code functions or performs. The reason we’re refactoring this code is because a programmer doesn’t want to use any more characters than necessary. You should begin thinking along these same lines and asking yourself if there’s a shorter way to write the same code.

To refactor this code, we’ll do this:

First in line 16, we’ll create a new string variable called message and initialize it to an empty string.

Second we’ll remove the various Console.WriteLine codes in each of the four possible outcomes – if, else if, else if, and else. We’ll replace each of these with our variable, "message."

Third we’ll create a single Console.WriteLine at the bottom of our code, just before the final Console.ReadLine();

Finally we’ll add the "message" variable to our Console.WriteLine in line 35.

Now, our program will simply print out whatever the variable becomes – very simple.

 

We’ll test our program to be certain it still functions. We see that if we select 2 we receive "You won a new boat." This is exactly what we expected.

 

Step 8 – Understanding the Importance of Curly Braces

Step 8 - Understanding the Importance of Curly Braces

One important note: If you only have one statement in a code block, the curly braces (lines 19 and 21) are optional. Some programmers will include the curly braces anyway, because they like the convention of using them regardless. However since you will eventually need to review code written by other programmers you should understand that the curly braces are optional, if there’s only a single line of code.

 

If there’s multiple lines of code, the curly braces are required.

In the above image, line 24 (you won a cat) will work without curly braces – there’s only a single line of code.

However lines 19 and 20 (you won a new car) will NOT work without curly braces – there are multiple lines of code.

If we were to run this code we would receive error messages.

 

Conclusion

In this lab you learned how to create an if statement to modify how your program functions based on user input. You then learned to create more complex statements by adding "else if" and "else" sections to your code. You learned what refactoring is, and you learned the importance of curly braces.

It’s important that you memorize this syntax. You should be as familiar with this syntax as with any combination of words that make up a sentence in the English language. The only way to memorize this is if you actually code – often. Follow along with the lab, but also try your own variables. Ask yourself, "what would happen if I try this?" Experiment with the code. Flashcards will also help you to memorize the basic format, which is exactly what you should be working towards.

By now you should also see that C# is relatively easy. Most of it is intuitive – C# just makes sense, and it’s easy to read once you understand the basic concepts.

 

Next Lesson

10 Year Anniversary Sale - 30% Off - ENDING SOON!!!    Learn more ...