C# Tutorial - 6. if statement in C#
Welcome to the sixth lesson of my Visual C# Tutorial. In this lesson, we are going to look at if statement and accepting user's input. If statement is one of the most important part in learning C# programming since it gives logics to your applications. Let me first start with accepting user's input which will be used in collaboration with If statement. Let's first create a new project called "IfStatement" for demonstration. If you have a trouble creating new projects, you can simply look at the First Lesson of this tutorial where we talked about creating new projects.
Step 1. Writing code for accepting user's input
First of all, I am going to write a bunch of code to take user's input. You can just copy the following code.
We have usually used ReadLine() method to make our console window not flash away before we can see the results. However, in this block of code, this method is responsible for retrieving whatever users typed in, and the input is assigned to string variable called "userInput". Then the next line of code displays the value of variable userInput with the a literal string "you typed".
When you run the application, the console window prompts you to type something as following.
Just in case you don't actually understand how the plus operator in the method, why
don't you get back to the Fifth Lesson where you can learn about "+" operators in detail.
Step 2. Let's add if statement
Now we are going to add some logics to this application with if statement. So I will write the following code.
Let's first take a look at the code and break it down. What does this code do? Basically this line of code takes whatever you typed in to the console window, and your input will be evaluated by if statement. Then if it is number 1, it will execute the block of code in between the curly braces of if statement. Let's run this application and type 1 in the console window. You will get the same result as following.
Tips. if you have only a single line of code between the curly brace of if statement, you don't actually need to have those curly braces. However, you should put those for more than one line of code.
Let's get to the syntax of if statement. First of all, the basic format of this statement is as following
if ( condition ) { code }
As you can see, there is double equal sign between parentheses of if statement. It is actually different from single equal sing which is for assignment. These double equal signs used for determining whether this expression is true or false, which is in this example whether the userInput variable is number 1 or not. If the expression evaluates to false, and then the code block is ignored, or not executed.
Notice! You must match the date types to compare in the condition of if statement. For example, if ( userInput (string variable) == "1" (string literal) )
What if we typed in something other than number 1? Let's try this scenario out
Then, your application will end right away, and you are going to get this debug box at the bottom of your Visual Studio.
Why do you suppose it is? It is basically because we don't actually have if statement to deal with any input other than number 1. That is why we need to put else if and else statements as well. Let's write a few more code for them.
Now, our application has been expanded with else if and else statement along with if statement. Basically the same format and syntax are applied to those two statements. One thing that you should notice is that else statement does not have ( condition ). It will deal with any input that is not satisfied to the conditions of if and else if statements. In this case, if the variable userInput is neither 1 nor 2, then else statement will execute its code as following.
Then, you can simply think of else if statement as another if statement. So why do we use else if statement instead of multiple individual if statement? It is to make it obvious that we are evaluating the same types of conditions.
Post a Comment