Home
Username:
Password:
Introduction to Microsoft C# 2008 Tutorials

C# Essentials / Expressions & Operators

Subtitles of the Movie

Now let's talk about expressions and operators. This is pretty standard but you will see some things a little different from C# or some of the other things again, especially if you're coming from the VB world or if you've been in a Basic world or some other language. The C languages have all been pretty standard for a while but let me just show you a couple of these. First of all we have the Plus Sign, which obviously means add, right? And let me get everybody on the same page here. Then we have the Minus Sign, which obviously means minus. That means that the person on the right, right? Let me just show you what I'm talking about here. The two on the right will be subtracted from the number on the left. That'll be three minus two and you probably already knew that. The asterisk is the multiplication sign so this would be two times three. Nothing new here. A division and I'll just do this, is the/. Four divided by two. That will return the answer. Then we have the modulus, OK? And the modulus looks like a percent sign and actually what this means is three divided by two, it means divide two into three and only return the value that is left over. So two divided by three would be one with one left over so the result of that would be one so it would be the modulus and the modulus is basically the remainder that's reported back. Then we have the Positive Sign and this is different from a plus and this is where we're actually changing the sign, where the positive and negative aspects of a particular digit and then we have the negative. The Negative Sign will actually change a positive to a negative and vice-versa. Let me go back and explain myself. The Positive Sign will simply return the value of whatever the number is and so this negative sign is the same as multiplying by negative one. And we'll actually switch the sign on it. And then we have the two that tends to confuse some folks and that is the two Plus Signs and we have the two, woops, not underlines; the two Negative Signs. Now, the two Plus Signs are an increment. This adds one to the operand. This is the same as saying X equal X plus one. What we can do here is just write X plus plus. It's the same thing. And this one down here is the same as X equals X minus one. We can just say X minus minus. To really confuse you and you'll have to go out and look in documentation about this, I can also write this instead of X plus plus, I can do plus plus X and if I do the right thing, I can do plus plus X. Now, that means depending on where I put this, if I put the plus in front the result is, it has to do with when I'm going to add whatever I'm adding to my value here. Notice this; I take the value and then I increment it and with this I increment it first and then apply the value. It'll get real confusing, OK? I'll probably confused myself here if I talk about it long. Go out and look in documentation and look at this. If we're just doing a counter, like a variable, like these two it's not going to make any difference. But in some of the more complicated expressions and statements and mathematical statements that you'll use, it will make a difference so just want you to be aware of that. So let's put all these to use in something that makes sense and I'll just put these up and pay close attention to them and go play with some of these, do a screen capture here or just create a little chart like this. Notice if X is equal to 14, then Y is equal to 8, then obviously X plus Y is 14 plus 8, which returns 22. If we subtract them it's 6, if we multiply them it's 112. If we divide them, 8 into 14 goes one time. If we do a modulus, 8, 14 divided by 8, it goes one time with a remainder of 6 and so 6 is what comes out. If we do a minus Y, which is a negative 8 plus 14, it returns 6 and if we do a minus minus Y, it takes whatever Y is and subtracts one from it and so when it comes back, Y is now equal to 7, then a plus X X takes whatever is in X, adds one to it and so X comes back at 15 because it's one higher than 14. I hope that makes sense. So these are your expressions. Now, an arithmetic expression or arithmetic expression, I was trying to say there, is any time that we're assigning a value, just like this right here. This is an expression or a statement. We are doing some sort of mathematical function and usually it means we're assigning a value. You'll hear this stated. It's one of those things that we just kind of say and never think about but what I really wanted to cover here with you are these upper operands and how they can be used; these two especially, this two minuses and the two pluses tend to confuse people so go out and do a little digging in the documentation and make sure you get your head around these, especially if you use these two guys in a very complicated mathematical statement because depending on which side of your variable you put these, it can make a difference in the outcome. That is expressions and operators in the C# language.

Tutorial Information

Course: Introduction to Microsoft C# 2008
Author: Mark Long
SKU: 34046
ISBN: 1-935320-78-5
Release Date: 2009-10-09
Duration: 7 hrs / 76 lessons
Work Files: Yes
Captions: Available on CD and Online University
Compatibility: Vista/XP/2000, OS X, Linux
QuickTime 7, Flash 8

VTC Sign up & Benefits

  • Unlimited Access
  • 98,729 Video Tutorials (23,265 free)
  • Video Available as Flash or QuickTime
  • Over 1026 Courses
  • $30 for One Month Access
  • Multi-User Discounts Available