OPERATORS AND DATA TYPES IN C++, Slides of C programming

An overview of memory concepts, variable names, data types, and operators in C++. It includes a table of data types with their corresponding sizes and ranges of values. The document also covers the const modifier, arithmetic operators, decision making with equality and relational operators, and confusing equality and assignment operators. It concludes with a discussion of C++ expressions and mixed type expressions. useful for students studying introductory programming in C++.

Typology: Slides

2021/2022

Available from 08/21/2022

SamenKhan
SamenKhan 🇵🇰

231 documents

1 / 28

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Fundamentals of Programming
(FOP)
C++ Operators and Data Types
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c

Partial preview of the text

Download OPERATORS AND DATA TYPES IN C++ and more Slides C programming in PDF only on Docsity!

Fundamentals of Programming

(FOP)

C++ Operators and Data Types

Memory Concepts

  • (^) Variable names
    • (^) Correspond to actual locations in computer's memory
    • (^) Every variable has
    1. Name - integer
    2. Type - int
    3. Size
    4. Value – Recall Lecture 2A example
    • (^) When new value placed into variable, overwrites previous value: destructive operation
    • (^) Reading variables from memory: nondestructive operation

Fundamental Data Types

  • (^) Different data types - different memory size char int float Single characters and symbols Negative & positive whole numbers Real numbers with decimal point a, A---z, Z, *, $, etc 22, 129, -67, (10,000 is not allowed) 10.25, -92.

Type Name Bytes Other Names Range of Values int 4 signed – 2,147,483,648 to 2,147,483, unsigned int 4 unsigned 0 to 4,294,967, bool 1 none false or true char 1 none – 128 to 127 signed char 1 none – 128 to 127 unsigned char 1 none 0 to 255 short 2 short int, signed short int – 32,768 to 32, unsigned short 2 unsigned short int 0 to 65, long 4 long int, signed long int – 2,147,483,648 to 2,147,483, unsigned long 4 unsigned long int 0 to 4,294,967, long long 8 none – 9,223,372,036,854,775,808 to 9,223,372,036,854,775, unsigned long long 8 none 0 to 18,446,744,073,709,551, float 4 none +/-3.4E +/- 38 double 8 none +/-1.7E +/- 308 long double same as double none same as double

Also read http://msdn.microsoft.com/en-us/library/s3f49ktz(v=vs.90).aspx

What if you try to change a const?

  • (^) The compiler will complain if your code tries to modify a const variable: const int temp = 100; … temp = 21; Error: l-value specifies const object

Arithmetic Operators

  • (^) Arithmetic calculations
    • (^) *
      • (^) Multiplication
    • (^) /
      • (^) Division
      • (^) Integer division truncates remainder
        • (^7) / 5 evaluates to 1
    • (^) %
      • (^) Modulus operator returns remainder
        • (^7) % 5 evaluates to 2 Integer / Integer will also be INTEGER

Expression Value

Arithmetic Operators

  • (^) 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 Operator(s) (^) • (^) Operators applied from left to rightOperation(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

Modulus

Evaluated second. If there are several, they re

evaluated left to right.

+ or - Addition

Subtraction

Evaluated last. If there are several, they are

evaluated left to right.

Decision Making: Equality and Relational Operators Standard algebraic equality operator or relational operator C++ equality or relational operator Example of C++ condition Meaning of C++ condition Relational operators

> x > y x is greater than^ y < < x < y x is less than y  >=^ x >= y^ x^ is greater than or equal to^ y<=^ x <= y^ x^ is less than or equal to^ y Equality operators = == x == y x is equal to^ y  !=^ x != y^ x^ is not equal to^ y

Precedence and Associativity

  • (^) Rules of Precedence control the order of evaluation of operators with different precedence. - (^) A high precedence means an operator is evaluated (applied) before any lower precedence operators.
  • (^) Rules of Associativity control the order of evaluation of operators with same precedence. ‒ (^) Operators that have the same precedence are evaluated from left to right mostly but NOT always. ‒ (^) For example 24+5*

Expression Value

Expression Value

8 / 0 Error

8.0 / 0.0 Error

8.0 % 5 Error

8 % 0 Error

26 if ( num1 < num2 ) 27 cout << num1 << " is less than " << num2 << endl; 28 29 if ( num1 > num2 ) 30 cout << num1 << " is greater than " << num2 << endl; 31 32 if ( num1 <= num2 ) 33 cout << num1 << " is less than or equal to " 34 << num2 << endl; 35 36 if ( num1 >= num2 ) 37 cout << num1 << " is greater than or equal to " 38 << num2 << endl; 39 40 return 0 ; // indicate that program ended successfully 41 42 } // end function main Enter two integers, and I will tell you the relationships they satisfy: 22 12 22 is not equal to 12 22 is greater than 12 22 is greater than or equal to 12

Statements may be split over

several lines.

Enter two integers, and I will tell you

  • 8 /
  • 8 /
  • 8 %
  • 8 %
  • 0 % - 10 / 2 * - 10 / 3 * - 10 % 3 - 4 / 2 - - 2 + 3 *
    • 5.0 * 2.0 / 4.0 * 2.0 5.
  • 5.0 * 2.0 / (4.0 * 2.0) 1. - (2 + 3) * - 5 * 2 / 4 * - 10 - 10 / 3 *
  • the relationships they satisfy:
  • 7 is equal to
  • 7 is less than or equal to
  • 7 is greater than or equal to