160 likes | 259 Views
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
E N D
Relational operators Logic Expressions, &If/else statements Shieu-Hong LinMATH/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 != Not equal to
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
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
Logical operators Logical operators | | ( logical or) && ( logical and) !(not)
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
If statements: If statements in C++ do something when a logic expression is true Case 1: if ( … ) { //Multiple statements as a block … … }
If statements, an example int x=10; if ( x>10 ) {cout<< “The value of x is “ << x <<endl; cout<< x << “ is greater than 10”; }
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”;
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”;
If statements, an example int x=10; if ( x>10 ) {cout<< “The value of x is “ << x <<endl; cout<< x << “ is greater than 10”; }
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”; }
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”;
If statements: Case 2: if ( … ) { //Multiple statements as a block … … } else { //Multiple statements as a block … … }
If statements: Case 3: if ( … ) { //Multiple statements as a block … } else if ( … ) { //Multiple statements as a block … } else { //Multiple statements as a block … }
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."; }