410 likes | 577 Views
Introduction to computer & programming. MS SADIA EJAZ CS DEPARTMENT. Programming Language C++. 2. Constants. It is a quantity that cannot be changed during program execution. Two types of constants. Literal constant Symbolic constant. Literal Constant.
E N D
Introduction to computer & programming MS SADIA EJAZ CS DEPARTMENT MS Sadia Ejaz CIIT ATTOCK
Programming Language C++ MS Sadia Ejaz CIIT ATTOCK 2
Constants • It is a quantity that cannot be changed during program execution. • Two types of constants. • Literal constant • Symbolic constant MS Sadia Ejaz CIIT ATTOCK
Literal Constant • It is a value that is typed directly in a program. • For example • int age = 19 ; • Types of Literal Constants • Integer constant e.g. 87 • Floating point constant e.g. 10.22F • Character constant e.g. ‘A’ • String constant e.g. “Pakistan” MS Sadia Ejaz CIIT ATTOCK
Symbolic Constants • It is a name given to values that cannot be changed. • It can be declared in two ways. • const Qualifier • const data_type identifier = value ; • e.g const int N = 100 ; • Define Directive • # define identifier value ; • e.g # define Pl 3.141593 ; MS Sadia Ejaz CIIT ATTOCK
Expression Operator Operands • It is a statement that evaluates to a value. • It consists of operators and operands. • e.g A + B ; MS Sadia Ejaz CIIT ATTOCK
Operators • There are the symbols that are used to perform certain operations on data. • These include : • Arithmetic operators • Relational operators • Logical operators • Bitwise operators , etc • The operators can be categorized as follows: • Unary Operators • Binary Operators MS Sadia Ejaz CIIT ATTOCK
Unary Operators It is a type of operator that works with one operand. - , ++ , -- e.g – a ; MS Sadia Ejaz CIIT ATTOCK
Binary Operators It is a type of operator that works with two operands. + , - , * , /, % e.g x / y ; MS Sadia Ejaz CIIT ATTOCK
Arithmetic Operators • It is a symbol that performs mathematical operation on data. • Addition + • Subtraction - • Multiplication * • Division / • Modulus % MS Sadia Ejaz CIIT ATTOCK
Lvalue and Rvalue • Lvalue • It is an operand that can be written on the left side of assignment operator =. • Rvalue • It is an operand that can be written on the right side of assignment operator =. MS Sadia Ejaz CIIT ATTOCK
Compound Assignment Statement It is an assignment statement that assigns a value to many variables. e.g. A = B = 10 ; MS Sadia Ejaz CIIT ATTOCK
Compound Assignment Operators They combine assignment operator with arithmetic operators. Variable operator = expression; e.g . N + = 10 ; (N +=10;) MS Sadia Ejaz CIIT ATTOCK
Increment Operator • It is used to increase the value of a variable by 1. • ++ • It is a unary operator and works with single variable . • e.g. A ++ • It can be used in two forms. • prefix form The increment operator is written before the variable. • postfix form The increment operator is written after the variable. • ++y ; prefix form • y++ ; postfix form MS Sadia Ejaz CIIT ATTOCK
Decrement Operator • It is used to increase the value of a variable by 1. • -- • It is a unary operator and works with single variable . • e.g. A -- • It can be used in two forms. • prefix form The decrement operator is written before the variable. • postfix form The decrement operator is written after the variable. • --y ; prefix form • y-- ; postfix form MS Sadia Ejaz CIIT ATTOCK
Operator Precedence • The order in which different types of operators in an expression are evaluated is known as operator precedence. It is also known as hierarchy of operators. • Each operator has its own precedence level. If an expression contains different types of operators, the operators with higher precedence are evaluated before the operators wit lower precedence. MS Sadia Ejaz CIIT ATTOCK
Operator Precedence (contd.) • 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. MS Sadia Ejaz CIIT ATTOCK
Operator Precedence (contd.) • Example: 10 * (24 / (5 - 2) ) + 13 10 * ( 24 / 3 ) + 13 10 * 8 + 13 80 + 13 93 MS Sadia Ejaz CIIT ATTOCK
Operator Associativity • The order in which operators od 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. MS Sadia Ejaz CIIT ATTOCK
Operator Associativity (contd.) • Operator associativity in C ++ language is as follows: MS Sadia Ejaz CIIT ATTOCK
Type Casting • The process of converting the data type of a value during execution is known as type casting. • It can be performed in two ways: • Implicit Type Casting • Explicit Type Casting MS Sadia Ejaz CIIT ATTOCK
Implicit Type Casting Highest data type long double double float long int char It is performed automatically by C++ compiler. e.g. char + float float MS Sadia Ejaz CIIT ATTOCK Lowest data type
Explicit Type Casting It is performed automatically by the programmer. (type) expression; e.g. (int) a% (int) b ; MS Sadia Ejaz CIIT ATTOCK
The “sizeof” Operator It is used to find the size of any data value. It gives the number of bytes occupied by that value. sizeof(operand); e.g. sizeof (10); MS Sadia Ejaz CIIT ATTOCK
Comments • These are the lines of program that are not executed. • They explain the purpose of the code. • The can be added anywhere in programs in two ways: • Sing-line Comments “//” • Multi-line Comments /* ---*/ MS Sadia Ejaz CIIT ATTOCK
Input and Output • Input • The process of giving something to computer is known as input. • Standard Input • This term refers to the input via keyboard. • Output • The process of getting something from computer is known as output. • Standard Output • This terms refers to the output displayed on monitor. • cout<<variable/constant/expression; MS Sadia Ejaz CIIT ATTOCK
Escape Sequences • These are special characters used in control string to modify the format of output. • Different escape sequences are as follows: Escape Sequence Purpose \ a Alarm \ b Backspace \ f Form feed \ n Carriage return \ t Tab \ ’ Single quote \ ” Double quote MS Sadia Ejaz CIIT ATTOCK
C++ Manipulators • These are used to format the output in different styles. • Some important manipulators are as follows: • endl end of line • setw set width • setprecision set the number of digits to be displayed • setfill replaces the leading or trailing blanks in output • showpoint displays the decimal part • fixed controls the output of floating-point numbers MS Sadia Ejaz CIIT ATTOCK
Standard Input It refers to the input given via keyboard. cin>> var ; MS Sadia Ejaz CIIT ATTOCK
LAB WORK MS Sadia Ejaz CIIT ATTOCK
Program .1 #include <iostream.h> #incldue<conio.h> void main() { clrscr(); cout << “Hello World” << endl; getch(); } MS Sadia Ejaz CIIT ATTOCK
Output • Hello World MS Sadia Ejaz CIIT ATTOCK
Program .2 • Write a program which will display your name. MS Sadia Ejaz CIIT ATTOCK
Program .3 #include <iostream.h> void main() { char ch1, ch2, sum; ch1 = ‘2’ ; ch2 = ‘6’ ; sum = ch1 + ch2 ; cout<<“Sum =“<<sum; } MS Sadia Ejaz CIIT ATTOCK
Output • 104 • Because ASCII values of ‘2’ and’6’ are 50 and 54 MS Sadia Ejaz CIIT ATTOCK
Program .4 #include <iostream.h> #incldue<conio.h> void main() { clrscr(); short testVar = 32767; cout << testVar << endl; testVar = testVar +1 ; cout << testVar << endl; testVar = testVar - 1 ; cout << testVar << endl; getch(); } MS Sadia Ejaz CIIT ATTOCK
Output • 32767 • - 32768 • 32767 • Because range of short is -32768 to 32767. MS Sadia Ejaz CIIT ATTOCK
Program . 5 #include <iostream.h> #incldue<conio.h> #define PI 3.141 void main() { float r, area; clrscr(); cout << “Enter radius:”; cin>> r; area = 2.0 * PI * r; cout << “Area=“ << area; getch(); } MS Sadia Ejaz CIIT ATTOCK
Output • User will give input, then Area will be displayed on the screen. MS Sadia Ejaz CIIT ATTOCK
Program. 6 #include <iostream.h> #incldue<conio.h> 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(); } MS Sadia Ejaz CIIT ATTOCK
Output • a+b =15 • a-b =5 • a*b =50 • a/b =2 • a%b =0 MS Sadia Ejaz CIIT ATTOCK