This is a presentation for CSC410 on expressions and assignment statements as covered in Concepts of Programming Languages by Robert Sebesta.

• Relational Expressions:
• Relational Expression: A boolean expression formed by comparing the values of two operands using a relational operator
• Some languages do not include a boolean type (C, C++), but the value returned from a relational expression is still a boolean conceptually
• Relational operators are commonly overloaded for a variety of types and will work for most of the primitive types
• Relational operators are given a lower precedence than arithmetic operators so they are evaluated second. (a + 1 <= b * 2)
• ```#!/usr/bin/perl

\$a = "25";
\$b = 25;
\$c = 102;
\$d = "Joe is ";
\$e = "forty";

push @test, "\$a gt \$c"; # interpolated
push @test, '\$a < \$c';
push @test, '\$a eq \$b';
push @test, '\$a == \$b';
push @test, 'print \$d + \$a . " "';
push @test, 'print \$d . \$a . " "';
push @test, 'print \$d + \$e . " "';

foreach \$test (@test) {
print "\$test is ";
(eval \$test) ? print "true\n" : "false\n";
}```
```25 gt 102 is true
\$a < \$c is true
\$a eq \$b is true
\$a == \$b is true
print \$d + \$a . " " is 25 true
print \$d . \$a . " " is Joe is 25 true
print \$d + \$e . " " is 0 true```
• Boolean expressions:
• Boolean operators: Operators used to combine boolean values
• Common boolean operators are:
• AND: '`&&`' or '`&`'
• Inclusive OR: '`||`' or '`|`'
• NOT: '`!`' or '`~`'
• Exclusive OR: '`^`'
• Operator presedence is usually NOT, then AND, then OR
• In pacsal boolean operators have higher precedence than relational, so (a < 5 or a > 0) is not legal because it is interpreted as ((a < (5 or a)) > 0) and (5 or a) is not a valid expression
• In ada boolean operators all have the same presedence and are nonassociative, so (a > b and a < c or k = 5) is not legal. It must be ((a > b and a < c) or k = 5)
• C does not have an explicit boolean type; relational expressions return 0 or 1. This makes (a < b < c) legal, though it is onterpreted as ((0 or 1) < c)
• Short circuit evaluation:
• (x * a) * (b / q + 5); when x is 0 the rest of the expression does not need to be evaluated since it will be 0. Likewise in (a >= 10 && b <= 50), when a is less than 10 the rest need not be computed since (false && anything) = false
• Boolean expressions are often short circuited, meaning that when the final result is known computation is stopped
• This allows certain types of expressions:
```// Simple linear search
for(i = 0; i < length && item[i] != searchItem; i++);```
• This can also introduce errors when function calls are allowed in expressions:
```import java.text.DateFormat;
import java.util.Calendar;

public class test {
public static void main(String[] args) {
DateFormat format = DateFormat.getDateInstance();

Calendar today = Calendar.getInstance();
today.set(2001, Calendar.OCTOBER, 31); // All Hallows Eve
Calendar tomorrow = Calendar.getInstance();
tomorrow.set(2001, Calendar.NOVEMBER, 1); // All Saints Day
while(today.get(Calendar.DATE) < tomorrow.get(Calendar.DATE) &

System.out.println("Pass #1:");
System.out.println("     Today: " + format.format(today.getTime()));
System.out.println("  Tomorrow: " + format.format(tomorrow.getTime()));

today.set(2001, Calendar.OCTOBER, 31); // All Hallows Eve

while(today.get(Calendar.DATE) < tomorrow.get(Calendar.DATE) &&

System.out.println("Pass #2:");
System.out.println("     Today: " + format.format(today.getTime()));
System.out.println("  Tomorrow: " + format.format(tomorrow.getTime()));
}

protected static boolean addDay(Calendar date) {
boolean newMonth = date.get(Calendar.DATE) >= date.getMaximum(Calendar.DATE);
return newMonth;
}
}```
```Pass #1:
Today: Nov 1, 2001
Tomorrow: Nov 1, 2001
Pass #2:
Today: Oct 31, 2001
Tomorrow: Nov 1, 2001```
• Different languages deal with handling side effects differently:
• Modula-2 ignores the issue and all boolean expressions are short circuit
• FORTRAN-77 says any relevant function calls must set affected variables to "undefined". This is difficult to do in general though
• Ada has short circuit `and` and `or` and then complete operators ```and then``` and `or else`
• C, C++ and Java have short circuit `&&` and `||` and then complete operators `&` and `|`
• Assignment Statements:
• Simple Assignment:
• Simple Assignment is of the form <target> <assignment operator> <target>
• FORTRAN, BASIC, PL/1, and C based languages use `=` as the assignment operator
• Algol 60 and many others use `:=`
• Most often a separate operators are used for assignment and equality. In PL/1 both are `=` and (a = b = c) will assign a boolean (b = c) to a
• Multiple Targets:
• In PL/1 (a, b = 0) will assign to both a and b
• In C based languages, the assignment operator returns a value equal to what was assigned. That makes (a = b = 0) assign the same value to both a and b
• This allows for statements like:
`while((ch = getchar()) != EOF);`
• Also though it allows for difficult to track typing errors like:
`if(a = b) // As opposed to if(a == b)`
• Java avoids this issue by only allowing boolean expressions within control structures
• Conditional targets:
• C++ and Java allow (```a > b ? m : n = 5```). This is the same as
```if(a > b) {
m = 5;
} else {
n = 5;
}```
• Compound Assignments:
• First introduced in Algol 68 and later adopted in C based languages
• There is arithmetic compound assignment (`+=`, `-=`, `*=`, `/=`) where (`a += b`) is the same as (`a = a + b`)
• In C based languages there are also binary compound assignments (`|=`, `&=`, `^=`, `~=`)
• Unary Assignments:
• C based languages include special unary increment and decrement operators
• (`a++` is the same as (```a = a + 1```)
• Mixed-Mode Assignments:
• Fortran, C and C++ use coersion to allow most types of assignments
• Java will only allow widening coersions, so a float cannot be assigned to an int without an explicit typecast