Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
37 views

II&CT Lecture 19-20 Programming Fundamentals (Basics, Pre Postfix Operators)

The document provides an introduction to basic input/output in C++. It discusses the cout and cin objects for output and input streams. It describes escape sequences, endl and setw manipulators, and how to use them. It also covers compound assignment statements, expressions, and increment/decrement operators. Examples of short C++ programs are provided to demonstrate these concepts.

Uploaded by

Abdullah Jutt
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

II&CT Lecture 19-20 Programming Fundamentals (Basics, Pre Postfix Operators)

The document provides an introduction to basic input/output in C++. It discusses the cout and cin objects for output and input streams. It describes escape sequences, endl and setw manipulators, and how to use them. It also covers compound assignment statements, expressions, and increment/decrement operators. Examples of short C++ programs are provided to demonstrate these concepts.

Uploaded by

Abdullah Jutt
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

INTRODUCTION TO ICT.

(Programming Fundamentals with C++)


Lecture # 19-20
By:
M.Nadeem Akhtar.
Department of CS & IT.

URL: https://sites.google.com/site/nadeemcsuoliict/home/lectures

1
OUTLINES
 Basic In put /out put
 The cout object
 Escape sequence
 The “endl” Manipulator
 The “setw” Manipulator
 The “cin” object –In put stream
 Compound Assignment statements
 Compound Assignment Expression
 Increment & Decrement Operators
Basic In put /Out put
 Statements used to get data from user & then assign it to variables are known
as input statements.
 The statements that are used to receive data from the computer memory &
then to send it to out put devices are called output statements.
The “Cout” object --- output stream:
“cout” is standard output stream in C++, to send out put to the screen.
The syntax of ‘cout’ is:
cout << const1/ var1 [ << const2/var2-------];
Where
cout = name of output stream object.
<< = put to operator or insertion operator. It directs the out put
to out put devices.
For example:
cout<<“one kilobyte = “<<1024<<“ bytes”;
Out put: one kilobyte = 1024 bytes

Mr.Nadeem Akhtar 3
Program:01-05
 Write a program to print a message on screen using “cout” object:

# include <iostream.h>
# include <conio.h>
int main ( )
{
clrscr ( );
char q;
cout<<"Well come to C++ programming language \n";
cout<<"C++ is a powerful programming language"<<endl;
cout<<"press any character to exit program =";
cin>>q;
return 0;
}

Note: in this program clrscr( ) function has been used to clear the computer screen.
This function is a part of “conio.h” header file.

Mr.Nadeem Akhtar 4
Escape Codes
 Escape codes are special characters that cannot be expressed
otherwise in the source code such as new line, tab and single quotes.
 All of these characters or symbols are preceded by an inverted
(back) slash (\).
The “endl "Manipulator.
 Manipulators are operators that are used with the put to operator (<<) to control format
of data.
 The “endl” stand for end of line. It has same effect as the escape sequence “\n”. This is
predefined iostream manipulator.
 For example :
cout<<“ C++ \n”<<“programming \n”<<“Language”;
is equivalent to :
cout<<“C++”<<endl<<“Programming”<<endl<<“Language”;
Both have same out put. The difference between “\n” and “endl” is that “endl” is a constant
symbol and given in “cout” stream with (<<) but “\n” is inserted into constant string in
double quotation.
Program: write a program to print a message on screen by using “endl "manipulator.
#include <iostream.h>
main ( )
{ cout<< “I am a ”<<endl<<“Pakistani” }
Program output :
I am a
Pakistani

Mr.Nadeem Akhtar 6
The “setw” Manipulator
 The “setw” stand for set width. This manipulator is used to set
width of the output on the out put device.
 The “”setw” manipulator specifies the width for an output . The out
put is printed left justified in the specified width.
 Its syntax is:
setw(n)
Where n specifies the width of the output field. it is an integer value.
For example , to print “Pakistan” in 1 st 10 columns and “Islamabad
"in next 15 columns, the out put statement is written as :
Cout<<setw(10)<<“pakistan”<<setw(15)<<“Islamabad”;
 The “setw” manipulator is a part of “iomanip.h” header file.if this
manipulator is to be used in the program, this header file must be
included at the beginning of program.

Mr.Nadeem Akhtar 7
Program 01-08A
Write a program to print “Pakistan” in 1st 10
columns and “Islamabad "in next 15
columns,
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
int main()
{
clrscr();
char q;
cout<<setw(10)<<"pakistan"<<setw(15)<<"Islamabad"<<endl;
cout<<"press any character to exit program =";
cin>>q;
return 0;
}

