This is a presentation for CSC410 on expressions and assignment statements as covered in Concepts of Programming Languages by Robert Sebesta.
#!/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
&&' or '&'||' or '|'!' or '~'^'// Simple linear search for(i = 0; i < length && item[i] != searchItem; i++);
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) &
test.addDay(today));
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) &&
test.addDay(today));
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);
date.add(Calendar.DATE, 1);
return newMonth;
}
}
Pass #1:
Today: Nov 1, 2001
Tomorrow: Nov 1, 2001
Pass #2:
Today: Oct 31, 2001
Tomorrow: Nov 1, 2001
and and
or and then complete operators and
then and or else&&
and || and then complete operators
& and |= as the assignment operator:==
and (a = b = c) will assign a boolean (b = c) to awhile((ch = getchar()) != EOF);
if(a = b) // As opposed to if(a == b)
a > b ? m : n =
5). This is the same as
if(a > b) {
m = 5;
} else {
n = 5;
}
+=, -=, *=,
/=) where (a += b) is the same
as (a = a + b)|=, &=,
^=, ~=)a++ is the same as (a = a +
1)