Computer Engineering 1 st Semester Dr Rabie A
Computer Engineering 1 st Semester Dr. Rabie A. Ramadan http: //rabieramadan. org 3
C++ Names l • • • The name of a variable could: Starts with an underscore “_” or a letter, lowercase or uppercase, e. g. _Students, p. Rice Can include letters, underscore, or digits. Examples are: keyboard, total_grade, _Score_Side 1 Cannot include special characters such as !, %, ], or $ Cannot include an empty space Cannot be any of the reserved words Should not be longer than 32 characters (although allowed) 2
Reserved Words 3
Variables and Their Data Types l The amount of memory space necessary to store a variable is also referred to as a data type. 4
5
Variables and Their Data Types l char l Signed char: l l • • 8 bits -128 +127 Unsigned char 0 255 Escape sequences are characters 6
Escape characters 7
Examples 8
Variables and Their Data Types l l l • • • Integers – 32 bits or 4 bytes Holds numerical data Signed or Unsigned Short integers 16 bits or 2 bytes Signed or Unsigned Long integers To enforce positive numbers 32 bit long 9
Example 10
Practice -- what is the o/p 11
Practice -- what is the o/p 12
O/P 13
Variables and Their Data Types l l l • • Floating points Real numbers ex. 5. 5 4 bytes Declared as : float x; Floating number with double precision 8 bytes Declared as : double x; Larger storage 10 bytes Declared as: long double x; 14
Example 15
Variables and Their Data Types l l • • String Group of characters No limit Declared as: string name; getline() function Takes the data from external source and save it into a string variable Ex. getline(cin, name); cin is to notify the compiler that the data is coming from external source Ex. getline( cin, name, ‘? ’) getline( cin, string. Name, delimiter ) Saves the data when the delimiter is entered 16
Example 17
Example (cont. ) 18
What are constants? const PI = 3. 14; #define PI 3. 14 19
Operators and Operands “Don't cut the tree that shades you. ” 20
Operators l • • Arithmetic : • • • Unary Operators sign (+ or -) Algebraic operators + Addition - Subtraction * Multiplication / Division % Reminder ++ Increment -- Decrement += Add to the current content -= subtract from the current content *= , /= , . . 21
Operators l • • • Logical operators o/p ! && || > < >= <= == true or false NOT AND OR greater than less than equal 22
Operators 23
Operators Conditional operator (? ) : 24
Operators l Explicit type casting operator • Converts a datum of a given type to another. 25
Operator Precedence Operator classification Operators parentheses () postfix operators [] x++ x-- unary operators ++x --x +x -x ! creation or cast new (type)x multiplicative */% additive +- relational < > >= <= equality (boolean) == logical AND && logical OR || assignment = != 26
Example 27
Example 28
Example 29
Control Statement 30
Flow of the Control 31
Control Statements l Ways for a programmer to control what pieces of the program are to be executed at certain times. l branching statements and loops. 32
Branching Statement 33
Example 34
Nesting Branch 35
36
Switch Statement 37
Switch Statement 38
Reading Materials Chapters 1, 2 and 3, 5 (till 5. 3). 39
- Slides: 39