360 likes | 369 Views
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
E N D
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 <Operand 1> <Relational operator> <Operand 2>
May be =, >, <, <>, >=, or <=. Syntax of Boolean expressions <Operand 1> <Relational operator> <Operand 2>
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.
4.2 Compound Boolean Expressions • Simple Boolean expressions may be joined by logical operators to form compound Boolean expressions. • Logical operators: or, not. and,
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> ...
Class Practise Workbook P. 38 Q.1-2
Example 4.5 Suppose Age = 19, Sex = ‘F’. Evaluate the value of the Boolean expression not (Sex =‘M’) or (Age > 12)and (Age < 18).
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.
4.3 Conditional Statements • Conditional statements allow the execution of one of a number of possible operations. • Conditional statements include: • if statements • case statements
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>
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>
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>
Sometimes, more than one statement is required to be executed under the condition. • In such case, a compound statement should be used.
A compound statement consists of two or more simple statements enclosed by a pair of reserved words begin and end.
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
Flowchart of an if-then-else statement with compound statements
If-then Statements Syntax of if-then statements if <Condition> then <Statement>
Syntax of if-then statements with compound statements if <Condition> thenbegin <Statement 1>; <Statement 2>; <Statement N> end
Class Practise Workbook P. 41 Q.5 Workbook P. 42 Q.1(b), 2,4
Nested If Statements • An if statement may be nested by another if statement to form a nested if statement.
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>
Reserved word else should be matched to the nearest preceding if in nested if statements.
Case Statements • A case statement is preferred for selection involving more than three alternatives.
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
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
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
Only one selection on the list of statements will be executed. • No statements will be executed if no case label match the selector.
Class Practise Workbook P. 46 Q.6