Computer Engineering 1 st Semester Dr. Rabie A. Ramadan 3
2 C++ Names The name of a variable could: Starts with an underscore “_” or a letter, lowercase or uppercase, e.g. _Students, pRice Can include letters, underscore, or digits. Examples are: keyboard, total_grade, _Score_Side1 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)
3 Reserved Words
4 Variables and Their Data Types The amount of memory space necessary to store a variable is also referred to as a data type.
5
6 Variables and Their Data Types char 8 bits Signed char: -128 +127 Unsigned char 0 255 Escape sequences are characters
Escape characters 7
Examples 8
9 Variables and Their Data Types 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
10 Example
11 Practice -- what is the o/p
12 Practice -- what is the o/p
13 O/P
14 Variables and Their Data Types Floating points Real numbers ex 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;
Example 15
16 Variables and Their Data Types 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, stringName, delimiter ) Saves the data when the delimiter is entered
Example 17
Example (cont.) 18
19 What are constants? const PI = 3.14; #define PI 3.14
20 Operators and Operands “Don't cut the tree that shades you.”
21 Operators Arithmetic : Unary Operators sign (+ or -) Algebraic operators + Addition - Subtraction * Multiplication / Division % Reminder ++ Increment -- Decrement += Add to the current content -= subtract from the current content *=, /=,..
22 Operators Logical operators o/p true or false ! NOT && AND || OR > greater than < less than >= greater than <= less than == equal
23 Operators
24 Operators Conditional operator (?) :
25 Operators Explicit type casting operator Converts a datum of a given type to another.
26 Operator Precedence Operator classificationOperators parentheses( ) postfix operators[] x++ x-- unary operators++x --x +x -x ! creation or castnew (type)x multiplicative* / % additive+ - relational >= <= equality (boolean)= = != logical AND&& logical OR|| assignment=
27 Example
28 Example
29 Example
30 Control Statement
31 Flow of the Control
32 Control Statements Ways for a programmer to control what pieces of the program are to be executed at certain times. branching statements and loops.
33 Branching Statement
34 Example
35 Nesting Branch
36
37 Switch Statement
38 Switch Statement
39 Reading Materials Chapters 1, 2 and 3, 5 (till 5.3).