LAB # O2: OPERATORS AND CONDITIONAL STATEMENT Assignment operator (=) The assignment operator assigns a value to a variable. X=5; Expression y = 2 + x; Increment and decrement (++, --) suffix X++ X-- prefix ++X --X Prepared by: Engr. M.Nadeem Page 1
In the case that the increase operator is used as a prefix (++x) the value, the expression evaluates to the final value of x, once it is already increased. On the other hand, in case that it is used as a suffix (x++) the value stored in x the expression evaluates to the value x had before being increased. Notice the difference: Example 1 Example 2 x = 3; x = 3; y = ++x; y = x++; // x contains 4, y contains 4 // x contains 4, y contains 3 In Example 1, the value assigned to y is the value of x after being increased. While in Example 2, it is the value x had before being increased. Prepared by: Engr. M.Nadeem Page 2
Prepared by: Engr. M.Nadeem Page 3
Sizeof Operator: The syntax of using sizeof is as follows: Conditional Operator: The Syntax of Conditional Operator as follows: Prepared by: Engr. M.Nadeem Page 4
Compare with If Statement Prepared by: Engr. M.Nadeem Page 5
Prepared by: Engr. M.Nadeem Page 6
Class Tasks to be performed 1- What will be the output of the given programs? Program 1: (Assignment Operator) int a, b; a = 10; b = 4; a = b; b = 7; cout << "a:"; cout << a; cout << " b:"; cout << b; Program 2: (Compound Assignment Operators) Prepared by: Engr. M.Nadeem Page 7
int a, b=3; a = b; a+=2; cout << a; // equivalent to a=a+2 Program 3: (Conditional Operator) int a,b,c; a=2; b=7; c = (a>b)? a : b; cout << c << '\n'; Program 4: (Arthmatic Operator) main() int a = 21; int b = 10; int c ; c = a + b; cout << "Line 1 - Value of c is :" << c << endl ; c = a - b; cout << "Line 2 - Value of c is :" << c << endl ; c = a * b; cout << "Line 3 - Value of c is :" << c << endl ; c = a / b; cout << "Line 4 - Value of c is :" << c << endl ; c = a % b; cout << "Line 5 - Value of c is :" << c << endl ; c = a++; cout << "Line 6 - Value of c is :" << c << endl ; c = a--; cout << "Line 7 - Value of c is :" << c << endl ; Program 5: (Relational Operators ) Prepared by: Engr. M.Nadeem Page 8
main() int a = 21; int b = 10; int c ; if( a == b ) cout << "Line 1 - a is equal to b" << endl ; else cout << "Line 1 - a is not equal to b" << endl ; if ( a < b ) cout << "Line 2 - a is less than b" << endl ; else cout << "Line 2 - a is not less than b" << endl ; if ( a > b ) cout << "Line 3 - a is greater than b" << endl ; else cout << "Line 3 - a is not greater than b" << endl ; /* Let's change the values of a and b */ a = 5; b = 20; if ( a <= b ) cout << "Line 4 - a is either less than \ or euqal to b" << endl ; if ( b >= a ) cout << "Line 5 - b is either greater than \ or equal to b" << endl ; Program 6: (Logical operators) main() int a = 5; int b = 20; int c ; if ( a && b ) Prepared by: Engr. M.Nadeem Page 9
cout << "Line 1 - Condition is true"<< endl ; if ( a b ) cout << "Line 2 - Condition is true"<< endl ; /* Let's change the values of a and b */ a = 0; b = 10; if ( a && b ) cout << "Line 3 - Condition is true"<< endl ; else cout << "Line 4 - Condition is not true"<< endl ; if (!(a && b) ) cout << "Line 5 - Condition is true"<< endl ; Program 7: (Size of) int main() cout << "Size of char : " << sizeof(char) << endl; cout << "Size of int : " << sizeof(int) << endl; cout << "Size of short int : " << sizeof(short int) << endl; cout << "Size of long int : " << sizeof(long int) << endl; cout << "Size of float : " << sizeof(float) << endl; cout << "Size of double : " << sizeof(double) << endl; cout << "Size of wchar_t : " << sizeof(wchar_t) << endl; Program 8: (IF Statement) // local variable declaration: int a = 10; // check the boolean condition if( a < 20 ) // if condition is true then print the following Prepared by: Engr. M.Nadeem Page 10
cout << "a is less than 20;" << endl; cout << "value of a is : " << a << endl; Program 9: (if else Statement) // local variable declaration: int a = 100; // check the boolean condition if( a < 20 ) // if condition is true then print the following cout << "a is less than 20;" << endl; else // if condition is false then print the following cout << "a is not less than 20;" << endl; cout << "value of a is : " << a << endl; Program 10: (The if...else if...else Statement): // local variable declaration: int a = 100; // check the boolean condition if( a == 10 ) // if condition is true then print the following cout << "Value of a is 10" << endl; else if( a == 20 ) // if else if condition is true cout << "Value of a is 20" << endl; else if( a == 30 ) // if else if condition is true cout << "Value of a is 30" << endl; Prepared by: Engr. M.Nadeem Page 11
else // if none of the conditions is true cout << "Value of a is not matching" << endl; cout << "Exact value of a is : " << a << endl; Program 10: (Switch Statement) // local variable declaration: char grade = 'D'; switch(grade) case 'A' : cout << "Excellent!" << endl; break; case 'B' : case 'C' : cout << "Well done" << endl; break; case 'D' : cout << "You passed" << endl; break; case 'F' : cout << "Better try again" << endl; break; default : cout << "Invalid grade" << endl; cout << "Your grade is " << grade << endl; Program 11: (Nested if else Statement) // local variable declaration: int a = 100; int b = 200; // check the boolean condition if( a == 100 ) Prepared by: Engr. M.Nadeem Page 12
// if condition is true then check the following if( b == 200 ) // if condition is true then print the following cout << "Value of a is 100 and b is 200" << endl; cout << "Exact value of a is : " << a << endl; cout << "Exact value of b is : " << b << endl; Prepared by: Engr. M.Nadeem Page 13