Mr.Nadeem Akhtar 8
The “cin” object – Input stream
 The “cin” stands for console input, used to get input from the key
board during execution of the program.
 The “cin” object is also a part of iostream header file.
 Its syntax is :
cin>> var1[>> var2 --------];
Where cin = represents object name used as an in put stream.
>> = Extraction operator or get from oerator.
Var 1, var2 = list of variables. Each variable is represented by
extraction operator “>>”.
For Example:
To input data into variables a, b and c, the input statement is
written as: cin>>a>>b>>c;

Mr.Nadeem Akhtar 9
Program 01-11
 Write a program in C++ to get two numbers from key board during
program execution. Calculate the sum and product of the numbers and
then print the result on computer screen.
#include<iostream.h>
#include<conio.h>
void main()
{
int n1,n2,s,p;
char q;
clrscr();
cout<<"Enter the first number?";
cin>>n1;
cout<<"Enter the 2nd number?";
cin>>n2;
s=n1+n2;
p=n1*n2;
cout<<"sum of numbers ="<<s<<endl;
cout<<"product of numbers ="<<p<<endl;
cout<<"press any character to exit program =";
cin>>q;
getch(); }
Mr.Nadeem Akhtar 10
Program 01-13
Write a program in C++ to read the name of a student and marks obtained in three
subjescts C++,english and calculus. Calculate the total and average. input the data
using "cin" object. Each subject has maximun of 100 marks.
#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
float avg;
int cpp,eng,calcu,total;
char name [20],q;
cout<<"Enter the name of student=";
cin>>name;
cout<<"Enter the marks obtained in C++=";
cin>>cpp;
cout<<"Enter the marks obtained in English=";
cin>>eng;
cout<<"Enter the marks obtained in calculus=";
cin>>calcu;
total=cpp+eng+calcu;
avg=total/3.0;
cout<<"Name of the student ="<<name<<endl;
cout<<"Total marks ="<<total<<endl;
cout<<"Average marks ="<<avg<<endl;
cout<<"press any character to exit program =";
cin>>q;
getch(); } Mr.Nadeem Akhtar 11
Compound Assignment statements
 The assignment statement can also be used to assign one value to many variables. This type of
assignment statement is called “compound Assignment statement.
 For example to assign an integer value 16 to two integer type variables x and y , the compound
assignment is written as:
x=y=16;
Program 01-16
 Write a program to assign a value 515 to integer type variabls x, y, a, b and c.also calculate the sum of
variables and print the result on the screen.
#include<iostream.h>
#include<conio.h>

int main()
{
clrscr();
char q;
int x,y,a,b,c,s;
x=y=a=b=c=515;
s=x+y+a+b+c;
cout<<"sum ="<<s<<endl;
cout<<"press any character to exit program =";
cin>>q;
return 0;
}

Mr.Nadeem Akhtar 12
Compound Assignment Expression
 Compound Assignment Expression is used to add, subtract, multiply or divide a value to
or from a variable without writing the variable on either side of the assignment operator
‘=‘
 In this assignment expression, the arithmetic operator is combined with the assignment
operator. Its syntax is
var op = expression
Where
Var represent variable to which the value is to be added.
Op represents arithmetic operators i.e. +.-,* and / etc.
Expression represents the arithmetic expression whose value is to
be assigned to the variable. It may be an expression, a
variable or a constant.
For example, to add 10 to a variable that already has a value, simple arithmetic expression is
written as:
xy= xy+10 this statement can be written as compound assignment expression as :
xy+= 10;
Following are some examples of Compound assignment expression.
x += 9; // Same as x = x+9;
x -= 2; // Same as x = x-2;
x * = 7; // same as x= x *7;
x /= 3; // same as x= x/3;
Mr.Nadeem Akhtar 13
Program 01-17
 Write a program in C++ to assign three values to three integer type variables a,b &
c. Add variables a & b and multiply their sum to variable c. Use compound
assignment statement.

#include<iostream.h>
#include<conio.h>

int main()
{
clrscr();
char q;
int a,b,c;
a=3;
b=6;
c=9;
c *= a+b;
cout<<"Result ="<<c<<endl;
cout<<"press any character to exit program =";
cin>>q;
return 0;
}

