introduction to computer & programming

41
MS Sadia Ejaz CIIT ATTOCK Introduction to computer & programming MS SADIA EJAZ CS DEPARTMENT

Upload: kevork

Post on 24-Jan-2016

47 views

Category:

Documents


1 download

DESCRIPTION

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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Introduction to computer &                  programming

MS Sadia Ejaz CIIT ATTOCK

Introduction to computer & programming

MS SADIA EJAZ

CS DEPARTMENT

Page 2: Introduction to computer &                  programming

MS Sadia Ejaz CIIT ATTOCK 22

Programming Programming Language C++Language C++

Page 3: Introduction to computer &                  programming

MS Sadia Ejaz CIIT ATTOCK

ConstantsConstants

It is a quantity that cannot be changed It is a quantity that cannot be changed during program execution.during program execution.

Two types of constants.Two types of constants. Literal constantLiteral constant Symbolic constantSymbolic constant

Page 4: Introduction to computer &                  programming

MS Sadia Ejaz CIIT ATTOCK

Literal Constant Literal Constant

It is a value that is typed directly in a It is a value that is typed directly in a program.program.

For exampleFor example int age = 19 ;int age = 19 ;

Types of Literal ConstantsTypes of Literal Constants Integer constantInteger constant e.g. 87e.g. 87 Floating point constantFloating point constant e.g. 10.22Fe.g. 10.22F Character constantCharacter constant e.g. ‘A’e.g. ‘A’ String constantString constant e.g. “Pakistan”e.g. “Pakistan”

Page 5: Introduction to computer &                  programming

MS Sadia Ejaz CIIT ATTOCK

Symbolic ConstantsSymbolic Constants

It is a name given to values that cannot It is a name given to values that cannot be changed.be changed.

It can be declared in two ways.It can be declared in two ways. const Qualifierconst 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 Directive # define identifier value ;# define identifier value ; e.g # define Pl 3.141593 ;e.g # define Pl 3.141593 ;

Page 6: Introduction to computer &                  programming

MS Sadia Ejaz CIIT ATTOCK

ExpressionExpression

It is a statement that evaluates to a It is a statement that evaluates to a value.value.

It consists of operators and operands.It consists of operators and operands. e.ge.g A + B ;A + B ;

Operator

Operands

Page 7: Introduction to computer &                  programming

MS Sadia Ejaz CIIT ATTOCK

OperatorsOperators There are the symbols that are used to There are the symbols that are used to

perform certain operations on data.perform certain operations on data. These include :These include :

Arithmetic operatorsArithmetic operators Relational operatorsRelational operators Logical operatorsLogical operators Bitwise operators , etcBitwise operators , etc

The operators can be categorized as follows:The operators can be categorized as follows: Unary OperatorsUnary Operators Binary OperatorsBinary Operators

Page 8: Introduction to computer &                  programming

MS Sadia Ejaz CIIT ATTOCK

Unary OperatorsUnary Operators

It is a type of operator that works with It is a type of operator that works with one operand.one operand.

- , ++ , --- , ++ , -- e.g – a ;e.g – a ;

Page 9: Introduction to computer &                  programming

MS Sadia Ejaz CIIT ATTOCK

Binary OperatorsBinary Operators

It is a type of operator that works with It is a type of operator that works with two operands.two operands.

+ , - , * , /, %+ , - , * , /, % e.g x / y ;e.g x / y ;

Page 10: Introduction to computer &                  programming

MS Sadia Ejaz CIIT ATTOCK

Arithmetic OperatorsArithmetic Operators

It is a symbol that performs It is a symbol that performs mathematical operation on data.mathematical operation on data. AdditionAddition ++ SubtractionSubtraction -- MultiplicationMultiplication ** DivisionDivision // ModulusModulus %%

Page 11: Introduction to computer &                  programming

MS Sadia Ejaz CIIT ATTOCK

Lvalue and RvalueLvalue and Rvalue

LvalueLvalue It is an operand that can be written on the It is an operand that can be written on the

left side of assignment operator =.left side of assignment operator =. RvalueRvalue

It is an operand that can be written on the It is an operand that can be written on the right side of assignment operator =.right side of assignment operator =.

Page 12: Introduction to computer &                  programming

MS Sadia Ejaz CIIT ATTOCK

Compound Assignment Compound Assignment StatementStatement It is an assignment statement that It is an assignment statement that

assigns a value to many variables.assigns a value to many variables. e.g.e.g. A = B = 10A = B = 10 ; ;

Page 13: Introduction to computer &                  programming

MS Sadia Ejaz CIIT ATTOCK

Compound Assignment Compound Assignment OperatorsOperators They combine assignment operator They combine assignment operator

