Skip to main content

Section 2.18 Boolean Relations

Subgoals for evaluating an assignment statement.

  1. Decide order of operations
    1. Decompose as necessary
  2. Determine operator behavior based on operands
    1. Operator and operands must be compatible
  3. Solve arithmetic, expression, or operation
    1. Decompose as necessary

Subsection 2.18.1

Given the following code snippet, evaluate the final statement (the last line). If invalid, give the reason. If valid, what value is assigned to the variable?
alpha = 42
beta = 1
gamma = 5
result = beta <= alpha

Subsection 2.18.2 SG1: Decide order of operations

There are three operators in this expression: two <= and one and (boolean logical AND) operator. The and operator determines if two boolean expressions are both True, and has a very low operator precedence. Specifically, the and precedence is lower than the <= comparison operator precedence, and so that operation will be last. Therefore, the two <= will happen first, in left to right order.

Subsection 2.18.3 SG2: Determine operator behavior based on operands

The first <= will be between two integers, which is a valid comparison that produces a boolean. The second <= is also between two integers, and once again produces a boolean. The resulting boolean values are then used as operands to the and, which will in this case produce a boolean value.

Subsection 2.18.4 SG3: Solve arithmetic, expression, or operation

1. In the first comparison, beta is substituted for 1 and gamma is substituted for 5. Then, the <= determines if 1 is less than or equal to 5 , which it is. Therefore, the expression evaluates to True.
2. In the second comparison, gamma is substituted for 5 and alpha is substituted for 42. Then, the < determines if 5 is less than or equal to 42, which it is. Therefore, the expression evaluates to True.
3. The two boolean values are used as the operands for the and, causing the expression True and True to be evaluated - the result is True, which is assigned to the result variable.
You have attempted of activities on this page.