Expressions
Expressions
2
Data Types and Expressions
• 2 and 2.0 are not the same number
• A whole number such as 2 is of type int
• A real number such as 2.0 is of type double
3
Writing Integer & Double Constants
• Type int does not contain decimal points
• Examples: 34 45 1 89
• Type double can be written in two ways
• Simple form must include a decimal point (We will use the simple form)
• Examples: 34.1 23.0034 1.0 89.9
• The e stands for exponent and means “multiply by 10 to the power that follows.”
• Number left of e does not require a decimal point
• Exponent cannot contain a decimal point 4
Type Compatibilities
• In general store values in variables of the same type
• This is a type mismatch:
int intVariable;
intVariable = 2.99;
• If your compiler allows this, intVariable will most likely contain the value 2,
not 2.99
5
int double (part 1)
• Variables of type double should not be assigned to variables of type
int
int intVariable;
double doubleVariable;
doubleVariable = 2.00;
intVariable = doubleVariable;
6
int double (part 2)
• Integer values can normally be stored in variables of type double
double doubleVariable;
doubleVariable = 2;
7
Arithmetic
• Arithmetic is performed with operators
• + for addition
• - for subtraction
• * for multiplication
• / for division
8
Results of Operators
• Arithmetic operators can be used with any numeric type
• An operand is a number or variable used by the operator
• Result of an operator depends on the types of operands
• If both operands are int, the result is int
• If one or both operands are double, the result is double
9
Division of Doubles
• Division with at least one operator of type double produces the expected results.
• quotient = 1.6666…
• Result is the same if either dividend or divisor is of type int
10
Division of Integers
• Be careful with the division operator!
• int / int produces an integer result
(true for variables or numeric constants)
• Example: cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout << "The price is "
<< price << endl;
12
Try: Printing decimal numbers nicely
Task: Modify the program so that the number is printed as 78.83838
#include <iostream>
using namespace std;
int main()
{
//To specify fixed point notation
cout.setf(ios::fixed);
return 0;
}
13
Integer Remainders
• % operator gives the remainder from integer division
14
Arithmetic Expressions
• Use spacing to make expressions readable
• Which is easier to read?
x+y*z or x + y * z
• Precedence rules for operators are the same as used in your algebra classes
• Use parentheses to alter the order of operations
x + y * z ( y is multiplied by z first)
(x + y) * z ( x and y are added first)
15
16
Task
• Write a program that reads a, b, c and calculates and prints the
discriminant.
17
Operator Shorthand
• Some expressions occur so often that C++ contains to shorthand operators for
them
• All arithmetic operators can be used this way
• += count = count + 2; becomes
count += 2;
• *= bonus = bonus * 2; becomes
bonus *= 2;
• /= time = time / rushFactor; becomes
time /= rushFactor;
• %= remainder = remainder % (cnt1+ cnt2); becomes
remainder %= (cnt1 + cnt2);
18
Other Number Types
• Various number types have different memory requirements
• More precision requires more bytes of memory
19
float notSoBigNumber;
Type char
• Computers process character data too
• char short for character
• Can be any single character from the keyboard
21
char int
• The following actions are possible but generally not recommended!
• It is possible to store char values in integer variables
int value = 'A';
value will contain an integer representing 'A'
22
Type string
• string is a class, different from the
primitive data types discussed so far
• Use double quotes around the text
to store into the string variable
• Requires the following be added
to the top of your program:
#include <string>
• To declare a variable of type string:
string name = "Dana Dghaym”;
23
Type bool
• Boolean expressions evaluate to one of the two values: true or false
• Boolean expressions are used in branching and looping statements
• Example: bool b = true
• The following actions are possible but generally not recommended!
• Values of type bool can be assigned to int variables
• True is stored as 1
• False is stored as 0
• Values of type int can be assigned to bool variables
• Any non-zero integer is stored as true
• Zero is stored as false
24
References
• Problem Solving with C++ - Walter Savitch
• Ch 2 - Introduction to Computers and C++
Programming
• Section: 2.3
25