#include <stdlib.h>
#include <stdio.h>
#include "../command.h"
Go to the source code of this file.
Functions | |
if (memory_IsUndef(ItemResult)) | |
switch (TYPE(ItemResult)) | |
END | SETPROGRAMCOUNTER (PARAMETERNODE) |
if (nToStatement==0) | |
if (nStepStatement) | |
if (nLoopEnd) | |
if (nStepStatement &&OPCODE(nStepStatement)==CMD_FORSTEP) | |
if (TYPE(vStartExpression)==VTYPE_LONG &&TYPE(vEndExpression)==VTYPE_LONG) | |
SETPROGRAMCOUNTER (nLoopEnd) | |
if (memory_IsUndef(*LetThisVariable)||(TYPE(*LetThisVariable)!=VTYPE_LONG &&TYPE(*LetThisVariable)!=VTYPE_DOUBLE)) | |
Variables | |
NODE | nItem |
NODE | nGoForward |
VARIABLE | ItemResult |
nItem = PARAMETERNODE | |
ItemResult = EVALUATEEXPRESSION(nItem) | |
ASSERTOKE | |
NEXTPARAMETER | |
nGoForward = CDR(PARAMETERNODE) | |
END NODE | nItem |
NODE | nGoForward |
VARIABLE | ItemResult |
nItem = PARAMETERNODE | |
ItemResult = EVALUATEEXPRESSION(nItem) | |
ASSERTOKE | |
NEXTPARAMETER | |
nGoForward = CDR(PARAMETERNODE) | |
END END END END END END END END VARIABLE | vStartExpression |
END END END END END END END END VARIABLE | vEndExpression |
END END END END END END END END VARIABLE | vStepExpression |
LEFTVALUE | LetThisVariable |
NODE | nToStatement |
NODE | nStepStatement |
NODE | nLoopStart |
NODE | nLoopEnd |
int | iGoingUp |
long | refcount |
int | bEnterTheLoop |
nToStatement = CDR(pEo->ProgramCounter) | |
nStepStatement = CDR(nToStatement) | |
nToStatement = CAR(nToStatement) | |
nLoopEnd = pEo->CommandArray[nLoopEnd -1].Parameter.CommandArgument.next | |
nLoopEnd = pEo->CommandArray[nLoopEnd-1].Parameter.CommandArgument.Argument.pNode | |
vStartExpression = CONVERT2NUMERIC(vStartExpression) | |
vEndExpression = EVALUATEEXPRESSION(pEo->CommandArray[nToStatement-1].Parameter.CommandArgument.Argument.pNode) | |
ASSERTOKE | |
vEndExpression = CONVERT2NUMERIC(vEndExpression) | |
END LEFTVALUE | LetThisVariable |
VARIABLE | vEndExpression |
VARIABLE | vStepExpression |
VARIABLE | vNewLoopValue |
NODE | nStepStatement |
NODE | nForStatement |
NODE | nLoopStart |
NODE | nLoopEnd |
int | iGoingUp |
int | bContinueLoop |
long | refcount |
int | iError |
vEndExpression = EVALUATEEXPRESSION(PARAMETERNODE) | |
ASSERTOKE | |
vEndExpression = CONVERT2NUMERIC(vEndExpression) | |
NEXTPARAMETER | |
nForStatement = CAR(PARAMETERNODE) | |
NEXTPARAMETER | |
nLoopEnd = CDR(PARAMETERNODE) | |
LetThisVariable = EVALUATELEFTVALUE( pEo->CommandArray[nForStatement-1].Parameter.CommandArgument.Argument.pNode ) | |
ASSERTOKE | |
nStepStatement = CDR(pEo->ProgramCounter) |
|
Definition at line 702 of file while.c. References COMMAND_ERROR_MEMORY_LOW, ERROR(), memory_ReplaceVariable(), NULL, and pEo. |
|
Definition at line 644 of file while.c. References memory_ReplaceVariable(), pEo, RETURN, and SETPROGRAMCOUNTER(). |
|
Definition at line 623 of file while.c. References ASSERTOKE, LONGVALUE(), OPCODE, and pEo. |
|
|
|
Definition at line 583 of file while.c. References pEo. |
|
Definition at line 574 of file while.c. References RETURN, and SETPROGRAMCOUNTER(). |
|
Definition at line 94 of file while.c. References RETURN, and SETPROGRAMCOUNTER(). |
|
|
|
|
|
Definition at line 99 of file while.c. References LONGVALUE(), RETURN, SETPROGRAMCOUNTER(), and STRINGVALUE(). |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DOUNTIL =section loop =title DO UNTIL condition This command implements a looping construct that loops the code between the line T<DO UNTIL> and T<LOOP> util the expression following the keywords on the loop starting line becomes T<true>. =verbatim DO UNTIL expression ... commands to repeat ... LOOP =noverbatim The expression is evaluated when the looping starts and each time the loop is restarted. It means that the code between the T<DO UNTIL> and T<LOOP> lines may be skipped totally if the expression evaluates to some T<TRUE> value during the first evaluation before the execution starts the loop. This command is practically equivalent to the construct =verbatim WHILE NOT expression ... commands to repeat ... WEND =noverbatim You can and you also should use the construct that creates more readable code. See also R<WHILE>, R<DOUNTIL>, R<DOWHILE>, R<REPEAT>, R<DO> and R<FOR>. |
|
|
|
WHILE =section loop =title WHILE condition Implements the 'while' loop as it is usually done in most basic implementations. The loop starts with the command T<while> and finished with the line containing the keyword T<wend>. The keyword T<while> is followed by an expression and the loop is executes so long as long the expression is evaluated T<true>. =verbatim while expression ... commands to repeat ... wend =noverbatim The expression is evaluated when the looping starts and each time the loop is restarted. It means that the code between the T<while> and T<wend> lines may be skipped totally if the expression evaluates to some T<false> value during the first evaluation before the execution starts the loop. In case some condition makes it necessary to exit the loop from its middle then the command R<GOTO> can be used. ScriptBasic implements several looping constructs to be compatible with different BASIC language dialects. Some constructs are even totally interchangeable to let programmers with different BASIC experience use the one that fit they the best. See also R<WHILE>, R<DOUNTIL>, R<DOWHILE>, R<REPEAT>, R<DO> and R<FOR>. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
FOR =section loop =title FOR var=exp_start TO exp_stop [ STEP exp_step ] Implements a FOR loop. The variable T gets the value of the start expression T<exp_start>, and after each execution of the loop body it is incremented or decrement by the value T<exp_step> until it reaches the stop value T<exp_stop>. =verbatim FOR var= exp_start TO exp_stop [ STEP exp_step] ... commands to repeat ... NEXT var =noverbatim The T<STEP> part of the command is optional. If this part is missing then the default value to increment the variable is 1. If =itemize =item the expression T<exp_start> is larger than the expression T<exp_stop> and T<exp_step> is positive or if =item the expression T<exp_start> is smaller than the expression T<exp_stop> and T<exp_step> is negative =noitemize then the loop body is not executed even once and the variable retains its old value. When the loop is executed at least once the variable gets the values one after the other and after the loop exists the loop variable holds the last value for which the loop already did not execute. Thus =verbatim for h= 1 to 3 next print h stop =noverbatim prints T<4>. The expression T<exp_start> is evaluated only once when the loop starts. The other two expressions T<exp_stop> and T<exp_step> are evaluated before each loop. Thus =verbatim j = 1 k = 10 for h= 1 to k step j print h,"\n" j += 1 k -= 1 next print k," ",j,"\n" stop =noverbatim will print =verbatim 1 3 6 7 4 =noverbatim To get into more details the following example loop =verbatim STEP_v = 5 for z= 1 to 10 step STEP_v print z,"\n" STEP_v -= 10 next z =noverbatim executes only once. This is because the step value changes its sign during the evaluation and the new value being negative commands the loop to terminate as the loop variable altered value is smaller then the end value. In other words the comparison also depends on the actual value of the step expression. These are not only the expressions that are evaluated before each loop, but the variable as well. If the loop variable is a simple variable then this has not too much effect. However if the loop variable is an array member then this really has to be taken into account. For example: =verbatim for j=1 to 9 A[j] = 0 next j = 1 for A[j]= 1 to 9 for k=1 to 9 print A[k] next k print j += 1 if j > 9 then STOP next =noverbatim prints =verbatim 100000000 110000000 111000000 111100000 111110000 111111000 111111100 111111110 111111111 =noverbatim so you can see that the loop takes, evaluates, compares and increments the actual array element as the variable T<j> in the sample code above is incremented. The loop variable or some other left value has to stand between the keyword T<FOR> and the sign T<=> on the start line of the loop but this is optional following the keyword T<NEXT>. ScriptBasic optionally allow you to write the variable name after the keyword T<NEXT> but the interpreter does not check if the symbol is a variable of the loop. The role of this symbol is merely documentation of the BASIC code. However, you can not write an array element following the keyword T<NEXT>, only a simple variable name. If the expression T<exp_step> is zero then the loop variable is not altered and the loop is re-executed with the same loop variable value. This way you can easily get into infinite loop. These are fine tuning details of the command T<FOR> that you may need to be aware when you read some tricky code. On the other hand you should never create any code that depends on these features. The loop variable is recommended to be a simple variable and the expressions in the loop head should evaluate the same for each execution of the loop. If you need something more special that may depend on some of the features discussed above then you have to consider using some other looping construct to get more readable code. |
|
|
|
|