Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Fundamental-1

Similar presentations


Presentation on theme: "Programming Fundamental-1"— Presentation transcript:

1 Programming Fundamental-1
Instructor Name: Muhammad Safyan Lecture-2

2 Lecture Outline What is language? What is programming?
Why programming is important? Skill needed for programming How to write the program (Basic Points) Conclusion 2

3 Variable Variable: A variable is used to store a piece of data for processing. It is called variable because you can change the value stored.  A variable is a named storage location, that stores a value of a particular data type   3

4 Variable In a program a variable has:
Name: A variable has a name (or identifier), e.g.,  radius, area, age, height. Name is needed to uniquely identify each variable. Type: A variable has a type. Examples of type are, int: for integers (whole numbers) such as 123 and -456; double: for floating-point or real numbers such as 3.1416, -55.66, having a decimal point and fractional part. 3. Size: may take 2 byte s, 4 bytes or 8 bytes in memory depends upon compiler. 4. Value: radius=5 , age=34

5 Variable Declaration Rules
C++ imposes the following rules on variable: An identifier is a sequence of characters, of up to a certain length (compiler-dependent, typically 255 characters), comprising of : uppercase lowercase letters (a-z, A-Z), digits (0-9), underscore"_". An identifier must begin with a letter or underscore. Prohibition: White space (blank, tab, new-line) and other special characters (such commas, etc.) are not allowed. 5

6 Prohibition It cannot begin with a digit.
An identifier cannot be a reserved keyword or a reserved literal (e.g. int, double, if, else, for). Identifiers are case-sensitive. E.g. A rose is NOT a Rose, and is NOT a ROSE. 6

7 Some Key Words of C++ main if else while do for

8 Recommendation for Variable
varibales beginning with an underscore are typically reserved for system use. So not recommended for program varibales It is important to choose a name that is self-descriptive and closely reflects the meaning of the variable, e.g., numberOfStudents or numStudents. Do not use meaningless names like a, b, c, d, i, j, k, i1, j99. Avoid single-alphabet names, which is easier to type but often meaningless, unless they are common names like x, y, z for coordinates, i for index. 8

9 Recommendation for Variable
It is perfectly okay to use long names of says 30 characters to make sure that the name accurately reflects its meaning! Use singular and plural nouns prudently to differentiate between singular and plural variables.  For example, you may use the variable row to refer to a single row number and the variable rows to refer to many rows. 9

10 Variable X Variable

11 Pic of the memory 25 10323 name of the variable

12 Small post box X

13 Variable Fundamental Types
Integers: used to store numerical values(signed or unsigned). Characters: used to store Characters ‘A’ to ‘z’. Every alphanumeric digit and or symbol encoded in single quotes in considered a character. Floating-point: used store the decimal point numbers. Boolean: A special type called bool , which takes a value of either true or false. Characters (e.g., 'a', 'Z', '0', '9') are encoded in ASCII into integers, and kept in type char. For example, character '0' is 48 (decimal) or 30H (hexadecimal); character 'A' is 65 (decimal) or41H (hexadecimal); character 'a' is 97 (decimal) or 61H (hexadecimal). Take note that the type char can be interpreted as character in ASCII code, or an 8-bit integer. Unlike int or long, which is signed,char could be signed or unsigned, depending on the implementation. You can use signed char or unsigned char to explicitly declare signed or unsigned char. 13

14 14

15 15

16  Fundamental Types The sizeof  Operator: used to determined to size of compiler. using namespace std; #include<iostream> #include<conio.h> main() { int i; long j; long long k; cout<<sizeof(i); cout<<sizeof(j); cout<<sizeof(k); getch(); } 16

