1 / 26

Lecture 4 Conditions and Logical Expressions Dr. Hebbat Allah A. Elwishy

Lecture 4 Conditions and Logical Expressions Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor Email: hbbtllh_elwishy@yahoo.com. Lecture 4 Topics. Using Relational and Logical Operators to Construct and Evaluate Logical Expressions If-Else Statements. Flow of Control.

Download Presentation

Lecture 4 Conditions and Logical Expressions Dr. Hebbat Allah A. Elwishy

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. Lecture 4 Conditions and Logical Expressions Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor Email: hbbtllh_elwishy@yahoo.com

  2. Lecture 4 Topics • Using Relational and Logical Operators to Construct and Evaluate Logical Expressions • If-Else Statements

  3. Flow of Control • the order in which program statements are executed WHAT ARE THE POSSIBILITIES. . .

  4. Flow of Control • is Sequential unless a “control structure” is used to change that • there are 2 general types of control structures: Selection (also called branching) Repetition(also called looping)

  5. C++ control structures • Selection if if . . . else switch • Repetition for loop while loop do . . . while loop

  6. Control Structures use logical expressions which may include: 6 Relational Operators < <= > >= == != 3 Logical Operators ! && ||

  7. 6 Relational Operators are used in expressions of form: ExpressionAOperatorExpressionB temperature > humidity B * B - 4.0 * A * C > 0.0 abs (number ) == 35 initial != ‘Q’

  8. int x, y ; x = 4; y = 6; EXPRESSIONVALUE x < y true x + 2 < y false x != y true x + 3 >= y true y == x false y == x+2 true

  9. Operator Meaning Associativity ! NOT Right *, / , % Multiplication, Division, Modulus Left + , - Addition, Subtraction Left < Less than Left <= Less than or equal to Left > Greater than Left >= Greater than or equal to Left == Is equal to Left != Is not equal to Left && AND Left || OR Left = Assignment

  10. LOGICAL EXPRESSION MEANING DESCRIPTION ! p NOT p ! p is false if p is true ! p is true if p is false p && q p AND q p && q is true if both p and q are true. It is false otherwise. p || q p OR q p || q is true if either p or q or both are true. It is false otherwise.

  11. Be Careful • What Numbers Satisfy: (x < 1) && (x > 10) (x < 1) || (x > 10) (x < 10) || (x > 1) (x < 10) && (x > 1)

  12. What is the value? int age, height; age = 25; height = 70; EXPRESSION VALUE !(age < 10) ? !(height > 60) ?

  13. Short-Circuit Example int age, height; age = 25; height = 70; EXPRESSION (age > 50) && (height > 60) false Evaluation can stop now because result of && is only true when both sides are true. It is already determined that the entire expression will be false.

  14. More Short-Circuiting int age, height; age = 25; height = 70; EXPRESSION (height > 60) || (age > 40) true Evaluation can stop now because result of || is true if one side is true. It is already determined that the entire expression will be true.

  15. What happens? int age, weight; age = 25; weight = 145; EXPRESSION (weight < 180) && (age >= 20) true Must still be evaluated because truth value of entire expression is not yet known. Why? Result of && is only true if both sides are true.

  16. What happens? int age, height; age = 25; height = 70; EXPRESSION ! (height > 60) || (age > 50) true false Does this part need to be evaluated?

  17. Write an expression for each taxRate is over 25% and income is less than 20000 temperature is less than or equal to 25 or humidity is less than 70% age is over 21 and age is less than 60 age is 21 or 22

  18. Some Answers (taxRate > .25) && (income < 20000) (temperature <= 25) || (humidity < .70) (age > 21) && (age < 60) (age == 21) || (age == 22)

  19. Operator precedence • ! • * / % • + - • < <= >= > • == != • && • ||

  20. If-Else Syntax if ( Expression ) StatementA else StatementB NOTE: StatementA and StatementB each can be a single statement, a null statement, or a block.

  21. if - else provides two-way selection between executing one of 2 clauses (the if clause or the else clause) TRUE FALSE expression if clause else clause

  22. Use of blocks recommended if ( Expression ) { } else { } “if clause” “else clause”

  23. Using the if Statement if ( boolean_expr ) statement1; [else statement2]; General : if (i % 2 == 0) cout << "Even"; else cout << “Odd"; … Examples: if (i % 2 == 0) { cout << i; cout << " is even"; }

  24. Example Write an interactive program that contains an if statements and may be used to compute the area of a square (area = side2 ) or triangle (area = 1/2 x base x height) after prompting the user to type the first character of the required figure name (t for triangle or s for square).

  25. Example #include <iostream> int main() { char c; float b, h, s, area;   cout << " Enter the figure name (t for triangle or s for square) : ";   cin >> c;   if ( c == 't' ) { cout << " Enter the base "; cin >> b; cout << "Enter the height"; cin >> h; area = 1/2 * h * b; cout << " area of triangle = " << area;   }

  26. Example else if ( c == 's') { cout << "Enter the side measure"; cin >> s; area = s * s; cout << " area of square = " << area; } else cout << "" error in figure name"; return 0; }

More Related