You are on page 1of 23

Programming

Fundamentals
Lecture 5

Copyright © 2013 The McGraw-Hill Companies, Inc.


Permission required for reproduction or display. 1
Data Types

• The types of data permitted, and the operations allowed for


each type are referred to as a data type.
set of all integer (whole
• Built-in data types/ primitive types, provided as part of the C+
+ compiler.

• C++ allows performing only certain operations on certain data types.

2
Built-in Data Types

Copyright © 2013 The McGraw-Hill Companies, Inc.


Permission required for reproduction or display. 3
Integer Data Types

• C++ provides nine built-in integer


data types.

• The essential difference between


these integer data types is the
amount of storage used for each
type

• int, char, and bool are the most


important and commonly used.

4
Data Type Storage

Copyright © 2013 The McGraw-Hill Companies, Inc.


Permission required for reproduction or display. 5
The ‘int’ Data Type

whole numbers, mathematically known as integers

Valid:

Invalid :

4 bytes for the int data type, which restricts the values used to
represent integers from
-2,147,483,648 to 2,147,483,647

Copyright © 2013 The McGraw-Hill Companies, Inc.


Permission required for reproduction or display. 6
The ‘char’ Data Type

• Store single characters, including


• Letters of the alphabet (uppercase and lowercase),
• Digits 0 through 9
• Special symbols, such as +$.,- and !.

• A character value is any single letter, digit, or special symbol enclosed


by single quotation marks, as shown in these examples:

Copyright © 2013 The McGraw-Hill Companies, Inc.


Permission required for reproduction or display. 7
8
The bool Data Type

• Represent Boolean (logical) data, so it’s restricted to one of two


values: true or false

Copyright © 2013 The McGraw-Hill Companies, Inc.


Permission required for reproduction or display. 9
Floating-Point Types

• A floating-point number, more commonly known as a real number,


can be the number 0 or any positive or negative number containing a
decimal point
• Valid

• Invalid

Copyright © 2013 The McGraw-Hill Companies, Inc.


Permission required for reproduction or display. 10
Floating-Point Types

A float literal is indicated by appending an f or F to the number, and a long double is


created by appending an l (lowercase L, not the numeral 1) or L to the number. In the
absence of these suffixes, a floating-point number defaults to a double. For
example, take a look at the following: 9.234 indicates a double literal 9.234F
indicates a float literal 9.234L indicates a long double literal

Copyright © 2013 The McGraw-Hill Companies, Inc.


Permission required for reproduction or display. 11
Arithmetic Operations
( 'A' + 1 results in the character 'B'.)

Binary operators, which means the operator requires two


operands to produce a result

A simple binary arithmetic expression consists of a binary


operator connecting two literal values in this form:
literalValue operator literalValue
Copyright © 2013 The McGraw-Hill Companies, Inc.
Permission required for reproduction or display. 12
Arithmetic Operations

#include<iostream>
using namespace std;
int main()
{
cout << (6 + 15);
cout << “ The sum of 6 and 15 is ” << (6 + 15);
return 0;
}

21 The sum of 6 and 15 is 21

Copyright © 2013 The McGraw-Hill Companies, Inc.


Permission required for reproduction or display. 13
Expression Types

• An expression containing only integer values as operands is called an


integer expression,

• an expression containing only floating-point values as operands is called


a floating-point expression

• An expression containing integer and floating-point values is called a


mixed-mode expression.

14
Expression Types
Following rules are important:
• If both operands are integers, the result of the operation is an integer.
• If one operand is a real value, the result of the operation is a double-precision
value.

Examples

10+9 10.12+2 10/3 10.0/3 10/3.0

Answers:
19 12.12 3 3.33 3.33

Can you tell the answer of 10.0/3.0??


Copyright © 2013 The McGraw-Hill Companies, Inc.
15
Permission required for reproduction or display.
Integer Division

• Modulus operator (%)


This operator captures the remainder when an integer is divided by an
integer

Copyright © 2013 The McGraw-Hill Companies, Inc.


Permission required for reproduction or display. 16
Negation

• In addition to binary operators, C++ provides unary operators,


which operate on a single operand.

• One of these unary operators uses the same symbol as binary


subtraction (-).

• With this unary operator, the minus sign in front of a single


numerical value negates (reverses the sign of) the number.

Copyright © 2013 The McGraw-Hill Companies, Inc.


Permission required for reproduction or display. 17
Summary

18
Operator Precedence and
Associativity
Following certain rules when writing expressions containing more than
one arithmetic operator:

• Two binary operator symbols must never be placed side by side. For
example, 5 * % 6 is invalid because two operators, * and %, are
placed next to each other.
• Parentheses can be used to form groupings, and all expressions
enclosed in parentheses are evaluated first.
• When parentheses are included within parentheses, expressions in
the innermost parentheses are always evaluated first.
• Parentheses can’t be used to indicate multiplication; instead, the
multiplication operator, *, must be used. Example: Can’t write (2)(3),
instead it should be written as 2*3.

19
Precedence

• There are three levels of precedence:


• P1
All negations are done first.

• P2
Multiplication, division, and modulus operations are computed next
(evaluated left to right if multiple operators)

• P3
Addition and subtraction are computed last
(evaluated left to right if multiple operators)

20
Associativity

• 6.0 * 6 / 4 ???
The answer is 9.0 because C++’s operators use the same
associativity as in general mathematics, which evaluates
multiplication from left to right, as rule P2 indicates.

• 35 / 7 % 3 * 4??

Answer is : 5%3*4
2*4
8

Copyright © 2013 The McGraw-Hill Companies, Inc.


Permission required for reproduction or display. 21
• 8 + 5 * 7 % 2 * 4 = ?????

Summary • 8+2*(3/2.0)+10-5*2=??

8+2* 1.5 +10 -5 *2


8+3.0+10-10
11.0

Copyright © 2013 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 22
Practice Task
Expressions Answers

21.3
10.0 +15/2 +4.3

21.8
10.0 +15.0/2 +4.3

3.2 + (6.1/5) 4.42

2.0* 3.0/12.0 *8.0 /4.0 1.0

Copyright © 2013 The McGraw-Hill Companies, Inc.


23
Permission required for reproduction or display.

You might also like