System ProgrammingLearn about Conditionals and Decision Making in C#

Learn about Conditionals and Decision Making in C#

In order to learn about logical operators, you first have to understand conditionals.

Conditionals
How many times did you have to take a decision that is based on a true or false condition? For example, if I have time this evening (true) I will try some C# code. But if I didn’t (false) I will have to do it some other time. All programming languages have this sort of “decision making”. Sometimes it’s called “flow control”, and C# is no exception.

Recall the variable types we discussed in an earlier post, and consider the bool variable type. bool is short for Boolean, it has only one value: true or false. It often used as a result of comparison.

To do a comparison, you have to use a special kind of operator called comparison operator, or logical operator.

Logical operators
Imagine you are coding a program that calculates the grades of a student. If the result is more than 50, the program notifies the user that he/she has passed. Otherwise, it prints “better luck next time”. Logical operators are your only way to compare the result with the threshold you set (50). Let’s add a fourth variable to the list of variables used in this post:

int var4 = 6;

The following table illustrates this type of operators:

Operator Uses Example
== Both variables are equal in value bool result = var1 == var2; //result is false
bool result = var1 == var4; //result is true
!= The value of the first variable is not equal to the value of the second one bool result = var2 != var3; //result is true
bool result = var1 != var4; //result is false
< The value of the first variable is less than the value of the second one bool result = var1 < var2; //result is false
bool result = var2 < var3; //result is true
> The value of the first variable is more than the value of the second one bool result = var1 > var2; //result is true
bool result = var3 > var4; //result is false
<= The value of the first variable is less then or equal to the value of the second one bool result = var1 <= var4; //result is true
bool result = var1 <= var2; //result is false
>= The value of the first variable is more than or equal to the value of the second one bool result = var1 >= var4; //result is true
bool result = var3 >= var2; //result is false

These are not the only Boolean variables in C#, there is also ! (NOT), && (AND), and || (OR). But those can be best explained through an IF conditional example.

The if statement
You can make decisions in C# using an if statement based on the Boolean value of some variable. The syntax is as follows:

if (test1)
{
	//code to execute test is true
}
else if (test2)
{
	//code to execute if test1 is false and test2 is true
}
else 
{
	//code to execute if test1 and test2 results are false
}

Notice that you can add else if statements as much as you need to pass from one test to another, until all the tests result in false to execute the else code.

Example:

int result = 80;
if (result < 50)
{
	Console.WriteLine("Better luck next time");
}
else if (result >= 50 && result < 70) 
{
	Console.WriteLine("You did good");
}
else 
{
	Console.WriteLine("You did excellent");
}

When you execute this code, the output will be You did excellent. Let’s have a look at what the code does here:

You started by assigning 80 to an integer variable. In a real world application, this number should be the result of a calculation or grabbed from a database. Then you started your if statements to test the result: if it’s less than 50, print a message, if it’s more than 50 yet less than 70, print another message. If it’s neither less than 50, nor between 50 and 70 (which effectively means more or equal than 70), print your final message.

But notice here the new operator && (AND). This operator evaluates to true only if both tests evaluate to true. So in our case, this test result is false because 80 is more than 50 (true), but not less than 70 (false).

Learn Cloud Computing from Scratch for Beginners

Now let’s have a look at the ! (NOT) operator:

bool morning = true;

bool morning = true;
if (!morning) 
{
	Console.WriteLine("Good afternoon");
}
else 
{
	Console.WriteLine("Good morning");
}

The code above greets the user based on the time of the day. You assign true to the bool variable morning. Then you test it’s value using if, and the ! operator. This operator evaluates to true only if the variable is false, and false if the variable is true (the inverse of the variable). So in our case, it’s like saying: if NOT morning is true (evaluates to false because morning is true), print “Good afternoon”, otherwise print “Good morning”.

The || (OR) operator is similar in syntax to the && one in that it accepts two variables. It evaluates to true if at least one of the variables is true. Consider the following:

bool morning = true;
int now = 13;
if (morning || now < 12) 
{
	Console.WriteLine("Good morning");
}
else 
{
	Console.WriteLine("Good afternoon");
}

Here you set the bool variable morning to true, and set the integer variable now to 13. Then you tested for morning = true (true), or now is less than 12 (false). But because the || operator will evaluate to true if at least one of the tests is true, “Good morning” will be printed to the screen.

Conclusion
In this post, we had a look at logical expressions. We also covered conditionals, and how the logical expressions are a concrete part of building a conditional if statement.

In the next article, we will continue our discussion of conditionals. We will introduce the switch statement and a very handy shorthand for using the if statement called the ternary operator.

I hope you enjoyed reading this post. See you in the next one.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -