1 / 16

Relational operators Logic Expressions, & If/else statements

Relational operators Logic Expressions, & If/else statements. Shieu -Hong Lin MATH/CS Department. Relational operators. Relational operators for numbers comparing numerical values > Greater than < Less than >= Greater than or equal to <= Less than or equal to == Equal to

Download Presentation

Relational operators Logic Expressions, & If/else statements

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. Relational operators Logic Expressions, &If/else statements Shieu-Hong LinMATH/CS Department

  2. Relational operators Relational operators for numbers • comparing numerical values > Greater than < Less than >= Greater than or equal to <= Less than or equal to == Equal to != Not equal to

  3. Simple logic expressions: comparing numbers The result iseither true or false Examples: int x=3, y=4; (x > y) false (x >= y) false (x-2 < y) true (y-1 < x+1)true (x == y) false (x+1 == y)true (x != y) true (x != y-1) false

  4. Use == for comparing numbers, not = Examples: int x=3, y=6; (x+1 == y) false OK (y == x+1)false OK (x+1 = y) wrongwill not compile (y = x+1)wrongwill compile, but wrongly reset the value of y to 4

  5. Logical operators Logical operators | | ( logical or) && ( logical and) !(not)

  6. Logic operators  complex logic expressions The result iseither true or false Examples: int x=3, y=4; (x > y && x+1 == y) false (x > y || x+1 == y) true ! (x > y ) true

  7. If statements: If statements in C++  do something when a logic expression is true Case 1: if ( … ) { //Multiple statements as a block … … }

  8. If statements, an example int x=10; if ( x>10 ) {cout<< “The value of x is “ << x <<endl; cout<< x << “ is greater than 10”; }

  9. If statements: Avoid mistakes //The following is not the same thing. Why? int x=10; if ( x>10 ) cout<< “The value of x is “ << x <<endl; cout << x << “ is greater than 10”;

  10. If statements: Avoid mistakes //Don’t forget the curly braces if you have more // than one statement to do as a block. //The previous slide is interpreted the same as int x=10; if ( x>10 ) {cout<< “The value of x is “ << x <<endl; } cout<< x << “ is greater than 10”;

  11. If statements, an example int x=10; if ( x>10 ) {cout<< “The value of x is “ << x <<endl; cout<< x << “ is greater than 10”; }

  12. If statements: Avoid mistakes //The following is not the same thing. Why? int x=10; if ( x>10 ); { cout<< “The value of x is “ << x <<endl; cout << x << “ is greater than 10”; }

  13. If statements: Avoid mistakes //The previous slide is interpreted the same as int x=10; if ( x>10 ) ; cout<< “The value of x is “ << x <<endl; cout<< x << “ is greater than 10”;

  14. If statements: Case 2: if ( … ) { //Multiple statements as a block … … } else { //Multiple statements as a block … … }

  15. If statements: Case 3: if ( … ) { //Multiple statements as a block … } else if ( … ) { //Multiple statements as a block … } else { //Multiple statements as a block … }

  16. If statements: intdollarAmount; cin >> dollarAmount; if (dollarAmount == 1) { cout << " You have 1 dollar."; } else if (dollarAmount ==0 || dollarAmount> 1 ) { cout << " You have " << dollarAmount << " dollars"; } else { cout << " Kidding. You cannot have a negative number of dollars."; }

More Related