with arithmetic operators.with arithmetic operators. Variable operator = expression;Variable operator = expression; e.g .e.g . N +N + = 10 ; = 10 ; (N +=10;)(N +=10;)

Page 14: Introduction to computer &                  programming

MS Sadia Ejaz CIIT ATTOCK

Increment OperatorIncrement Operator It is used to increase the value of a variable by 1.It is used to increase the value of a variable by 1. ++++ It is a unary operator and works with single variable .It is a unary operator and works with single variable . e.g. A ++e.g. A ++ It can be used in two forms.It can be used in two forms.

prefix form prefix form The increment operator is written The increment operator is written beforebefore the variable. the variable.

postfix form postfix form The increment operator is written The increment operator is written afterafter the variable. the variable.

++y ; ++y ; prefix form prefix form y++ ; y++ ; postfix form postfix form

Page 15: Introduction to computer &                  programming

MS Sadia Ejaz CIIT ATTOCK

Decrement OperatorDecrement Operator It is used to increase the value of a variable by 1.It is used to increase the value of a variable by 1. ---- It is a unary operator and works with single variable .It is a unary operator and works with single variable . e.g. A --e.g. A -- It can be used in two forms.It can be used in two forms.

prefix form prefix form The decrement operator is written The decrement operator is written beforebefore the variable. the variable.

postfix form postfix form The decrement operator is written The decrement operator is written afterafter the variable. the variable.

--y ; --y ; prefix form prefix form y-- ; y-- ; postfix form postfix form

Page 16: Introduction to computer &                  programming

MS Sadia Ejaz CIIT ATTOCK

Operator PrecedenceOperator Precedence

The order in which different types of The order in which different types of operators in an expression are evaluated operators in an expression are evaluated is known as operator precedence. It is is known as operator precedence. It is also known as hierarchy of operators.also known as hierarchy of operators.

Each operator has its own precedence Each operator has its own precedence level. If an expression contains different level. If an expression contains different types of operators, the operators with types of operators, the operators with higher precedence are evaluated before higher precedence are evaluated before the operators wit lower precedence.the operators wit lower precedence.

Page 17: Introduction to computer &                  programming

MS Sadia Ejaz CIIT ATTOCK

Operator Precedence (contd.)Operator Precedence (contd.)

The order of precedence in C ++ language The order of precedence in C ++ language is as follows:is as follows: Any expression given in parenthesis is Any expression given in parenthesis is

evaluated first.evaluated first. Then multiplication * and division / operators Then multiplication * and division / operators

are evaluated.are evaluated. Then plus + and minus – operators are Then plus + and minus – operators are

evaluated.evaluated. In case of parenthesis within parenthesis, the In case of parenthesis within parenthesis, the

expression of the inner parenthesis will be expression of the inner parenthesis will be evaluated first.evaluated first.

Page 18: Introduction to computer &                  programming

MS Sadia Ejaz CIIT ATTOCK

Operator Precedence (contd.)Operator Precedence (contd.) Example:Example: 10 * (24 / 10 * (24 / (5 - 2)(5 - 2) ) + 13 ) + 13

10 * 10 * ( 24 / 3 )( 24 / 3 ) + 13 + 13

10 * 8 10 * 8 + 13+ 13

80 + 1380 + 13

9393

Page 19: Introduction to computer &                  programming

MS Sadia Ejaz CIIT ATTOCK

Operator AssociativityOperator Associativity

The order in which operators od same The order in which operators od same precedence are evaluated is known as precedence are evaluated is known as operator associativity. operator associativity.

If an expression contains some If an expression contains some operators that have same precedence operators that have same precedence level, the expression is evaluated from level, the expression is evaluated from left-to-right or right-to-left.left-to-right or right-to-left.

Page 20: Introduction to computer &                  programming

MS Sadia Ejaz CIIT ATTOCK

Operator Associativity Operator Associativity (contd.)(contd.) Operator associativity in C ++ language Operator associativity in C ++ language

is as follows:is as follows:

Operators Associativity

() ++(postfix) --(postfix)

Left-to-right

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

Left-to-right

* / % Left-to-right

+ - Left-to-right

= += -= *= /= Right-to-left

Page 21: Introduction to computer &                  programming

MS Sadia Ejaz CIIT ATTOCK

Type CastingType Casting

The process of converting the data type The process of converting the data type of a value during execution is known as of a value during execution is known as type casting.type casting.

It can be performed in two ways:It can be performed in two ways: Implicit Type CastingImplicit Type Casting Explicit Type CastingExplicit Type Casting

Page 22: Introduction to computer &                  programming

MS Sadia Ejaz CIIT ATTOCK

Implicit Type CastingImplicit Type Casting

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

long double

double

long

float

int

char

Highest data type

