if a < b then begin
a := b;
b := 0;
end else
a := -b;
begin, end used to group multiple statements.
Result is a compound statement.
Optional when a branch contains only one statement.
C, C++, Java, and gobs of others
if (a < b) {
a = b;
b = 0;
} else
a = -b;
Uses curly braces instead of begin, end keywords.
Ada, some others:
if a < b then
a := b;
b := 0;
else
a := -b;
end if
No separate grouping; part of the statement.
Eliminates the option not to group a singleton (good idea).
End has to say what it's closing. Reduces difficulty finding errors
when you leave out a close.
Python, all by itself:
if a < b:
a = b
b = 0
else:
a = -b
Bracketing accomplished by indents. (If you don't indent
properly, it won't compile.)
No bracketing markers are used.
Short-circuit evaluation is also a variety of control.
Switch or case
Earliest form was a computed goto: GO TO (80, 100, 10, 35), IDING - JBAT
C, C++, Java: Cases must be broken.
switch(x) {
case 1:
type = "control";
break;
case 2:
type = "data";
break;
case 3:
case 4:
case 5:
type = "attribute";
break;
default:
type = "error";
}
The newer Go language keeps the C-like syntax, but does not require
break.
switch x {
case 1:
type = "control";
case 2:
type = "data";
case 3,4,5:
type = "attribute";
default:
type = "error";
}
Pascal, Ada, Ruby, others: Various syntax, but again no break is
required (that's a C thing). Ada:
case x is
when 1 => type := "control";
when 2 => type := "data";
when 3|4|5 => type := "attribute";
when others => type := "error";
end case;
Variations.
In in C, and traditionally, the cases had to be constants, the type
of the expression limited to integer or some other integral type, and
cases could not be repeated.
This allows a more efficient translation than the equivalent of
if/else if/else if/...else
Ada adds the requirement that you must cover all possible values of
the case expression (using the default mark others takes care of this).
Java follows C, but at some point allowed switching on String since
it's so blasted convenient.
In C, each case executes from its label to the end of the switch.
That is why you need to use break at the end. Most languages outside
the C family end each case at the beginning of the next.
C's rules allow you to run one branch in several cases by stacking
the case statements without a break. Languages that don't use
break usually provide a range and/or list syntax to combine cases
values for one branch.
Some languages, including Go and many interpreted langauges,
allow cases to have more general types,
or to be expressions, or to be repeated (the first match wins).
There is much variation.
Loops
While loop -- most general.
Counted for loop (Pascal): for a := 1 to 10 do
For-each style loops
Go through some sort of list or collection.
Started in interpreted and scripting languages; now entering compiled
languages. Now very common. C++:
int s = 0;
for(int i: { 4, 9, 18, 3, 22, 17 }) {
s += i;
}
Variations between language in syntax and the type of collection
allowed.
Break statement.
Continue statement.
I/O
Binary or character.
Binary: Data are written exactly as stored.
Character: Data are written as character strings.
Free format (unformatted)
Formatted: Fixed-width fields
Standard streams.
Sequential or random access.
Syntactic class
Statement (FORTRAN)
Library call (Most common).
Privileged function call (Pascal).
Error handling
Pascal: aborts on bad conversions.
Reporting through a return value (C).
Current trend is to use exceptions. Some languages allow both
Exceptions
An error condition which the operation cannot handle itself.
Opinions differ about what events are exceptions.
Exception-less languages.
Return an error value (typically -1 or nil). Perhaps an additional
return value if the language permits.
Extra output parameter describing success.
Exception support.
Program aborts can be thought of as uncatchable exceptions.
Exception types.
A type of its own (Ada).
An object with a certain base class (Java Throwable).
Any type (C++).
Raising an exception transfers directly to a handler.
Options
How are handlers associated with exceptions?
The try block is familiar.
PL/I on statement assigns the block to the exception type.
C signals are similar.
Does the operation resume after the handler completes?
No: Most languages today.
Yes: PL/I used this. C signals likewise.
Must somehow make the operation become legal after restart.
Really tough for bad input.
Handlers often jump away or exit program to prevent restart.