0% found this document useful (0 votes)
22 views25 pages

Expressions

The document covers the basics of C++ data types and expressions, focusing on numeric and non-numeric types, arithmetic expressions, and type compatibility. It explains the differences between integer and double types, how to perform arithmetic operations, and the importance of type compatibility when assigning values. Additionally, it introduces character and string types, as well as boolean expressions and their usage in programming.

Uploaded by

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

Expressions

The document covers the basics of C++ data types and expressions, focusing on numeric and non-numeric types, arithmetic expressions, and type compatibility. It explains the differences between integer and double types, how to perform arithmetic operations, and the importance of type compatibility when assigning values. Additionally, it introduces character and string types, as well as boolean expressions and their usage in programming.

Uploaded by

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

C++ Basics

Data Types & Expressions

Dr. Dana Dghaym


[email protected]
Spring 2024
Learning Objectives
• C++ Data Types
• Numeric & non-numeric types
• Aritmetic Expressions
• Type compatibility

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

• Numbers of type int are stored as exact values

• Numbers of type double may be stored as approximate values due to limitations


on number of significant digits that can be represented

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

• Floating Point Notation (Scientific Notation)


• Examples: 3.41e1 means 34.1
3.67e17 means 367000000000000000.0
5.89e-6 means 0.00000589

• 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;

• If allowed, intVariable contains 2, not 2.00

6
int  double (part 2)
• Integer values can normally be stored in variables of type double

double doubleVariable;
doubleVariable = 2;

• doubleVariable will contain 2.0

7
Arithmetic
• Arithmetic is performed with operators
• + for addition
• - for subtraction
• * for multiplication
• / for division

• Example: storing a product in the variable totalWeight

totalWeight = oneWeight * numberOfBars;

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.

double divisor, dividend, quotient;


divisor = 3;
dividend = 5;
quotient = dividend / divisor;

• 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)

int dividend, divisor, quotient;


dividend = 5;
divisor = 3;
quotient = dividend / divisor;

• The value of quotient is 1, not 1.666…


• Integer division does not round the result, the fractional part is discarded!
11
Showing Decimal Places
• cout includes tools to specify the output of type double

• To specify fixed point notation


• setf(ios::fixed)
• To specify that the decimal point will always be shown
• setf(ios::showpoint)
• To specify that two decimal places will always be shown
• precision(2)

• 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);

//To specify that the decimal point will always be shown


cout.setf(ios::showpoint);

//To specify that two decimal places will always be shown


cout.precision(2);
cout << 78.838383838 << endl;

return 0;
}
13
Integer Remainders
• % operator gives the remainder from integer division

• int dividend, divisor, remainder;


dividend = 5;
divisor = 3;
remainder = dividend % divisor;

• The value of remainder is 2

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

Floating Point types


Integer types
• long double (often 10 bytes)
• long or long int (often 4 bytes)
• Declares floating point numbers with up to
long bigTotal;
19 significant digits
long int bigTotal;
long double bigNumber;

• short or short int (often 2 bytes) • float (often 4 bytes)


short smallTotal; • Declares floating point numbers with up to 7
short int smallTtotal; significant digits

19
float notSoBigNumber;
Type char
• Computers process character data too
• char short for character
• Can be any single character from the keyboard

• To declare a variable of type char: char letter;


• Character constants are enclosed in single quotes
char letter = 'a';
• Strings of characters, even if only one character is enclosed in double quotes
• "a" is a string of characters containing one character
• 'a' is a value of type character
20
Reading Character Data
• cin skips blanks and line breaks looking for data
• The following reads two characters but skips
any space that might be between

char symbol1, symbol2;


cin >> symbol1 >> symbol2;

• User normally separate data items by spaces


J D
• Results are the same if the data is not
separated by spaces
JD

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'

• It is possible to store int values in char variables


char letter = 65;

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

You might also like