- Expressions
- building blocks to statements
- Statements and block statements
- building blocks to C++
- Operators
- Assignment
- Arithmetic
- Increment and decrement
- Equality
- Relational
- Logical
- Compound assignment
- Precedence
-
Expressions
- the most basic building block of a program
- "a sequence of operatros and operands that specifies a computation"
- computes a value from a number of operands
- There is much, much more to expressions - not necessary at this level...
/* Expressions - examples */ 34 // literal favorite_number // variable 1.5 + 2.8 // addition 2 * 5 // multiplication a > b // relational a = b // assignment
-
Statements
- a complete line of code that performs some action
- usually terminated with a semi-colon (
;
) - usually contain expressions
- C++ has many types of statments
- expression, null, compound, selection, iteration, declaration, jump, try blocks, and others.
/* Statements - examples */ int x; // declaration favorite_number = 12; // assignment 1.5 + 2.8; // expression x = 2 * 5; // assignment if (a > b) cout << "a is greater than b"; // if statement ; // null statement
- C++ has a rich set of operators
- unary, binary, ternary
- Common operators can be grouped as follows:
- assignment
- arithmetic
- increment/decrement
- relational
- logical (
not
,and
,or
, etc.) - member access (
.
) - other
LHS = RHS
- RHS is an expression that is evaluated to a value
- the value of the RHS is stored to the LHS
- the value of the RHS must be type compatible with the LHS
- the LHS must be assignable
- assignment expressions is evaluated to what was just assigned
- more than one variable can be assigned in a single statement
- Addition (
+
) - Subtraction (
-
) - Multiplication (
*
) - Division (
/
) - Modulo or remainder (
%
)- only works with integers
- Increment operator
++
- Decrement operator
--
- Increments or decrements its operand by 1
- Can be used with integers, floating point types and pointers
- Below behavior is not identical
- Prefix
++num
- Postfix
num++
- see Example 2 - 5 under
section08_source_code/IncrementDecrementOperators/main.cpp
- Prefix
- Don't use the operator twice for the same variable in the same statement!!!
- i.e. DON'T:
cout << i++ << ++i
cout << i++ + ++i
- i.e. DON'T:
- C++ operations occur on same type operands
- If operands are of different types, C++ will convert one
- Important! Since it could affect calculation results
- C++ will attempt to automatically convert types (coercion)
- If it can't, a compiler error will occur
- i.e. try to assign
string
toint
- Higer vs Lower types are based on the size of the values the type can hold
- can typically convert from lower typ to higher type automatically since it will fit but the opposite may not be true
- decending order: long double, double, float, unsigned long, long, unsigned int, int
- short and char types are always converted to int
- Type Coercion: conversion of one operand to another data
- sometimes done automatically
- Promotion: conversion to a higher type
- Used in mathematical expressions
- Demotion: conversion to a lower type
- Used with assignment to lower type
- The
lower
is promoted to ahigher
- lower op higher:
2 * 5.2
- 2 is promoted to 2.0 automatically
- lower op higher:
- The
higher
is demoted to alower
- lower = higher:
int num {0}; num = 100.2;
- lower = higher:
int total_amount {100};
int total_number {8};
double average {0.0};
average = total_amount / total_number;
cout << average << endl;
// displays 12 since int operations
/*
- use static_cast<type>
- () the variable name
- total_number is automatically promoted
- do double division instead of int division
*/
average = static_cast<double>(total_amount) / total_number;
cout << average << endl;
// displays 12.5
- Compares the value of 2 expressions
- Evaluates to a Boolean (True or False, 1 or 0)
- Commonly used in control flow statements
expr1 == expr2
expr1 != expr2
100 == 200
-> Falsenum1 != num2
bool result {false};
result = (100 == 50 + 50);
result = (num1 != num2);
cout << (num1 == num2) << endl; // 0 or 1
cout << std::boolalpha; // this allow display "true or false" rather than "0 or 1"
cout << (num1 == num2) << endl; // true or false
cout << std::noboolalpha; // this revert display "true or false" back to "0 or 1"
Operator | Meaning |
---|---|
> |
greater than |
>= |
greater than or equal to |
< |
less than |
<= |
less than or equal to |
<=> |
three-way comparision (C++20) |
Operator | Meaning |
---|---|
not , ! |
negation |
and , && |
logical and |
or , ` |
- is only true if both operands are true
Expression A | Expression B | A and B or A && B |
---|---|---|
true | true | true |
true | false | false |
false | true | false |
false | false | false |
- is true if one or more operands are true
| Expression A | Expression B | A or B
or A || B
|
| --- | --- | --- |
| true | true | true |
| true | false | true |
| false | true | true |
| false | false | false |
not
has higher precedence thanand
and
has higher precedence thanor
not
is a unary operatorand
andor
are binary operators
- When evaluating a logical expression C++ stops as soon as the result is known
Operator | Example | Meaning |
---|---|---|
+= |
lhs += rhs; | lhs = lhs + (rhs); |
-= |
lhs -= rhs; | lhs = lhs - (rhs); |
*= |
lhs *= rhs; | lhs = lhs * (rhs); |
/= |
lhs /= rhs; | lhs = lhs / (rhs); |
%= |
lhs %= rhs; | lhs = lhs % (rhs); |
>>= |
lhs >>= rhs; | lhs = lhs >> (rhs); |
<<= |
lhs <<= rhs; | lhs = lhs << (rhs); |
&= |
lhs &= rhs; | lhs = lhs & (rhs); |
^= |
lhs ^= rhs; | lhs = lhs ^ (rhs); |
` | =` | lhs |
a += 1; // a = a + 1;
a /= 5; // a = a / 5;
a *= b + c; // a = a * (b + c);
cost += items * tax; // cost = cost + (items * tax);
- C++ Operator Precedence
- Use parenthesis is good practice to remove confusion