Revisiting expressions
In the section Introduction to programming, we had defined an expression as “A mathematical entity that evaluates to a value”. However, the term mathematical entity is somewhat vague. More precisely, an expression is a combination of literals, variables, operators, and functions that evaluates to a value.
A literal is simply a number, such as 5, or 3.14159. When we talk about the expression “3 + 4″, both 3 and 4 are literals. Literals always evaluate to themselves.
You have already seen variables and functions. Variables evaluate to the values they hold. Functions evaluate to produce a value of the function’s return type. Because functions that return void do not have return values, they are usually not part of expressions.
Literals, variables, and functions are all known as operands. Operands are the objects of an expression that are acted upon. Operands supply the data that the expression works with.
Operators
The last piece of the expressions puzzle is operators. Operators tell how to combine the operands to produce a new result. For example, in the expression “3 + 4″, the + is the plus operator. The + operator tells how to combine the operands 3 and 4 to produce a new value (7).
You are likely already quite familiar with standard arithmetic operators, including addition (+), subtraction (-), multiplication (*), and division (/). Assignment (=) is an operator as well.
Operators come in two types:
Unary operators act on one operand. An example of a unary operator is the – operator. In the expression -5, the – operator is only being applied to one operand (5) to produce a new value (-5).
Binary operators act on two operands (known as left and right). An example of a binary operator is the + operator. In the expression 3 + 4, the + operator is working with a left operand (3) and a right operand (4) to produce a new value (7).
Note that some operators have more than one meaning. For example, the – operator has two contexts. It can be used in unary form to invert a number’s sign (eg. -5), or it can be used in binary form to do arithmetic subtraction (eg. 4 – 3).
Conclusion
This is just the tip of the iceberg in terms of operators. We will take an in-depth look at operators in more detail in a future section.
1.6 — Whitespace and basic formatting
|
Index
|
1.4 — A first look at functions
|
1.6 — Whitespace and basic formatting
Index
1.4 — A first look at functions
[...] 1.5 — A first look at operators [...]
Simple yet to the point.
This is the best tutorial I have come across. Well done and thank you very much for this valuble
information about the C++. I wonder if you have tutorial for other languages such as C# and
“Perl”, and “verilog”.
Once again thank you very much
Fuad Mannai
Nope, no tutorials for other languages. I’m not even done with this one yet!
A good Perl book is “Teach Yourself Perl in 21 Days”. For Perl TK (Tool Kit), try “Mastering Perl Tk”.
Hope this helps.
As a side note, Alex, well done for taking the trouble to compile such an in-depth tutorial. I’m sure hundreds, if not thousands out there appreciate your work.
El Nino
I am one of them
You’re welcome. Thanks for visiting.
Thank u gt a xam in 3 days fr c ++
ur notes r very usefull
Thank you very much, this is an awesome tut. I learned so much already.
First of all, very good tutorial, I’m new to programming and was thinking of buying a book until I came across this on Google. And second, what is an example of an operand? In the equation “1 + 2″ would 1 be an operand?
Adam
In your equation both 1 and 2 are the operands upon which the + operator acts.
Ben
I have read it till here now, and i iwent for my 2nd c++ practical lecture, what seemed like cm chinese lingo to me in the 1st lecture was all crystal clear!!!! this is the best!! undoubteldy
I must say that I searched for a walkthrough tutorial, and have gone through 10+ different sites…this is the easiest to understand and the most in-depth one that I found…….
MUCH THANX!!
Nick M.
You deserve an award of some kind for doing these tutorials! Trying to learn C++ to make coding for the iPhone (Cocoa Touc, Objective-C) easier to adjust to and this website is a allowing for an easier transition. :)
Alex……Your work is amazing for it was not for you i should be nowere in C++
Thank you so much
hey i love the tutorials! very easy to follow.
just one thought: correct me if im wrong but the – operator is always unary as 4 – 3 is essentially 4 + (-3)
:D
Most easy to understand tutorial I have come across
This is the best tutorial I have come across, pretty much everything is explained so well that I just get it and can play around with it easily. Thanks Alex!
Same thing everyone else has stated a thousand times before. This is an amazing tutorial and you are really putting this language into a context that even I, a high school student, can understand. I am an aspiring programmer and I would like you to know that you will be in my heart forever
But seriously, thanks Alex!
I am beginner in c++ it is helping me very much.
very good tutorial……thank u so much
it says above.
As far as I understand, a literal can also be a letter.
See Constants at cplusplus.com for more information.
PS: To balance my somewhat negative comments, I must also emphasize that this tutorial on C++ is the best one I have come across sofar. For now, I give it 4 out of 5 points; later, I may raise that grade.
I tried to correct the PS above to below but ran out of time:
To balance my somewhat negative comments, I must emphasize that this tutorial on C++ is the best one I have come across sofar. For now, I give it 5 out of 5 points; later, I may even raise that grade! (for an explanation of the last statement, see Links at mkforlag.com).
That you only have a few minutes to correct your comments is probably what I dislike most about this website.
I believe that the last thing mentioned about – being both a unary and binary operator is wrong. It is only unary because when you enter something like (x – y) it seems like binary but is really just short for (x + -y) witch still leaves – as unary. Compilers are simply just built to understand what you meant.
awesome job alex!
awesome……….. hurray…………..!!!!!!!!!!