Output:: Prepared By: Mohammad H. Ryalat
Output:: Prepared By: Mohammad H. Ryalat
#include <iostream.h>
void main ( )
{
int a, b;
a = 10;
b = 4;
a = b;
b = 7;
cout << "a:";
cout << a;
cout << " b:";
cout << b;
}
Ex2
Read the following.
a = 2 + (b = 5);
is equivalent to:
b = 5;
a = 2 + b;
Output: 5
int B=3;
int A=++B;
cout<<A<<"\t"<<B;
Output: 4 4
int B=3;
int A=B++;
cout<<A<<"\t"<<B;
Output: 3 4
Ex5
Read the following.
Suppose that a=2, b=3 and c=6,
Be careful! The operator = (one equal sign) is not the same as the operator == (two equal
signs), the first one is an assignment operator (assigns the value at its right to the variable
at its left) and the other one (==) is the equality operator that compares whether both
expressions in the two sides of it are equal to each other. Thus, in the last expression
((b=2) == a), we first assigned the value 2 to b and then we compared it to a, that also
stores the value 2, so the result of the operation is true.
Ex6
Read the following.
!(5 == 5) // evaluates to false because the expression at its right (5 == 5) is true.
!(6 <= 4) // evaluates to true because (6 <= 4) would be false.
!true // evaluates to false
!false // evaluates to true.
Ex7
Read the following.
Conditional operator ( ? )
a=2;
b=7;
c = (a>b) ? a : b;
cout << c;
Output: 7
Ex8
Read the following.
int i;
float f = 3.14;
i = (int) f;
cout<< i<<"\t"<<f;
Output: 3 3.14
Ex9
Read the following.
sizeof()
int a = sizeof (char);
int b = sizeof (long);
short c = 27;
int d = sizeof(c);
cout<<a<<"\t"<<b<<"\t"<<d;
Output: 1 4 2
Ex10
Read the following.
short is equivalent to short int and long is
equivalent to long int. The following two
variable declarations are equivalent:
short Year;
short int Year;
Ex11
Read the following.
int x =11;
cout<<x<<"\t"<< 50;
Output: 11 50
You can use cin to request more than one datum input from the user:
is equivalent to:
cin >> a;
cin >> b;
Ex13
Read the following.
char m1 = 97, y =90;
cout<<m1<<"\t";
char m2 = ++y + 7;
cout << (int)m1;
cout<<"\t"<< m2;
Output: a 97 b
Ex14
Read the following.
bool bValue = true;
cout << bValue << endl; Output
cout << !bValue << endl; 1
0
0
bool bValue2 = false;
1
cout << bValue2 << endl;
cout << !bValue2 << endl;
Ex15
Which one is a legal identifier (correct variable name) and which one
is illegal identifier (incorrect variable name)?
1) int sum;
2) intm;
3) int cats=5, dogs=5;
9) int void = 5;
10) int nAngle;
11) int 3some;
9) void is a keyword.
11) Variable names can not start with a number.
Ex16
Read the following.
One of the most common uses for boolean variables is inside if
statements:
Dont forget that you can use the logical not operator to reverse a boolean
value: