130 likes | 347 Views
Selection. Boolean. What is Boolean ?. Boolean is a set with only two values : true false true and false are standard identifiers in Pascal, called Boolean constants. Boolean Expression. Boolean expressions are always in the form of
E N D
Selection Boolean
What is Boolean ? • Boolean is a set with only two values : • true • false • true and false are standard identifiers in Pascal, called Boolean constants
Boolean Expression • Boolean expressions are always in the form of • <expression-1> <relational operator> <expression-2> • expression-1 and expression2 could be any valid expressions • The value of a Boolean expression is either true or false
Rules on Boolean Expression • On each side of the relational operator, there must be one and only one expression (operand) • Relational operator have lowest order of precedence, therefore all the calculation of both operands are performed before the relational operation • Both operands must be of the same type
Rules on Boolean Expression • When comparing two character/string, the ASCII values of the operands are compared. • The comparison of character/string starts from the first character of both operands. If the first characters are the same, the second characters are compared, until there is different.
Examples false true false false true false false
Boolean Data Type • Boolean is one of the data types in Pascal (like integer, real, string ……) • A Boolean variable contains one of the two possible values: true or false • Example: • var bool1, bool2 : boolean; • bool1 := 1 > 3; • bool2 := false;
Boolean Operators • Operate on Boolean expressions • Perform logical operations • Three Boolean operators: • not • and • or
Truth Table and Operator • Value is true only if both operands are true • Otherwise, the value is false
or Operator • Value is true if any one of the two operands is true • Value is false only when both operands are false
not Operator • not is an unary operator (operator with only one operand) • Value is the negation of the operand