17 Char Data type The data type char is the smallest integral data type
Range is (128 to 127), char data type can represent every key on your keyboard. Enclosed in single quotation marks. E.g. 'A', 'a', '0', '*', '+', '$', '&', ‘ ‘ Several character set are in use Most commonly used is :American Standard Code for Information Interchange (ASCII), Extended Binary- Coded Decimal Interchange Code (EBCDIC). The ASCII character set has 128 values. Characters (e.g., 'a', 'Z', '0', '9') are encoded in ASCII into integers, and kept in type char. For example, character '0' is 48 (decimal) or 30H (hexadecimal); character 'A' is 65 (decimal) or41H (hexadecimal); character 'a' is 97 (decimal) or 61H (hexadecimal). Take note that the type char can be interpreted as character in ASCII code, or an 8-bit integer. Unlike int or long, which is signed,char could be signed or unsigned, depending on the implementation. You can use signed char or unsigned char to explicitly declare signed or unsigned char. 17

18 Char Data type 65 represents 'A', and the value 43 represents '+'. 18
Characters (e.g., 'a', 'Z', '0', '9') are encoded in ASCII into integers, and kept in type char. For example, character '0' is 48 (decimal) or 30H (hexadecimal); character 'A' is 65 (decimal) or41H (hexadecimal); character 'a' is 97 (decimal) or 61H (hexadecimal). Take note that the type char can be interpreted as character in ASCII code, or an 8-bit integer. Unlike int or long, which is signed,char could be signed or unsigned, depending on the implementation. You can use signed char or unsigned char to explicitly declare signed or unsigned char. 18

19  Expressions An expression is a combination of operators and operands , that can be evaluated to yield a single value of a certain type. For example, 1 + 2 * // give int 7 sum +number (variables or literal values) 19

20 Arithmetic operators Plus + Minus - Multiply * Divide / Modulus %
Binary operator: have two Operands Unary Operator: have one operands

21 Arithmetic operators i + j x * y a / b a % b

22 % = Remainder 5 % 2 = 1 2 % 2 = 0

23 4 / 2 = 2 5 / 2 = ? Integral Expression
Floating Point Expression: All operands have floating point data type. Mixed Expression: An expression that has operands of different data types is called a mixed expression

24 Mixed Expression Rules apply when evaluating a mixed expression:
1. When evaluating an operator in a mixed expression: a. If both operands have same data type then result will be of operand type b. If the operator has both types of operands (that is, one is an integer and the other is a floating-point number), then during calculation the integer is changed to a floating-point number with the decimal part of zero and the operator is evaluated. The result is a floating point number.

25 Order Precedence Highest: ( ) Next: * , / , % Lowest: + , -
Processing order in left to right Highest: ( ) Next: * , / , % Lowest: + , -

26  Assignment (=) An assignment statement: assigns a literal value (of the RHS) to a variable (of the LHS); or evaluates an expression (of the RHS) and assign the resultant value to a variable (of the LHS). The RHS shall be a value; and the LHS shall be a variable (or memory address). 26

27 Variable is the name of a location in
the memory e.g. x= 2;

28 Assignment Operator = x = 2 X

29 Assignment Operator L.H.S = R.H.S. X+ 3 = y + 4 Wrong Z = x +4
x +4 = Z Wrong

30 10 X X = 10 ; X = 30 ; 30 X

31 X = X + 1; X 10 + 1 = 11 X

32 #include <iostream.h>
main ( ) { int x ; int y ; int z ; x = 10 ; y = 20 ; z = x + y ; cout << " x = " ; cout << x ; cout << " y = " ; cout << y ; cout << " z =x + y = " ; cout << z ; }

33 Constant const dataType identifier = value;
Named constant: A memory location whose content is not allowed to change during program execution Need constant in many real life scanerios For example, the pay rate is usually the same for all part-time employees. A conversion formula that converts inches into centimeters is fixed, because 1 inch is always equal to 2.54 centimeters Value of pi const dataType identifier = value;

34 Constant const dataType identifier = value;
const double CONVERSION = 2.54; const int NO_OF_STUDENTS = 20; const char BLANK = ' '; const double PAY_RATE = 15.75;


Download ppt "Programming Fundamental-1"

Similar presentations


Ads by Google