The If Else C# Statement Option: Learning to Use It

if else c#If else statements are based on the basic true false principle and they are used in programming to set conditions and then set a response to those conditions. For example, you could write a statement about a color: if the egg is white then write the word “white,” else write the words “not white.” In an if else statement, a programmer is able to have a program run while a particular condition is true or false.

C# programming is a form of object oriented programming, and object oriented programming can be used to create things such as video games and other forms of interactive media. An if else statement used in this context could be something as simple as: if the character is alive then play game else end game.

C# programming utilizes if else statements quite often and it is a part of basic programming that you will definitely have to learn sooner rather than later. If you really want to get into programming, then now is the best time. Try the Udemy course C# Introduction to get started on your own interactive programs.

The Basic Structure of If Else Statements

If (condition)
{
Then-statement;
}
Else
{
Else-statement;
}

Above is the structure for an if else statement. It can be broken down pretty easily.

Note the if (condition) first line. This is the line to set the condition. If this condition is true or false the then-statement is used and the program continues on.

If the condition is not set, then the program moves on to the Else line. The Else line states that if the “if” condition is not set then the program follows the else statement instead.

Following that else is the else statement, which is written in place of the then-statement since the condition isn’t true.

There’s also the addition of the else if statement, which comes between the other two statements.

Else if (condition)
{
Else if statement
}

This condition is a bit different from the previous ones established. Instead of just moving onto the else statement, an else if statement sets a new condition and it checks the same value that was checked for the original if statement. If the value isn’t there, then the program either moves to the next else if statement or it moves to the else statement and is completed.

Now that you have seen the basic structure of the statements, look at the example below and see how these programs are used. Master and learning C# programming involves learning things like this, so don’t worry if you are having a bit of trouble getting the hang of the concept.

Looking at an Example of If Else Statements

There are two terms you need to know to understand if else statements, and that is the individual term if and else. An if statement is made for a value that isn’t known and you have to make a decision based on that value. An if statement tests various expressions and evaluates them. After evaluating whether or not the expression is true a set list of commands are performed.

Let’s look at a basic if statement and see how it is applied to programming.

Int value = 10 – 5;
If (value == 5)
{
Console.WriteLine(the answer is true);
}

In this block of code we see that we are giving a math problem where the particular value isn’t given. So we write an if statement instead to evaluate the problem accordingly.

We set the condition that if the value of the problem is equal to number 5 then the following statement will be written “true”.

Now that the condition has been set the program runs and it shows that the value of the problem is 5 so it writes true. Now what if the program doesn’t have a condition that is true? What if the problem was 10 – 4? What does the program do then? That is where the concept of the else statement comes into play.

There are two types of else statements. The else if statement is more specific and it adds in an additional condition, but the else statement works differently. The else condition works only if all of the other conditions previously set didn’t work.

Let’s look at an example.

We set the value of a number to 300.

Value = 300;

Now we place a condition in the if statement of the problem.

If (value == 299)
{
Return 1;
}

In case the value doesn’t match the condition that’s been set an else if statement is created. An else if statement has its own set of conditions and the value isn’t returned unless that else if statement runs true. Below are a list of else if statements.

Else if (value >= 500)
{
Return 0;
}
Else if (value >= 450)
{
Return 1;
}
Else if (value >= 400)
{
Return 0;
}

As you can see, there are several else if statements. Each statement is designed to return a value if the condition is true. If the condition isn’t true the program moves onto the next else if statement.

Now we will move onto the final part of the program, the else statement. This statement is what is set if none of the conditions set are true.

Else
{
Return 2;
}

In this case since none of the conditions that we set are true, the else statement is what the program chooses and it returns the number 2.

When testing an else statement, you have to use the more restrictive version of the tests before you use the less restrictive ones in order to prevent the program from being confused.

For example, notice that our value was 300 and we progressively checked numbers that were lower and lower. Say that our value was 600 and we checked if the number 500 were smaller than 600 and then we checked if the number 450 were smaller. Of course if the number 500 is smaller than the same can be said for 450, but since the condition was set out of order the program chooses not to follow the rule of moving to the number 450. So whatever statement that was set for 450 is skipped since the programmer chose to check the larger value over the smaller one.

This is just a small example of what a programmer can do for if else conditions. These conditions are great to run through a series of characters, values or people and what the program should do given a set condition. It’s a level of true-false evaluation that can work wonders depending on the context of the program. This is a valuable asset for programmers, and it can be used in all sorts of projects.

Getting Acquainted with C# Programming Concepts

C# programming is one of the easiest languages you can learn. In fact, a lot of people start off programming with this language. The writing for it is simple, which means that you will more than likely get the hang of it soon with a bit of reading and practice. Luckily Udemy has several great courses for you. Try the series Learning C# 2010 Part 1, Part 2, and Part 3, and you will go from a beginner to a master programmer in no time.