Professional Documents
Culture Documents
Introduction To Computer & Programming: Ms Sadia Ejaz Cs Department
Introduction To Computer & Programming: Ms Sadia Ejaz Cs Department
Constants
It is a quantity that cannot be changed during program execution. Two types of constants.
Literal Constant
int age = 19 ; Integer constant Floating point constant Character constant String constant e.g. 87 e.g. 10.22F e.g. A e.g. Pakistan
Symbolic Constants
It is a name given to values that cannot be changed. It can be declared in two ways.
const Qualifier
e.g
const data_type identifier = value ; const int N = 100 ; # define identifier value ; # define Pl 3.141593 ;
Define Directive
e.g
Expression
e.g
A+B;
Operands Operator
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 Unary Operators Binary Operators
Unary Operators
Binary Operators
Arithmetic Operators
+ / %
Lvalue
It is an operand that can be written on the left side of assignment operator =. It is an operand that can be written on the right side of assignment operator =.
Rvalue
They combine assignment operator with arithmetic operators. Variable operator = expression; e.g . N+ = 10 ; (N +=10;)
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
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
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.
()
++(postfix)
--(postfix)
* + -
= +=
Right-to-left
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:
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);
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:
//
/* ---*/
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 \a \b \f \n \t \ \
Purpose Alarm Backspace Form feed Carriage return 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:
end of line set width set the number of digits to be displayed replaces the leading or trailing blanks in output displays the decimal part controls the output of floating-point numbers
MS Sadia Ejaz CIIT ATTOCK
Standard Input
LAB WORK
Program .1
#include <iostream.h> #incldue<conio.h> void main() { clrscr(); cout << Hello World << endl; getch(); }
MS Sadia Ejaz CIIT ATTOCK
Output
Hello World
Program .2
Program .3
#include <iostream.h> void main() { char ch1, ch2, sum; ch1 = 2 ; ch2 = 6 ; sum = ch1 + ch2 ; cout<<Sum =<<sum; }
Output
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
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(); }
Output
User will give input, then Area will be displayed on the screen.
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(); }
Output