Lowest data type

Page 23: Introduction to computer &                  programming

MS Sadia Ejaz CIIT ATTOCK

Explicit Type CastingExplicit Type Casting

It is performed automatically by the It is performed automatically by the programmer.programmer.

(type) expression;(type) expression; e.g. (int) a% (int) b ;e.g. (int) a% (int) b ;

Page 24: Introduction to computer &                  programming

MS Sadia Ejaz CIIT ATTOCK

The “sizeof” OperatorThe “sizeof” Operator

It is used to find the size of any data It is used to find the size of any data value.value.

It gives the number of bytes occupied It gives the number of bytes occupied by that value.by that value.

sizeof(operand);sizeof(operand); e.g. sizeof (10);e.g. sizeof (10);

Page 25: Introduction to computer &                  programming

MS Sadia Ejaz CIIT ATTOCK

CommentsComments

These are the lines of program that are These are the lines of program that are not executed.not executed.

They explain the purpose of the code.They explain the purpose of the code. The can be added anywhere in The can be added anywhere in

programs in two ways:programs in two ways: Sing-line CommentsSing-line Comments “//”“//” Multi-line Comments Multi-line Comments /* ---*//* ---*/

Page 26: Introduction to computer &                  programming

MS Sadia Ejaz CIIT ATTOCK

Input and OutputInput and Output InputInput The process of giving something to computer is The process of giving something to computer is

known as input.known as input. Standard InputStandard Input

This term refers to the input via keyboard.This term refers to the input via keyboard. OutputOutput

The process of getting something from computer is The process of getting something from computer is known as output.known as output.

Standard OutputStandard Output This terms refers to the output displayed on monitor.This terms refers to the output displayed on monitor. cout<<variable/constant/expression;cout<<variable/constant/expression;

Page 27: Introduction to computer &                  programming

MS Sadia Ejaz CIIT ATTOCK

Escape SequencesEscape Sequences These are special characters used in control string to These are special characters used in control string to

modify the format of output.modify the format of output. Different escape sequences are as follows:Different escape sequences are as follows:

Escape SequenceEscape Sequence PurposePurpose

\ a\ a Alarm Alarm

\ b\ b Backspace Backspace

\ f\ f Form feed Form feed

\ n \ n Carriage return Carriage return

\ t\ t Tab Tab

\ ’\ ’ Single quote Single quote

\ ”\ ” Double quote Double quote

Page 28: Introduction to computer &                  programming

MS Sadia Ejaz CIIT ATTOCK

C++ ManipulatorsC++ Manipulators These are used to format the output in These are used to format the output in

different styles.different styles. Some important manipulators are as Some important manipulators are as

follows:follows: endlendl end of lineend of line setwsetw set widthset width setprecisionsetprecision set the number of digits to be set the number of digits to be

displayeddisplayed setfillsetfill replaces the leading or trailing blanks in outputreplaces the leading or trailing blanks in output

showpointshowpoint displays the decimal partdisplays the decimal part fixedfixed controls the output of floating-point numberscontrols the output of floating-point numbers

Page 29: Introduction to computer &                  programming

MS Sadia Ejaz CIIT ATTOCK

Standard InputStandard Input

It refers to the input given via It refers to the input given via keyboard.keyboard.

cin>> var ;cin>> var ;

Page 30: Introduction to computer &                  programming

MS Sadia Ejaz CIIT ATTOCK

LAB WORK

Page 31: Introduction to computer &                  programming

MS Sadia Ejaz CIIT ATTOCK

Program .1

#include <iostream.h> #incldue<conio.h> void main()

{clrscr();cout << “Hello World” << endl;getch();

}

Page 32: Introduction to computer &                  programming

MS Sadia Ejaz CIIT ATTOCK

Output

Hello World

Page 33: Introduction to computer &                  programming

MS Sadia Ejaz CIIT ATTOCK

Program .2

Write a program which will display your name.

Page 34: Introduction to computer &                  programming

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;

}

Page 35: Introduction to computer &                  programming

MS Sadia Ejaz CIIT ATTOCK

Output

104 Because ASCII values of ‘2’ and’6’ are

50 and 54

Page 36: Introduction to computer &                  programming

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();

}

Page 37: Introduction to computer &                  programming

MS Sadia Ejaz CIIT ATTOCK

Output

32767 - 32768 32767 Because range of short is -32768 to

32767.

Page 38: Introduction to computer &                  programming

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();

}

Page 39: Introduction to computer &                  programming

MS Sadia Ejaz CIIT ATTOCK

Output

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

Page 40: Introduction to computer &                  programming

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();

}

Page 41: Introduction to computer &                  programming

MS Sadia Ejaz CIIT ATTOCK

Output

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