From the course: Learning C++
Expressions, assignments, and operations - C++ Tutorial
From the course: Learning C++
Expressions, assignments, and operations
- [Instructor] Now let's talk about three important components of C++ code. Let's start with expressions. An expression is a symbolic representation of a calculation like the expressions you see in algebra. An expression may contain variables, constants, and operators, and every expression must produce a value. An assignment is a line of code that assigns a value to a variable. It must contain a left-hand side and a right-hand side expression. The left-hand side expression must be able to hold a value and the data types of both expressions must match. This last point is not enforced by the compiler, as you saw when we assigned a negative number to a unsigned variable, so you as a programmer are responsible of keeping your code consistent. Lastly, let's review some of the most important operations in C++. We have arithmetic operators, plus for addition, minus for subtraction, asterisk for multiplication, forward slash for integer division, that's quotient, and the percentage symbol for the modulo or remainder of a division. The forward slash operator is also for floating point divisions. It all depends on the operands you use. We also have bitwise Boolean operations. That is binary operations performed bit by bit between the operands. We have single ampersand for bitwise and, single vertical bar for bitwise or, tilde for bitwise nought, and caret for bitwise xor. Again, these are bitwise operations, not logical. Now logical operators are the ones you probably want to use in an if statements condition. These evaluate two operands that can either be true or false in the same logic we've seen so far. Remember, zero means false and anything else means true. The logical operators are double ampersand for and, double vertical bar for or, and exclamation sign for nought. We have relational operators to compare values, double equal sign for equality. Be careful with this one. A common mistake is to use the single equal sign in a conditional, and the problem is that this is valid because it returns the assigned value. Next, we have exclamation sign equal for not equal, greater than, less than, greater or equal and less or equal. And finally, we have three popular pointer operations, prefix asterisk for indirection, which dereferences the pointer to its right, which is its only operand. That is to say it gives you access to the variable the pointer is pointing to. This is not the multiplication operator despite sharing the same character. We also have prefix ampersand for the address of operator, which returns the address of the variable at its right, which is its only operands. And we have the arrow operator, which gives you access to members of classes or structures from a pointer. You may have noticed the lack of a logical xor operator, and that's an interesting exercise to think about. Here's a hint. What does the not equal relational operator do? Since you'll be seeing a lot of expressions, assignments, and operations up ahead, you may want to consult cppreference.com to learn more. Just Google CPP reference operator precedents and you'll see an exhaustive list of operators and their precedents. Take a minute to browse through it.
Contents
-
-
-
-
(Locked)
Basic data types3m 44s
-
Variables2m 6s
-
(Locked)
Using variables5m 18s
-
Expressions, assignments, and operations3m 52s
-
(Locked)
Type inference with auto3m 5s
-
(Locked)
Preprocessor directives7m 10s
-
(Locked)
Constants1m 58s
-
Arrays6m 44s
-
(Locked)
Strings5m 42s
-
(Locked)
Type casting4m 34s
-
(Locked)
Type casting examples5m 13s
-
(Locked)
Enumerations7m 55s
-
(Locked)
Challenge: Calculate an average57s
-
(Locked)
Solution: Calculate an average54s
-
(Locked)
-
-
-
-
-