Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Computer Programming. MS Sadia Ejaz CIIT ATTOCK Identifiers The identifiers are the names used to represent variable, constants, types,

Similar presentations


Presentation on theme: "Introduction to Computer Programming. MS Sadia Ejaz CIIT ATTOCK Identifiers The identifiers are the names used to represent variable, constants, types,"— Presentation transcript:

1 Introduction to Computer Programming

2 MS Sadia Ejaz CIIT ATTOCK Identifiers The identifiers are the names used to represent variable, constants, types, functions and labels in the program. The identifiers are the names used to represent variable, constants, types, functions and labels in the program. An identifier in C++ may consist of 31 characters. An identifier in C++ may consist of 31 characters.

3 MS Sadia Ejaz CIIT ATTOCK Types of Identifiers Standard Identifiers Standard Identifiers  A type of identifier that has special meaning in C++ is known standard identifier.  For example. cout, cin, etc. User-defined Identifiers User-defined Identifiers  The type of identifier that is defined by the programmer to access memory location is known as user-defined identifier.  For example, marks, age, etc.

4 MS Sadia Ejaz CIIT ATTOCK In a program every variable has Name Type Size Value The variables having a name, type and size are just empty boxes. They are useless until we put some value in them. To put some value in these boxes is known as assigning values to variables. In C++ language, we use assignment operator for this purpose.

5 MS Sadia Ejaz CIIT ATTOCK What is a data type? When we wish to store data in a C++ program, such as a whole number or a character, we have to tell the compiler which type of data we want to store. The data type will have characteristics such as the range of values that can be stored and the operations that can be performed on variables of that type.

6 MS Sadia Ejaz CIIT ATTOCK Fundamental types C++ provides the following fundamental built- in data types: Boolean, character, integer and floating-point. It also enables us to create our own user- defined data types using enumerations and classes.

7 MS Sadia Ejaz CIIT ATTOCK Boolean Type The Boolean type can have the value true or false. For example: bool isEven = false; bool keyFound = true; If a Boolean value is converted to an integer value true becomes 1 and false becomes 0. If an integer value is converted to a Boolean value 0 becomes false and non-zero becomes true.

8 MS Sadia Ejaz CIIT ATTOCK Character Type The character type is used to store characters - typically ASCII characters but not always. For example: char menuSelection = 'q'; char userInput = '3';

9 MS Sadia Ejaz CIIT ATTOCK Integer Types The integer type is used for storing whole numbers. We can use signed, unsigned or plain integer values as follows: signed int index = 41982; signed int temperature = -32; unsigned int count = 0; int height = 100; int balance = -67; signed integers can hold positive or negative values, and unsigned integers can hold only positive values

10 MS Sadia Ejaz CIIT ATTOCK Integer (size) Integer values come in three sizes, plain int, short int and long int. int normal = 1000; short int smallValue = 100; long int bigValue = 10000;

11 MS Sadia Ejaz CIIT ATTOCK Floating-Point Types Floating point types can contain decimal numbers, for example 1.23, -.087. There are three sizes, float (single-precision), double (double-precision) and long double (extended-precision). Some examples: float celsius = 37.623; double fahrenheit = 98.415; long double accountBalance = 1897.23;

12 MS Sadia Ejaz CIIT ATTOCK Constant Values A constant is a value that does not change. There are various categories of constants you will be using in your programs. For example, the algebraic numbers you are aware of are constants because they never change. Examples of constant numbers are 12, 0, 1505, or 88146. Therefore, any number you can think of is a constant. Every letter of the alphabet is a constant and is always the same. Examples of constant letters are d, n, c.

13 MS Sadia Ejaz CIIT ATTOCK Example #include void main() { cout << 28; cout << "\nStudent Age: " << 14; cout << "\n\n"; return 0; }

14 MS Sadia Ejaz CIIT ATTOCK Escape Sequences list of escape sequences:

15 MS Sadia Ejaz CIIT ATTOCK Int Data Types Size Data TypeSize in ByteDescription Int2-32768 to 32767 Short2-32768 to 32767 Unsigned int20 to 65535 long4-2,147,483,648 to 2,147,483,647 Unsigned long40 to 4,294,967,295

