Looping contructs:
type dayname = (sunday,monday,...saturday)
workday = monday..friday
var wday:workday
:
wdaymonday
while wday != ? do
: // some processing
wdaysucc(wday)
endwhile
// CAREFUL WITH OTHER CONSTRUCTS
algorithm example
type sexstatus=(male,female)
maritalstatus=(single,married,separated,divorced,widowed)
var quit,done:boolean
sex,gender:sexstatus
mstat1,mstat2:martialstatus
begin
/* Booleans */
quittrue /* Assignment from a Boolean Constant */
donequit /* Assignment from a Boolean Variable */
/* sexstatus */
sexfemale /* Assignment from a Sexstatus Constant */
gendersex /* Assignment from a Sexstatus Variable */
/* maritalstatus */
mstat1single /* from Maritalstatus constant */
mstat2widowed /* from maritalstatus constant */
mstat1mstat2 /* from VAR of type maritalstatus */
for mstat1single to divorced do
mstat2mstat1
/* mstat1 single -> married
married -> separated
separated -> divorced */
endalgorithm // of example program.
(1) Comments about the purpose of sections of code:
/* the following for loop could represent LIFE */
/* of loop that calculates mortgage interest */
import local.units.sdsu.io.*;
import java.lang.*;
import java.io.*;
class Sexstatus
{
static final int MALE =0;
static final int FEMALE =1;
int status;
}
public class Sexy1
{
// Mainline Variable Declarations
static PrintStream output = System.out;
static ASCIIInputStream input = new ASCIIInputStream(System.in);
static final int max=7; // 7 characters only for reply
static Sexstatus sex = new Sexstatus();
static boolean error;
static int i,length;
static char ch;
static char string[] = new char[max];
public static void main(String argv[]) throws IOException
{
do
{
error = false;
Format.print(output,"Sex: ");output.flush();
do
{
ch = input.readChar();
} while (ch == ' '); // skip blanks.
i = 0;
while ( (i<max) && (ch != '\n') )
{
string[i] = Character.toLowerCase(ch);
i++;
ch = input.readChar();
}
if(ch != '\n')
input.flushLine();
ch = string[0]; // TEMP STORAGE FOR EFFICIENCY.
// Assume upper case only
if ( (ch=='f') || (ch=='m') || (ch=='y') )
switch(ch)
{
case 'm': // look for 'ale'
if ( ( string[1] != 'a') || (string[2] != 'l') ||
(string[3] != 'e') ) error = true;
else sex.status = Sexstatus.MALE;
break;
case 'f': // look for 'emale'
if ( (string[1] != 'e') || (string[2] != 'm') ||
(string[3] != 'a') || (string[4] != 'l') ||
(string[5] != 'e') ) error = true;
else sex.status = Sexstatus.FEMALE;
break;
case 'y': // Person has typed YES please ...
Format.print(output,"Animal- What is your sex -\n");
error = true;
break;
}
else error = true;
if ( error ) Format.print(output," Eh?\n");
} while ( error);
if (sex.status==Sexstatus.MALE ) Format.print(output,"I guess you are male.\n");
else Format.print(output,"I deduce you are female.\n");
}
}
import local.units.sdsu.io.*;
import java.lang.*;
import java.io.*;
class Sexstatus
{
static final int MALE =0;
static final int FEMALE =1;
int status;
}
public class Sexy2
{
// Mainline Variable Declarations
static PrintStream output = System.out;
static ASCIIInputStream input = new ASCIIInputStream(System.in);
static final int max = 7;
static final String SMALE ="male";
static final String SFEMALE ="female";
static Sexstatus sex = new Sexstatus();
static boolean error;
static int i,length;
static char ch;
static char string[] = new char[max];
static String response ;
public static void main(String argv[]) throws IOException
{
do
{
error = false;
Format.print(output,"Sex: ");output.flush();
do
{
ch = input.readChar();
} while (ch == ' '); // skip blanks.
i = 0;
while ( (i<max) && (ch != '\n') )
{
string[i] = Character.toLowerCase(ch);
i++;
ch = input.readChar();
}
if(ch != '\n')
input.flushLine();
response = new String(string,0,i);
if(response.equals(SMALE)) sex.status = Sexstatus.MALE;
else if(response.equals(SFEMALE)) sex.status = Sexstatus.FEMALE;
else
{
Format.print(output,"Animal- What is your sex -\n");
error = true;
}
if ( error ) Format.print(output,"Eh?\n");
} while ( error);
if (sex.status==Sexstatus.MALE ) Format.print(output,"I guess you are male.\n");
else Format.print(output,"I deduce you are female.\n");
}
}