1 / 11

C++ Revision on Chapter 2,3

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;

franz
Download Presentation

C++ Revision on Chapter 2,3

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C++ Revision onChapter 2,3 By: Fatimah Alakeel

  2. Outline • Boolean Data type • Short Circuit Evaluation • Increment and decrement operators

  3. 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

  4. 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)

  5. 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

  6. 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.

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

More Related