sjm.imperative
Class ForCommand
java.lang.Object
|
+--sjm.imperative.Command
|
+--sjm.imperative.ForCommand
- public class ForCommand
- extends Command
This command mimics a normal "for" loop, such as:
for (int i = 0; i < limit; i++){
// body
}
This class retains four parameters which the constructors
receive or establish. These parameters are a setup
command, a condition, an end command, and a body command.
The execute method executes a "for" loop,
essentially executing:
for (setup; condition; endCommand){
bodyCommand;
}
|
Constructor Summary |
ForCommand(Command setupCommand,
BooleanTerm condition,
Command endCommand,
Command bodyCommand)
Construct a "for" command from the given setup command,
condition, endCommand, and bodyCommand. |
ForCommand(Variable v,
double from,
double to,
double step,
Command bodyCommand)
Construct a "for" command that iterates the supplied
variable over the doubles from the "from" parameter
to the "to" parameter, stepping by the "step" parameter. |
ForCommand(Variable v,
int from,
int to,
Command bodyCommand)
Construct a "for" command that iterates the supplied
variable over the integers from the "from" parameter
to the "to" parameter. |
|
Method Summary |
void |
execute()
Execute this "for" command. |
java.lang.String |
toString()
Returns a string description of this for command. |
| Methods inherited from class java.lang.Object |
clone,
equals,
finalize,
getClass,
hashCode,
notify,
notifyAll,
wait,
wait,
wait |
setupCommand
protected Command setupCommand
condition
protected BooleanTerm condition
endCommand
protected Command endCommand
bodyCommand
protected Command bodyCommand
ForCommand
public ForCommand(Variable v,
double from,
double to,
double step,
Command bodyCommand)
- Construct a "for" command that iterates the supplied
variable over the doubles from the "from" parameter
to the "to" parameter, stepping by the "step" parameter.
This command will iterate, adding step to the initial
value, ceasing the loop when the value is greater than
the "to" limit.
- Parameters:
Variable - the variable to iteratefrom - the initial value for the variableto - a value not to exceed in the loopstep - the amount to increment byCommand - the command to repeatedly execute
ForCommand
public ForCommand(Variable v,
int from,
int to,
Command bodyCommand)
- Construct a "for" command that iterates the supplied
variable over the integers from the "from" parameter
to the "to" parameter. For example, the following code
segment prints out
"my\n" five times.
Variable i = new Variable("i");
Fact f = new Fact("my");
PrintCommand p = new PrintCommand(f); *
new ForCommand(i, 1, 5, p).execute();
- Parameters:
Variable - the variable to iterateint - the initial value for the variableint - the final value for the variableCommand - the command to repeatedly execute
ForCommand
public ForCommand(Command setupCommand,
BooleanTerm condition,
Command endCommand,
Command bodyCommand)
- Construct a "for" command from the given setup command,
condition, endCommand, and bodyCommand.
- Parameters:
setupCommand - the command to execute before the
"for" loop beginscondition - the condition to check before executing
the bodyendCommand - the condition to check after executing
the bodybodyCommand - the command to repeatedly execute
execute
public void execute()
- Execute this "for" command. This means executing the
setup command and then looping. The loop checks that the
condition it true, executes the body command, and
executes the end command.
- Overrides:
- execute in class Command
toString
public java.lang.String toString()
- Returns a string description of this for command.
- Overrides:
- toString in class java.lang.Object
- Returns:
- a string description of this for command