1 / 36

Chapter 4

Chapter 4. Boolean Expressions and Conditional Statements. 4.1 Boolean Expressions. Consists of 2 operands joined by a relational operator. May be arithmetic expressions, characters, string or variables and their types must be compatible. Syntax of Boolean expressions

Download Presentation

Chapter 4

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. Chapter 4 Boolean Expressions and Conditional Statements

  2. 4.1 Boolean Expressions • Consists of 2 operands joined by a relational operator.

  3. May be arithmetic expressions, characters, string or variables and their types must be compatible. Syntax of Boolean expressions <Operand 1> <Relational operator> <Operand 2>

  4. May be =, >, <, <>, >=, or <=. Syntax of Boolean expressions <Operand 1> <Relational operator> <Operand 2>

  5. Relational Operators

  6. Boolean Constants and Boolean Variables • Boolean is a kind of logical data type with value either TRUE or FALSE. • Boolean constants and Boolean variables are in type Boolean.

  7. 4.2 Compound Boolean Expressions • Simple Boolean expressions may be joined by logical operators to form compound Boolean expressions. • Logical operators: or, not. and,

  8. May be not. May be and, or, not. Syntax of compound Boolean expressions <Logical operator 1> <Simple Boolean expression 1> <Logical operator 2> <Simple Boolean expression 2> ...

  9. Class Practise Workbook P. 38 Q.1-2

  10. Example 4.5 Suppose Age = 19, Sex = ‘F’. Evaluate the value of the Boolean expression not (Sex =‘M’) or (Age > 12)and (Age < 18).

  11. Solution not (Sex = ‘M’) or (Age > 12) and (Age < 18) not (‘F’ = ‘M’) or (19 > 12) and (19 < 18) not FALSE or TRUE and FALSE TRUE or TRUE and FALSE TRUE or FALSE TRUE The value is TRUE.

  12. 4.3 Conditional Statements • Conditional statements allow the execution of one of a number of possible operations. • Conditional statements include: • if statements • case statements

  13. May be a Boolean constant, Boolean variable or Boolean expression. If-then-else Statements Syntax of if-then-else statements if <Condition> then <Statement A> else <Statement B>

  14. Will be executed if <Condition> is TRUE. If-then-else Statements Syntax of if-then-else statements if <Condition> then <Statement A> else <Statement B>

  15. Will be executed if <Condition> is FALSE. If-then-else Statements Syntax of if-then-else statements if <Condition> then <Statement A> else <Statement B>

  16. Flowchart of an if-then-else statement

  17. Sometimes, more than one statement is required to be executed under the condition. • In such case, a compound statement should be used.

  18. A compound statement consists of two or more simple statements enclosed by a pair of reserved words begin and end.

  19. Compound statement Compound statement Syntax of if-then-else statements with compound statements if <Condition> thenbegin <Statement A1>;  <Statement AN> end elsebegin <Statement B1>;  <Statement BN> end

  20. Flowchart of an if-then-else statement with compound statements

  21. If-then Statements Syntax of if-then statements if <Condition> then <Statement>

  22. Flowchart of an if-then statement

  23. Syntax of if-then statements with compound statements if <Condition> thenbegin <Statement 1>; <Statement 2>;  <Statement N> end

  24. Flowchart of an if-then statement with compound statement

  25. Class Practise Workbook P. 41 Q.5 Workbook P. 42 Q.1(b), 2,4

  26. Nested If Statements • An if statement may be nested by another if statement to form a nested if statement.

  27. Syntax of nested if statements if <Condition C1> thenif <Condition C2> then <Statement A1> else <Statement A2> else if <Condition C3> then <Statement B1> else <Statement B2>

  28. Flowchart of a nested if statement

  29. Reserved word else should be matched to the nearest preceding if in nested if statements.

  30. Case Statements • A case statement is preferred for selection involving more than three alternatives.

  31. A variable or an expression of ordinal data types (integer, char or Boolean). Syntax of case statements case <Selector> of <Case label 1> : <Statement 1>; <Case label 2> : <Statement 2>;  <Case label N> : <Statement N> end

  32. Lists of possible values of <Selector>. Syntax of case statements case <Selector> of <Case label 1> : <Statement 1>; <Case label 2> : <Statement 2>;  <Case label N> : <Statement N> end

  33. Simple or compound statements. Syntax of case statements case <Selector> of <Case label 1> : <Statement 1>; <Case label 2> : <Statement 2>;  <Case label N> : <Statement N> end

  34. Only one selection on the list of statements will be executed. • No statements will be executed if no case label match the selector.

  35. Class Practise Workbook P. 46 Q.6

More Related