0% found this document useful (0 votes)
21 views12 pages

Lecture 06

The document discusses expressions and data types in C++. It covers arithmetic operators, order of operations, integer and floating point division, and using cout to output values and different data types to the screen.

Uploaded by

Saad Qayyum
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views12 pages

Lecture 06

The document discusses expressions and data types in C++. It covers arithmetic operators, order of operations, integer and floating point division, and using cout to output values and different data types to the screen.

Uploaded by

Saad Qayyum
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

1

Lecture 6

Expressions and Data-types


C++ Output (with 'cout')
12

EXPRESSIONS
13

Arithmetic Operators
• Addition, subtraction, multiplication work as expected for
both integer and floating point types
• Modulus is only defined for integers
Operator Operation
+ Addition
- Subtraction
* Multiplication
/ Division
(Integer vs. Double division)
% Modulus (remainder) 10 % 3 =
[for integers only] 17 % 10 =
14

Precedence
• Order of operations/
evaluation of an expression
• Top Priority = highest
(done first)
• Notice operations with the
same level or precedence
usually are evaluated left to
right (explained at bottom)
• Evaluate:
– 2*-4-3+5%2;
• Tips:
– Use parenthesis to add clarity
– Add a space between literals January 2007 v2.2. Copyright °c 2007 Joseph H. Silverman
Permission is granted to make and distribute copies of this card pro-

( 2 * - 4) - 3 + ( 5 % 2) vided the copyright notice and this permission notice are preserved on
all copies.
Send comments and corrections to J.H. Silverman, Math. Dept., Brown
Univ., Providence, RI 02912 USA. [email protected]
15

Division
• Computers perform division differently based on the
type of values used as inputs
• Integer Division:
– When dividing two integral values, the result will also be
an integer (any remainder/fraction will be dropped)
– 10 / 4 = 2 52 / 10 = 5 6/7=0
• Floating-point (Double) & Mixed Division
– 10.0 / 4.0 = 2.5 52.0 / 10 = 5.2 6 / 7.0 = 0.8571
– Note: If one input is a double, the other will be promoted
temporarily to compute the result as a double
16

Exercise Review
• Evaluate the following:
25 / 3
20 - 12 / 4 * 2
3 - 15 % 7
18.0 / 4
28 - 5 / 2.0

Exercises from: D.S. Malik, C++ Programming, 5th Ed., Ch. 2, Q6.
17

Using 'cout'…

SIMPLE C++ OUTPUT


18

Output From Your Program


• To see the output in C++ we
need to explicitly tell the
computer to output the value
using 'cout' Performing computation is To output a result to the
like having a thought. No screen in C++ (i.e. "write it
– So what happens to the result of output is generated unless down") we use the 'cout'
you explicitly write it down. command
12*3 on the first line?
• Note: 'endl' stands for / / iostream allows access to ' c o u t '
#include <iostream>
end-line and causes the cursor using namespace std;

/ / Execution always starts at the main() function


to move to the next line of the i n t main()
{
screen 12 * 3; / / No result printed

cout << 12 * 3 << endl; / / 36 printed

return 0;
}

This Photo by Unknown Author is licensed under CC BY-SA-NC


This Photo by Unknown Author is licensed under CC BY-SA-NC
19

Printing Different Values & Types


/ / iostream allows access to ' c o u t '
#include <iostream>
• 'cout' requires appropriate use of using namespace std;

separators between consecutive


/ / Execution always starts at the main() function
values or different types of values i n t main()
{
• 'cout' does not add spaces between cout << 345 754 << endl; / / Bad
cout << 345 << 754 << endl; / / Better, but no spaces cout
consecutive values; you must do so << 345 << " " << 754 << endl; / / Best
return 0;
explicitly }

– Since text strings are a different


/ / iostream allows access to ' c o u t '
value we must separate it with the #include <iostream>
using namespace std;
'<<' operator
/ / Execution always starts at the main() function
• Generally good practice to give some i n t main()
{
descriptive text with your numeric cout << "3 dozen i s " << 3*12 << " items." << endl;
output cout << "There are " << 60*24*365 << " minutes";
– Note: You may divide up output over cout << " i n a year." << endl;
return 0;
multiple 'cout' statements. Unless an }
'endl' or '\n' is used, the next 'cout'
statement will resume where the last Output:
3 dozen i s 36 items.
one left off There are 525600 minutes i n a year.
20

SOLUTIONS
21

You're Just My Type


• Indicate which constants are matched with
the correct type.
Constant Type Right / Wrong
4.0 int double (.0)
5 int int
'a' string char
"abc" string C-string
5. double float/double (. = non-integer)
5 char Int…but if you store 5 in a char
variable it'd be okay (char = some
number that fits in 8-bits/1-byte
"5.0" double C-string
'5' int char
22

Exercise Review
• Evaluate the following:
– 25 / 3 = 8
– 20 - 12 / 4 * 2 = 14
– 3 - 15 % 7 = 2
– 18.0 / 4 = 2.5
– 28 - 5 / 2.0 = 25.5

You might also like