16 MS Sadia Ejaz CIIT ATTOCK Float Data Type Data TypeSize in ByteDescription Float43.4*10 -38 to 3.4*10 +38 double81.7*10 -308 to 1.7*10 +308 Long double101.7*10 -4932 to 1.7*10 +4932

17 MS Sadia Ejaz CIIT ATTOCK Variable Declaration The process of specifying the variable name and its type is called variable declaration Syntax  Data-type variable name  Int marks  Float average  Char grade

18 MS Sadia Ejaz CIIT ATTOCK Variable Initialization The process of assigning a value to a variable at the time of declaration is known as Variable Initializationexample  Int n=100

19 MS Sadia Ejaz CIIT ATTOCK Expression A statement that evaluate to value is called expression An expression gives a single value An expression consist of a operator and operand Example  A+B  M/N  X+100

20 MS Sadia Ejaz CIIT ATTOCK Assignment Statement A statement that assign a value to a variable is known as assignment statement Syntax  Variable=expression  A=100  C=A+B

21 MS Sadia Ejaz CIIT ATTOCK Example int In this example we take two integers, add them and display the answer on the screen. The code of the program is written below.  #include  main()  {  int x;  int y;  int z;  x = 5;  y = 10;  z = x + y;  cout << “x = “;  cout << x;  cout << “ y=“;  cout << y;  cout << “ z = x + y = “;  cout << z;  }

22 MS Sadia Ejaz CIIT ATTOCK Short int example /*This program uses short data type to store values */ #include main() { short x; short y; short z; x = 5; y = 10; z = x + y; cout << “x = “; cout << x; cout << “ y=“; cout << y; cout << “ z = x + y = “; cout << z; }

23 MS Sadia Ejaz CIIT ATTOCK long Data Type On the other side if we have a very large whole number that can not be stored in an int then we use the data type long provided by C++. So when we are going to deal with very big whole numbers in our program, we use long data type. We use it in program as: long x = 300500200;

24 MS Sadia Ejaz CIIT ATTOCK Real Numbers The C language provides two data types to deal with real numbers (numbers with decimal points e.g. 1.35, 735.251). The real numbers are also known as floating point numbers. float double

25 MS Sadia Ejaz CIIT ATTOCK float Data Type Example To store real numbers, float data type is used. The float data type uses four bytes to store a real number. Here is program that uses float data types. /*This program uses short data type to store values */ #include main() { float x; float y; float z; x = 12.35; y = 25.57; z = x + y; cout << “ x = “; cout << x; cout << “ y = “; cout << y; cout << “ z = x + y = “; cout << z; }

26 MS Sadia Ejaz CIIT ATTOCK double Data Type If we need to store a large real number which cannot be store in four bytes, then we use double data type. Normally the size of double is twice the size of float. In program we use it as: double x = 345624.769123;

27 MS Sadia Ejaz CIIT ATTOCK char Data Type So far we have been looking on data types to store numbers, In programming we do need to store characters like a,b,c etc. For storing the character data C language provides char data type. By using char data type we can store characters in variables. While assigning a character value to a char type variable single quotes are used around the character as ‘a’.

28 MS Sadia Ejaz CIIT ATTOCK Example /* This program uses short data type to store values */ #include main() { char x; x = ’a’; cout << “The character value in x = “; cout << x; }

29 MS Sadia Ejaz CIIT ATTOCK Integer Overflow and Underflow Integer Overflow Integer Overflow  It occurs when the value assigned to an integer variable is more than maximum possible value. Integer Underflow Integer Underflow  It occurs when the value assigned to an integer variable is less than possible minimum value.

30 Operators Operators are the symbols that are used to perform certain operation on data C++ provides variety of operator  Arithmetic operator  Relational operator  Logical operators  Bitwise operators

31 Arithmetic operators The five arithmetical operations supported by the C++ language are Arithmetic operator is a symbol that perform mathematical operation on data Example  + for addition e.g. a+b  - for subtraction e.g. a-b  * for multiplication e.g. a*b  / for division e.g. a/b  %for modulo e.g. a%b

