Lecutre_1
.pdfMemory Concepts
std::cin >> integer1;
Assume user entered 45
std::cin >> integer2;
Assume user entered 72
sum = integer1 + integer2;
Fundamental data types
When programming, we store the variables in our computer's memory, but the computer has to know what kind of data we want to store in them, since it is not going to occupy the same amount of memory to store a simple number than to store a single letter or a large number, and they are not going to be interpreted the same way.
The memory in our computers is organized in bytes. A byte is the minimum amount of memory that we can manage in C++
Specifiers
Specifiers modify the meanings of the basic built-in types and expand them to a much larger
set. There are four specifiers:
Long
Short Signed
Unsigned
Fundamental data types in C++
*The values of the columns Size and Range depend on the system the program is compiled for. The values shown above are those found on most 32-bit systems.
Arithmetic operators
Arithmetic operators
Integer division
-yields an integer quotient
-fractional part is discarded (i.e., truncated) no rounding occurs
Example:
int a = 7, b = 4;
cout<<a/b; 1
Modulus operator (%)
-yields the remainder after integer division
-can be used only with integer operands *
Example:
int a = 7, b = 4; |
3 |
|
|
cout<<a%b; |
|
*Attempting to use the modulus operator (%) with noninteger operands is a compilation error.
Rules of operator precedence
Operators in parentheses evaluated first
Nested/embedded parentheses
Operators in innermost pair first
Multiplication, division, modulus applied next
Operators applied from left to right
Addition, subtraction applied last
Operators applied from left to right
|
|
|
Operator(s) |
Operation(s) |
Order of evaluation (precedence) |
|
|
|
() |
Parentheses |
Evaluated first. If the parentheses are nested, the |
|
|
expression in the innermost pair is evaluated first. If |
|
|
there are several pairs of parentheses “on the same level” |
|
|
(i.e., not nested), they are evaluated left to right. |
*, /, or % |
Multiplication Division |
Evaluated second. If there are several, they re |
|
Modulus |
evaluated left to right. |
+ or - |
Addition |
Evaluated last. If there are several, they are |
|
Subtraction |
evaluated left to right. |
Decision Making: Equality and
Relational Operators
if structure
Make decision based on truth or falsity of condition
-If condition met, body executed
-Else, body not executed
Equality and relational operators
Equality operators
- Same level of precedence
Relational operators
- Same level of precedence
Associate left to right
Equality and relational operators