From the course: Learning C#

Operators - C# Tutorial

From the course: Learning C#

Operators

- [Instructor] So far, we've seen how to declare variables and we've learned about some basic data types in C-sharp along with some basic C-sharp statements in tax. So now let's take a look at some of the common operators that your program uses to operate on data. So here in the start folder, in the overview and operators folder, I'm going to click on the program file, and let's have some more room there. So I've already created a few variables here. So I've got some integers and a couple strings and we'll use these to try out some operations. So as you might expect, the basic math operators like addition, subtraction, and so on are available. So let's try those out first. I'm going to print the results of a math statement. So I'll just do X divided by Y times X. And in fact, you could even do some operations like addition for example, on strings. So I'll do the same thing (keyboard typing) What I'm going to to write here is A plus B and you can see that a is A string and B is a string. So when you do this on two strings you're basically just concatenating them together. And there are some shorthand ways of doing math operations. So for example, if all I want to do is either add one or subtract one from an integer, I can use the plus plus to increment or minus minus to decrement a number. And so let's go ahead and print those out as well. (keyboard typing) X, Okay. (keyboard typing) Making that Y. All right. And I can also use some shorthand for the math operators. So for example, instead of writing out this entire expression, A equals A plus B I can shorten that to A plus equals B and let's go ahead and print that out. (keyboard typing) So let's run what we have so far. So we're going to do a couple of math operations and some shorthand and then a little shorthand assignment operator right here. So I'll save that and right click on this folder, open integrated terminal. And again, you can use the regular terminal if you want to, then I'll type .net run. So .net it's going to go ahead and build the code and in the terminal, we can see the result. So here's the result of the math. So here's X divided by Y times X and then here's the two strings being added together and then four X plus plus and Y minus minus we've got 11 and four now. And for A plus equals B, when we print out A we can see that A has now been increased to B both strings. Okay, let's take a look at another kind of operator. We're going to look at logical operators. So let's go ahead and hide the terminal. And what I'm going to do is comment out these Console.WriteLines. So you can do multiple comments by doing control slash on a selection or command slash if you're on the Mac. Some I'm just going to go ahead and comment these out so that we minimize our output, comment that one as well. I'm going to leave X plus plus and Y minus minus though, I'm going to keep those. So let's try some logical operators. Logical operators are used when your program needs to make decisions, which we'll learn about later or check certain conditions. So these operators produce Boolean results of either true or false. So for example, I can use the double ampersand operator to perform a logical and operation. So I'm going to print out X is greater than Y and then and Y greater than equal to five. So this expression will be true if X is greater than Y and Y is greater than or equal to five. And then similarly, the, or operator is two vertical bars. And so I'm going to go ahead and make a copy of this statement and I'll just change this to two vertical bars. So now I have X is greater than Y or Y is greater than, or equal to five. Let's go ahead and run the updated code. So we'll get our terminal back and we will .net run and we're not using some variables so we get some mornings. But you can see that the results are false and true. The first result is false because while X is greater than Y, Y is not greater than or equal to five, right? Because when we had this minus minus statement in here so that brings it down to four from its initial value of five. So the first one was false. The second one however, is true because X is greater than Y, or Y is greater than equal to five. So this condition is true if only one of these other sub conditions is true. And since this one does satisfy that condition we can see that the output is true. So there are two more operators to check out and they are both of a form of what's called the no coalescing operator. So let's go ahead one more time and let's close that window and we'll comment these guys out, and I will uncomment this string variable. So the first example is the double question mark operator string that down. So the double question mark operator uses the left operand if it's not null, or the right one, if it is. Again, let's assume I have this string variable whose value is set to null. And remember we learned earlier that null just means it has no value. So I can write a shorthand operation like this. I can write Console.WriteLine, and what I'm going to do is write str and then double question marks, and then another string called unknown string. So what's going to happen is this expression is going to be evaluated. And the string that gets passed to write line is going to be str if it's not null, or this string, if it is null. And since str is currently null, we should see this in the output. So the other operator that we're going to look at is called the double question mark equals operator, which performs an assignment if the value is null. It doesn't just check for null, it actually performs the assignment. So for example, instead of writing out this long if statement, which we'll learn about ifs in a little bit. But instead of writing out this long form of checking to see if a variable is null and then assigning a value if it is, what I can do instead is something like this. I can write str, double question mark equals new string. So if str is null, then this value will be assigned. Otherwise this won't happen and it'll just keep whatever value it currently is. It'll just be left alone. So let's go ahead and try these two out. And I'm actually going to print (keyboard typing) that string before I forget. Okay, so let's save, and let's get our terminal back. And let's .net run one more time. So we can see that the output of the first expression was unknown string because str was null. And then the second example we have str double question mark equals new string. So because str was null, this assignment did take place. Now, if I go back and change this to something else, let's make it hello. And then let's run this again. See, now you'll see that we get hello in both cases because in this case, str got selected 'cause it's not null. And then this assignment didn't happen because again, the value is not null. Okay, so that's enough about operators to move along to the rest of the chorus. But if you feel like maybe you need some more practice then I suggest spending a few minutes here experimenting and trying out different combination of operators to see how they work.

Contents