Additional NewtonScript Features

if/then/else Statements


if expression then statement
if expression then statement1 else statement2


NewtonScript's if statements are similar to those found in other languages. Here are some examples:

if a < b then
   min := a
else
   min := b;

if a < b then
   x := y;

min := if a < b then
      a
   else
      b;
An if statement has a value--like all NewtonScript statements. Where an else is lacking, the value of the if statement is either:

If there is an else, then the value of the if statement is either:

If there is an else, it is associated with the closest preceding if. For instance,

if a < b then if b < c then x := y else y := x;
is parsed as

if a < b then
   if b < c then
      x := y
   else
      y := x
rather than as

if a < b then
   if b < c then
      x := y
else
   y := x
There is one other important point about if statements:

This optional syntax makes migration from other languages easier--some languages prohibit a semicolon at this point (like Pascal), while others require it (like C). The preferred approach is not to use the optional semicolon, however.


An online version of Programming for the Newton using Macintosh, 2nd ed. ©1996, 1994, Julie McKeehan and Neil Rhodes.

Last modified: 1 DEC 1996