0% found this document useful (0 votes)
28 views6 pages

Notes For Chapter 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views6 pages

Notes For Chapter 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

Ch.

2
Variable declaration:
type varname;
type var1, var2, …, varn;

Variable initialization:
varname = value;

 Variable can be declared and initialized at the same line of code.


Type varname = initial value;

Example 1:
#include <iostream.h>
int main ( )
{
//declaring variables
int a, b;
int result;
// initialize
a = 5;
b = 2;
result = a - b;
// print out the result:
cout<<a<<b<<endl;
cout << result;
cout << a+1;
// terminate the program:
return 0;
}
Example 2:
# include <iostream.h>
void main ( )
{
int a = 17;
int 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;
}

Example 3:
void main( )
{
int a,b,c;
float v;
a= 5; b=7; c=2;
v=a+b+c/3;
cout<<”Average = “<<v<<endl;
}

Example 4:
#include <iostream.h>
int main( )
{
cout << "3 / 2 + 5.5 = " << 3 / 2 + 5.5 << endl;
cout << "15.6 / 2 + 5 = " << 15.6 / 2 + 5 << endl;
cout << "4 + 5 / 2.0 = " << 4 + 5 / 2.0 << endl;
cout << "4*3+7/5-25.5= "<<4*3+7/5-25.5 << endl;
return 0;
}

Example 5:
#include <iostream>
#include <string>
using namespace std;
void main ( )
{
string s;
s = "This is the initial string content";
cout << s << endl;
s = "This is a different string content";
cout << s << endl;
}

Example 6:
#include <iostream>
#include <string>
using namespace std;
void main()
{
int num1, num2;
double sale; char first;
string str;
num1 = 4;
cout << "num1 = " << num1 << endl;
num2 = 4 * 5 - 11;
cout << "num2 = " << num2 << endl;
sale = 0.02 * 1000;
cout << "sale = " << sale << endl;
first = 'D';
cout << "first = " << first << endl;
str = "It is a sunny day.";
cout << "str = " << str << endl;
}
Input (Read) Statement

Example 1:
Write a C++ program to insert the length(l) and width (w) of a rectangle. The program
should compute and print the area of the rectangle.

void main( )
{
int l,w,a;
cout<<”Please insert the length (l):”;
cin>>l;
cout<<”Please insert the width(w):”;
cin>>w;
a=l*w;
cout<<”The area =”<<a<<endl;
}

Example 2:
Write a C++ program to insert three integer values, compute the sum and average of
these values, and print it.

#include <iostream.h>
void main()
{
int x,y,z;
int sum,
float avrg;
cout<<"Insert the value of x:";
cin>>x;
cout<<"Insert the value of y:";
cin>>y;
cout<<"Insert the value of z:";
cin>>z;
sum=x+y+z;
avrg=sum/3;
cout << "Sum= " << sum << endl;
cout << "Average = " << avrg << endl;
}

Example 3:
Write a C++ program to insert the number of Kilometers (Km) then convert it to
equivalent number of meters. (1 Km = 1000 m).

#include <iostream.h>
void main()
{
int km,m;
cout<<”Please insert Km:”;
cin>>km;
m = km * 1000;
cout<<”The meters = “<<m<<endl;
}

Increment & Decrement Operators


 Unary operators
 Increment operator: increment variable by 1
− Pre-increment: + + variable
− Post-increment: variable + +
 Decrement operator: decrement variable by 1
− Pre-decrement: - - variable
− Post-decrement: variable - -

Example 1:
What is the output of the following code?
#include <iostream.h>
void main()
{
int a=4;
cout<<"a= "<<a<<endl<<endl;
a++;
cout<<"after a++, a= "<<a<<endl<<endl;
++a;
cout<<a<<endl;
a--;
cout<<a<<endl;
--a;
cout<<a<<endl;
}

Example 2:
What is the output of the following code?
#include <iostream.h>
void main()
{
int a=7;
cout<<a<<endl<<endl;
cout<<a++<<endl<<endl;
cout<<a<<endl<<endl;
cout<<++a<<endl<<endl;
cout<<a<<endl<<endl;
}
Compound assignment statement

Special assignment statement.


var1 operator = var2;  var1 = var1 operator var2;

Example:
x+=y;  x = x + y;
a*=b;  a = a * b;
y*=++x;  y = y * (++x);

Order of precedence:
1. ()
2. Pre-increment, pre-decrement
3. * / %
4. + -
5. Compound operators
6. Assignment
7. Post-increment, post –decrement

Example 1:
What are the values of i and j after executing the following code?

i = 7;
j = 4 + --i;
What are the values of i and j after executing the following code?
i = 7;
j = 4 + i--;
?

What are the values of i and j after executing the following code?
i = 7;
j = (4 + i)--; //error
cout<<i<<j;
?

Example 2:
What are the values of x and y after executing the following code?
a)
int x=1; double y=1.5;
y*=++x;
y=y*(++x)=3 x=2 & y=3

b)
y=1.5; x=1;
y*=x++;
y=y*(x++)=1.5x=2 & y= 1.5

c)
y=1.5; x=1;
y*=x++ +1;
y=y*(x++ +1)x=2 & y=3;

d)
y=1.5; x=1;
y=y*x++ +1;
 x=2 & y=2.5

Is it the same like y*=x++ +1; ??? NO

e)
x=1; y=2;
y*=++x+1;
 x=2 & y=6

Example 3:
What is the output of the following code?

#include <iostream.h>
void main()
{
int a=4;
cout<<"value of a="<<a<<endl;
cout<<"value of a++ + 5 is "<<a++ + 5<<endl;
cout<<"new value of a="<<a<<endl;
cout<<"value of ++a+5 is "<< ++a+5<< endl;
cout<<"new value of a="<<a<<endl;
cout<<"value of (++a)++ +5 is "<<(++a)++ +5<<endl;
cout<<"new value of a= "<<a<<endl;
}
value of a= 4
value of a++ + 5 is 9
new value of a= 5
value of ++a+5 is 11
new value of a= 6
value of (++a)++ +5 is 12
new value of a= 8

You might also like