Você está na página 1de 41

Introduction to computer & programming

MS SADIA EJAZ CS DEPARTMENT


MS Sadia Ejaz CIIT ATTOCK

Programming Language C++

MS Sadia Ejaz CIIT ATTOCK

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 ; Integer constant Floating point constant Character constant String constant e.g. 87 e.g. 10.22F e.g. A e.g. Pakistan

Types of Literal Constants


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

e.g

const data_type identifier = value ; const int N = 100 ; # define identifier value ; # define Pl 3.141593 ;

Define Directive

e.g

MS Sadia Ejaz CIIT ATTOCK

Expression

It is a statement that evaluates to a value. It consists of operators and operands.

e.g

A+B;
Operands Operator

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 Unary Operators Binary Operators

The operators can be categorized as follows:



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 =. It is an operand that can be written on the right side of assignment operator =.

Rvalue

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:


Operators Associativity Left-to-right Left-to-right Left-to-right Left-to-right -= *= /=
MS Sadia Ejaz CIIT ATTOCK

()

++(postfix)

--(postfix)

+(unary) -(unary) ++(prefix) --(prefix)

* + -

= +=

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:

Implicit Type Casting Explicit Type Casting

MS Sadia Ejaz CIIT ATTOCK

Implicit Type Casting

It is performed automatically by C++ compiler. e.g. char + float float


Highest data type long double double float long int char
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 \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:

endl setw setprecision setfill showpoint fixed

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

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 and6 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

Você também pode gostar