32 %for modulo Modulo is the operation that gives the remainder of a division of two values. For example, if we write: a = 11 % 3; The variable a will contain the value 2, since 2 is the remainder from dividing 11 between 3.

33 Assignment (=) The assignment operator assigns a value to a variable. a = 5; This statement assigns the integer value 5 to the variable a. The part at the left of the assignment operator (=) is known as the lvalue (left value) and the right one as the rvalue (right value). The lvalue has to be a variable whereas the rvalue can be either a constant, a variable, the result of an operation or any combination of these. The most important rule when assigning is the right- to-left rule: The assignment operation always takes place from right to left, and never the other way:

34 Example // assignment operator #include int main () { int a, b; a = 10; b = 4; A=b b = 7; cout << "a:"; cout << a; cout << " b:"; cout << b; return 0; }

35 Compound Assignment statement An assignment that assign a value to many variables is known as compound assignment operator. Example  A=b=20  X=y=z=100

36 Compound assignment Operator When we want to modify the value of a variable by performing an operation on the value currently stored in that variable we can use compound assignment operators: C++ provide Compound assignment Operator that combine assignment operator with arithmetic operator

37 Syntax Variable op=expression Example  N+=10 = N=N+1 List of Compound assignment Operator (+=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |=)

38 Example int main () { int a, b=3; a = b; a+=2; // equivalent to a=a+2 cout << a; return 0; }

39 Example Prefix  B=3; A=--B; // A contains 2, B contains 2 Postfix  B=3; A=B--; // A contains 3, B contains 2

40 Relational and equality operators In order to evaluate a comparison between two expressions we can use the relational and equality operators. The result of a relational operation is a Boolean value that can only be true or false, according to its Boolean result. We may want to compare two expressions, for example, to know if they are equal or if one is greater than the other is.

41 Example ==Equal to !=Not equal to >Greater than <Less than >=Greater than or equal to <=Less than or equal to

42 Example 7 == 5) // evaluates to false. (5 > 4) // evaluates to true. (3 != 2) // evaluates to true. (6 >= 6) // evaluates to true. (5 < 5) // evaluates to false.

43 Example 2 (a == 5) // evaluates to false since a is not equal to 5. (a*b >= c) // evaluates to true since (2*3 >= 6) is true. (b+4 > a*c) // evaluates to false since (3+4 > 2*6) is false. ((b=2) == a) // evaluates to true.

44 Be careful The operator = (one equal sign) is not the same as the operator == (two equal signs) the first one is an assignment operator (assigns the value at its right to the variable at its left) and the other one (==) is the equality operator that compares whether both expressions in the two sides of it are equal to each other. Thus, in the last expression ((b=2) == a), we first assigned the value 2 to b and then we compared it to a, that also stores the value 2, so the result of the operation is true.

45 Logical operators ( !, &&, || ) The Operator ! is the C++ operator to perform the Boolean operation NOT, it has only one operand, located at its right, and the only thing that it does is to inverse the value of it, producing false if its operand is true and true if its operand is false. Basically, it returns the opposite Boolean value of evaluating its operand.

46 logical operators && and || The logical operators && and || are used when evaluating two expressions to obtain a single relational result. The operator && corresponds with Boolean logical operation AND. This operation results true if both its two operands are true, and false otherwise.

47 Example The following panel shows the result of operator && evaluating the expression a && b: && OPERATOR a b a && b true true true True false false False true false False false false

48 Example 2 The operator || corresponds with Boolean logical operation OR. This operation results true if either one of its two operands is true, thus being false only when both operands are false themselves. Here are the possible results of a || b: && OPERATOR A b a || b true true true True false true False true true False false false

49 For example: ( (5 == 5) && (3 > 6) ) // evaluates to false ( true && false ). ( (5 == 5) || (3 > 6) ) // evaluates to true ( true || false ).

50 Precedence of operators When writing complex expressions with several operands, we may have some doubts about which operand is evaluated first and which later. For example, in this expression: a = 5 + 7 % 2 we may doubt if it really means: a = 5 + (7 % 2) // with a result of 6, or a = (5 + 7) % 2 // with a result of 0 The correct answer is the first of the two expressions, with a result of 6.