Mr.Nadeem Akhtar 14
Increment & Decrement Operators
 The operator that is used to add “1”to the value of a variable is called
increment operator.
 The operator that is used to subtract “1” from the value of a variable is
called decrement operator.
 Can be applied to variables but not to constants or ordinary expressions.
++i; legal
cnt--; legal
777++; illegal
++(a * b -1); illegal
 May either prefix or postfix the operand.
Prefix ++x; or Postfix x++;

x = x + 1;

++ & -- both cause a value of a variable to change in memory.

Mr.Nadeem Akhtar 15
Cont..
 Prefix Increment : ++i;
Expression value is the value After you increment.“increment - then use”

 Postfix Increment : i++;


Expression value is the current value (Before you increment) then it
increments. “use - then increment”

 Prefix Decrement : --i;


“decrement - then use”

 Postfix Decrement : i--;


“use - then decrement”

Mr.Nadeem Akhtar 16
The increment operator (++)
 It is represented by double plus sign (++). It is used to add “1”
to the value of an integer variable.
 This operator can be used before or after the variable name.
 For example , to add “1” to a value of variable xy, it is normally
written
as : xy =xy+1;
by using increment operator “++” it is written as: xy++;
 The increment operator can be written either before or
after the variable . If it is written before the variable , it is known
as “prefixing”. If it is written after the variable it is known as
“post fixing” .
 Prefix & Postfix operators have different effects when they
are used in expressions.

Mr.Nadeem Akhtar 17
Prefix increment operator
 When an increment operator is used in prefix mode in an expression it adds “1” to
the value of variable before the value of the variable is used in the expression.
 For example, Program 01-18: in this program “1” will be added to the value of c before it
is used in the expression.Thus after execution, the sum will be equal to 6 and value of c will be
4.
#include<iostream.h>
#include<conio.h>
int main()
{ clrscr();
int a,b,c,sum;
char q;
a=1;
b=1;
c=3;
sum=a+b+ (++c);
cout<<“sum =“<<sum<<endl;
cout<<"press any character to exit program =";
cin>>q;
return 0;
}

Mr.Nadeem Akhtar 18
Postfix Increment operator
 When an increment operator is used in postfix mode in an expression it adds “1” to the value
of the variable after the value of the variable has been used in the expression.
 For example, // in this case “1” will be added to the value of c after its existing value has
been used in the expression.Thus after execution, the sum will be equal to 5 and value of c will
be 4.
#include<iostream.h>
#include<conio.h>
int main()
{ clrscr();
int a,b,c,sum;
char q;
a=1;
b=1;
c=3;
sum=a+b+ c++;
cout<<“sum =“<<sum<<endl;
cout<<"press any character to exit program =";
cin>>q;
return 0;
}

Mr.Nadeem Akhtar 19
The Decrement Operator (--)

 The decrement operator is represented by a double


minus (--) sign.
 It is used to subtract “1” from the value of an integer
variable.
 For example, to subtract “1” from the value of a
variable xy, the decrement statement is written as:
xy--; or –xy;

Mr.Nadeem Akhtar 20
Prefix Decrement Operator
 When a decrement operator is used in prefix mode in an expression it subtract “1” from the
value of the variable before the value of the variable is used in the expression.
 For Example, in this program “1” will be subtracted from the value of c before it is used in the
expression.Thus after execution, the sum will be equal to 4 and value of c will be 2.
#include<iostream.h>
#include<conio.h>
int main()
{ clrscr();
char q;
int a,b,c,sum;
a=1;
b=1;
c=3;
sum=a+b+ (--c);
cout<<“sum =“<<sum<<endl;
cout<<“c =“<<c;
cout<<"press any character to exit program =";
cin>>q;
return 0;
}

Mr.Nadeem Akhtar 21
Postfix Decrement Operator
 When a decrement operator is used in postfix mode in an expression it subtracts “1” from the value
of the variable after the value of the variable has been used in the expression.
 For example, // in this case “1” will be subtracted from the value of c after its existing value has
been used in the expression.Thus after execution, the sum will be equal to 5 and value of c will be 2.
#include<iostream.h>
#include<conio.h>
int main()
{ clrscr();
int a,b,c,sum;
char q;
a=1;
b=1;
c=3;
sum=a+b+ c--;
cout<<“sum =“<<sum<<endl;
cout<<“c =“<<c;
cout<<"press any character to exit program =";
cin>>q;
return 0;
}

Mr.Nadeem Akhtar 22

You might also like