110 likes | 516 Views
C++ Revision on Chapter 2,3. By: Fatimah Alakeel. Outline. Boolean Data type Short Circuit Evaluation I ncrement and decrement operators. Boolean Data Type in C++. Syntax: bool variable_name ; Boolean value is: True = 1 False = 0 Example: bool flag = true;
E N D
C++ Revision onChapter 2,3 By: Fatimah Alakeel
Outline • Boolean Data type • Short Circuit Evaluation • Increment and decrement operators
Boolean Data Type in C++ • Syntax: boolvariable_name; • Boolean value is: • True = 1 • False = 0 Example: bool flag = true; Cout<< flag ; // prints 1 Cout<< !flag ; // prints 0
Boolean Data Type in C++ • If a Boolean value is converted to an integer value • true becomes 1 • false becomes 0 Example: bool flag = true ; x= (int) flag; cout<<x; //1 will be printed • If an integer value is converted to a Boolean value • 0 becomes false • non-zero becomes true Example: int x=39; bool flag; flag = (bool) x; cout<< flag; //1 will be printed (true) Example: int x=0 ; bool flag; flag = (bool) x; cout<< flag; //0 will be printed (false)
Short Circuit Evaluation • It means that the second argument is only executed or evaluated if the first argument is not enough to determine the value of the expression: • when the first argument of the AND function evaluates to false, the overall value must be false; • the right side of the logical AND expression is evaluated ONLY if the left side is true • when the first argument of the OR function evaluates to true, the overall value must be true • the right side of the logical OR expression is evaluated ONLY if the left side is false A.AlOsaimi
Increment and Decrement Operators • If increment and decrement operators are used in an expression within a statement (e.g. if, cout) their effect depends on the short circuit evaluation.
Increment and Decrement Operatorsand “AND” Operator • If the first part evaluates to TRUE in an AND statement, the effect of the increment/decrement will effect the second part. • If the first part evaluates to FALSE in an AND statement, the effect of the increment/decrement will not effect the second part. int x=5, y=4; if ( (--x) && (x==y)) // (4 && 4==4) => true && true cout<<"if is executed"<<endl; cout<< "x="<<x<<endl; //x=4 x=5; y=4; if ((x==y)&& (--x)) // (false && --x) => false, so x is not changed cout<<"if is executed"<<endl; //this will not be executed cout<< "x="<<x<<endl; //x=5
AND Example b=5; cout<<"The result="<<((b==4)&&(--b))<<endl; // ( 0 && --b) ==> first part is false, the second part (--b) is not evaluated cout<<"b="<<b<<endl; // b is not changed b=5 b=5; cout<<"The result="<<((b==5)&&(--b))<<endl; // ( 1 && --b) ==> first part is true, the second part (--b) is evaluated cout<<"b="<<b<<endl; //b is changed b=4
Increment and Decrement Operatorsand “OR” Operator • If the first part evaluates to TRUE in an OR statement, the effect of the increment/decrement will not effect the second part. • If the first part evaluates to FALSE in an AND statement, the effect of the increment/decrement will effect the second part. int x=5, y=4; if ( (--x) || (--y)) // (4 || --y) => true || --y => --y is not executed due to short circuit evaluation cout<<"if is executed"<<endl; cout<< "x="<<x<<endl; // x=4 cout<< "y="<<x<<endl; // y=4 x=5; y=4; if ((x==y)|| (--x)) // (false || --x) => false so the second part is evaluated cout<<"if is executed"<<endl; cout<< "x="<<x<<endl; //x=4
OR Example b=5; cout<<"The result="<<((b==4)||(--b))<<endl; // ( 0 && --b) ==> first part is false, the second part (--b) must be evaluated cout<<"b="<<b<<endl; // b is changed b=4 b=5; cout<<"The result="<<((b==5)||(--b))<<endl; // ( 1 && --b) ==> first part is true, the second part (--b) is not evaluated cout<<"b="<<b<<endl; //b is not changed b=5
Think about this … • Example: assume i=1; if (( --i == 0) && ( ++i ==1)) { Cout<< “true branch” << endl; Cout<< “i=” << i << endl; } else{ Cout<< “false branch”; Cout<< “i=” << i << endl; } OUTPUT: true branch i=1 i=0