51 Operator Precedence (contd.) Example:10 * (24 / (5 - 2) ) + 13 Example:10 * (24 / (5 - 2) ) + 13 10 * ( 24 / 3 ) + 13 10 * ( 24 / 3 ) + 13 10 * 8 + 13 10 * 8 + 13 80 + 13 80 + 13 93 93

52 MS Sadia Ejaz CIIT ATTOCK i = 2 * 3 / 4 + 4 / 4 + 8 - 2 + 5 / 8 Question:perform setwise operation on the given equation

53 Operator Precedence (contd.) The order of precedence in C ++ language is as follows: The order of precedence in C ++ language is as follows:  Any expression given in parenthesis is evaluated first.  Then multiplication * and division / operators are evaluated.  Then plus + and minus – operators are evaluated.  In case of parenthesis within parenthesis, the expression of the inner parenthesis will be evaluated first.

54 Operator Associativity The order in which operators has same precedence are evaluated is known as operator associativity. The order in which operators has same precedence are evaluated is known as operator associativity. If an expression contains some operators that have same precedence level, the expression is evaluated from left-to-right or right-to-left. If an expression contains some operators that have same precedence level, the expression is evaluated from left-to-right or right-to-left.

55 Operator Associativity (contd.) Operator associativity in C ++ language is as follows: Operator associativity in C ++ language is as follows: OperatorsAssociativity () ++(postfix) --(postfix)Left-to-right +(unary) -(unary) ++(prefix) --(prefix) Left-to-right * / %Left-to-right + -Left-to-right = += -= *= /=Right-to-left

56 Lvalue and Rvalue Lvalue Lvalue  It is an operand that can be written on the left side of assignment operator =. Rvalue Rvalue  It is an operand that can be written on the right side of assignment operator =.

57 Example a = 5 + 7 % 2;might be written either as: a = 5 + (7 % 2); a = (5 + 7) % 2;

58 Example 2 (a == 5) // evaluates to false since a is not equal to 5. (a*b >= c) // evaluates to true since (2*3 >= 6) is true. (b+4 > a*c) // evaluates to false since (3+4 > 2*6) is false. ((b=2) == a) // evaluates to true.

59 Escape Sequences These are special characters used in control string to modify the format of output. These are special characters used in control string to modify the format of output. Different escape sequences are as follows: Different escape sequences are as follows: Escape SequencePurpose Escape SequencePurpose \ a Alarm \ a Alarm \ b Backspace \ b Backspace \ f Form feed \ f Form feed \ n Carriage return \ n Carriage return \ t Tab \ t Tab \ ’ Single quote \ ’ Single quote \ ” Double quote \ ” Double quote

60 Type casting The process of converting the data type of a value during execution is known as type casting Two types of type casting  Implicit type casting  Explicit type casting

61 Implicit type casting Implicit type casting is performed automatically by the c++ compiler.

62 Implicit Type Casting It is performed automatically by C++ compiler. e.g. char + float  float It is performed automatically by C++ compiler. e.g. char + float  float long double double long float int char Highest data type Lowest data type

63 Explicit type casting Explicit type casting is performed by the programmer It is performed by using cast operator The cast operator tells the computer to convert the data type of a value Syntax  (type) expression

64 Example  Float a,b  Int c;  A=10.3  B=5.2  c=(int)a % (int)b;  cout<<c; Result  Shows 0

65 The sizeof operator The sizeof operator is used to find the size of any data value It gives the number of types occupied by the value Syntax  Sizeof(operand) Example cout<<sizeof(int) Result  2

66 comments Comments are line of program that are not executed Compiler ignore comments and does not include them in executable program Comments can be added anywhere in program in two ways Single line comments \\ Multiline comments /* */

67 C++ Manipulator C++ manipulator are used to format the output in different styles. The manipulators are the most common way to control output formatting  Endl  Setw  Showpoint

68 Endl manipulator The endl stands for end line The endl manipulator is used to move cursor to the beginning of next line  Example Cout<<“hello”<<endl<<“comsats”

69 Constants It is a quantity that cannot be changed during program execution. It is a quantity that cannot be changed during program execution. Two types of constants. Two types of constants.  Literal constant  Symbolic constant

70 Literal Constant It is a value that is typed directly in a program. It is a value that is typed directly in a program. For example For example  int age = 19 ; Types of Literal Constants Types of Literal Constants  Integer constante.g. 87  Floating point constante.g. 10.22F  Character constante.g. ‘A’  String constante.g. “Pakistan”

71 Symbolic Constants It is a name given to values that cannot be changed. It is a name given to values that cannot be changed. It can be declared in two ways. It can be declared in two ways.  const Qualifier const data_type identifier = value ; const data_type identifier = value ; e.g const int N = 100 ; e.g const int N = 100 ;  Define Directive # define identifier value ; # define identifier value ; e.g # define Pl 3.141593 ; e.g # define Pl 3.141593 ;

72 Expression It is a statement that evaluates to a value. It is a statement that evaluates to a value. It consists of operators and operands. It consists of operators and operands.  e.gA + B ; Operator Operands

73 Program #include void main() { char ch1, ch2, sum; ch1 = ‘2’ ; ch2 = ‘6’ ; sum = ch1 + ch2 ; cout<<“Sum =“<<sum; }

74 Output 104 Because ASCII values of ‘2’ and’6’ are 50 and 54

75 Program #include #incldue void main() { clrscr(); short var1 = 32767; cout << var1 << endl; var1= var1 +1 ; cout <<var1 << endl; var1 = var1 - 1 ; cout << var1 << endl; getch(); }

76 Output 32767 - 32768 32767 Because range of short is -32768 to 32767.

77 Program #include #incldue #define PI 3.141 void main() { float r, area; clrscr(); cout << “Enter radius:”; cin>> r; area = 2.0 * PI * r; cout << “Area=“ << area; getch(); }

78 Output User will give input, then Area will be displayed on the screen.

79 Program #include #incldue void main() { clrscr(); int a,b; a = 10; b = 5; cout << “a+b =“<< a+b << endl; cout << “a-b =“<< a-b << endl; cout << “a*b =“<< a*b << endl; cout << “a/b =“<< a/b << endl; cout << “a%b =“<< a%b << endl; getch(); }

80 Output a+b =15 a-b =5 a*b =50 a/b =2 a%b =0

81 Lectures (program) 4.1. Write a program to compute area of a circle after reading input from user. 4.2. Write a program to calculate total marks. 4.3. Write a program to calculate total marks and percentage of three subjects. 4.4. Write a program to find out power of any number. 4.5. Write a program to Sum, Subtract, Multiply & Division of two numbers. Write a program that gets temperature from the user in Celsius and convert it into Fahrenheit using the formula F=9/5*C+32 MS Sadia Ejaz CIIT ATTOCK

82 CIIT Attock Ms Sadia Ejaz Lab Work 1. Write a program that input name, age and address from the user and display it on the screen 2. Write a program that input two numbers, swaps the value and then display them 3. Write a program that gets temperature from the user in Celsius and convert it into Fahrenheit using the formula F=9/5*C+32 4. Write a program that inputs radius from the user and calculates area and circumference of circle using the formula Area=pi r 2 and circumference=2pii r 5. Write a program that input 4 numbers and calculate the sum, average, and product of all the numbers

83 CIIT Attock Ms Sadia Ejaz Assignment Write a program that gets a three-digit number from the user and display in reverse order. For example user enter 123, it display 321 Write a program to show following output using one cout statement  1 2 3 4 5  6 7 8 9 10 Write a program that input age in years and display age in days and month Mr X’s basic salary is input through the keyboard. His dearness allowance is 40% of basic salary, and house rent allowance is 20% of basic salary. Write a program to calculate his gross salary Write a program that adds two character and shows the sum on screen


Download ppt "Introduction to Computer Programming. MS Sadia Ejaz CIIT ATTOCK Identifiers The identifiers are the names used to represent variable, constants, types,"

Similar presentations


Ads by Google