Control of Program Flow

 

Next Topic

 

The WIL Tutorial

§         Topic by Topic

§         The Tutorial Course

§         The Complete WIL Tutorial

 

 

image\gflag8_shg.gif

 

The linear flow of statements (executing one statement after another) is not always preferred or possible. WIL provides the standard set of flow control commands: For, While, Switch and GoSub. These commands give you the ability to redirect the flow of control in your WIL programs.

The For command controls the looping of a block of code based on an incrementing index. The While command conditionally and/or repeatedly executes a series of statements. The Switch statement allows selection among multiple blocks of statements. GoSub transfers control to another point in the WIL program and saves the location for a Return statement.

Let's explore the use of these commands further. Perhaps you need to break your Solitaire habit by limiting your time of play (it has, by now, become obvious to your boss and co-workers that, ever since you got this program, all you do is play solitaire).

 

Getting started

Using WIL

Reference

Notes

 

 

 

First you need to ask yourself how long you would like to play by adding the following line to the top of your script.

mins = AskLine("Solitaire","How many mins do you want to play?","", 0)

 

 

 

Tutor Example continued...

1. Exit Solitaire. (If it is running.)

2. Edit tutor.wbt -Add AskLine to the top of the script.

3. Save the file.

4. Run the wbt by double-clicking on the filename.

image\golftee_shg.gifNext…

 

This will display a message box which prompts you for the number of minutes you would like to play.

Once you enter the desired number of minutes, you could display an additional message as a response to the specific amount of time entered. Switch, as you remember, allows selection from among multiple blocks of statements. Each block of statements is called a case. In the sample below, there are several case statement blocks. Selection of one of the cases is determined by the number of minutes stored in the variable mins . If the number is 3, then case 3 will be executed. All numbers not accounted for will be executed by the default case, mins.

 

 

image\gflag9_shg.gif

 

 

 

1. Exit Solitaire.

2. Edit tutor.wbt to add the Switch statement.

 

 

Type the following code into tutor.wbt. Test the Switch statement by entering a number into the AskLine dialog box. Try running it several times using various numbers.

 

Your code should look like the code below. Remove extra statements.

 

mins = AskLine("Solitaire", "How many mins do you want to play?", "", 0)
mins = Int(mins)
Switch mins
case 0
 Display(5, "", "Game canceled")
 exit
 break
case 1
 Message("Only a minute?", "Wow! You've got willpower.")
 break
case 2
 Message("2 Minutes?", "This isn't much of a break.")
 break
case 3
 Message("3 Minutes?", "You've barely got time to shuffle.")
 break
case 4
 Message("HA,HA,HA", "I dare you to try to beat me.")
 break
case mins ;default case - must be last in the switch
 Message("THAT LONG!!!", "Where did you get all that time?")
 break
EndSwitch
Run("C:\Program Files\Microsoft Games\Solitaire\Solitaire.exe", "")

 

 

image\golftee_shg.gif

Next…

 

 

In our example, each case statement block is composed of three parts; a case statement followed by a number, a series of one or more statements and the break command. If the number behind the case statement matches the number behind the switch statement, then the case block is executed. Once the correct message has been displayed, break terminates the case block and transfers control to the EndSwitch statement.

 

 

image\gflag10_shg.gif
Tutor Example continued...

1. Exit Solitaire.

2. Edit tutor.wbt

Set up variables.

 

 

Now we need to create a timer to track the time elapsed and compare it to the time entered. The While command, which repeats execution of a series of statements by telling WIL, "Do the following while a condition is present," does this job nicely.

 

First let's set up a couple of variables.

goal = mins * 60
timer = 0

 

Add the variables and timing code below the Run statement.

 

Now for the While statement. The first line sets the condition, "While the timer is less than the goal execute this series of statements."

 

While timer < goal
  remain = goal - timer
  WinTitle("Solitaire","Solitaire (%remain% seconds left)")
  TimeDelay(10)
  timer = timer + 10
EndWhile

 

3. Save the file.

4. Run the wbt by double-clicking on the filename..

 

image\golftee_shg.gif Next…

 

The rest of our series of statements include: a computation of the time remaining (remain) to be displayed, a line to display the time remaining in the Solitaire window title bar, a delay statement to allow time to pass, and a statement to calculate the time elapsed. EndWhile marks the end of statements. WIL marches through the While loop until the variable timer exceeds the value of the variable goal.

 

 

image\gflag11_shg.gif
Tutor Example continued...

 

 

So what happens if suddenly your time is up and you're four moves away from winning? Can't have that happening.

We can give ourselves the opportunity to add more time by adding another Askline statement.

 

mins = AskLine("More Time?","Enter additional minutes.",0, 0)

 

 

 

1. Exit Solitaire.

2. Edit tutor.wbt

Add new line to bottom of script.

3. Save the file.

4. Run the wbt by double-clicking on the filename.

image\golftee_shg.gif Next…

 

If a time is entered the timer will need to be used again. Of course, it would be easy to copy that portion of the script and insert it after the new line. However, the same script can be utilized with the assistance of GoSub.

GoSub causes the flow of control to go to another point in the WIL program while remembering its point of origin. The name GoSub is an abbreviation of "Go To Subroutine". You must specify where you want the flow of control to be transferred -- the subroutine name, and you must mark this point with a label. A label is simply a destination address, or marker. The form of the GoSub command is:

 

GoSub label

where label is an identifier that you specify. The same rules apply to label names as to variable names (the first character must be a letter, the label name may consist of any combination of letters and numbers, and the label name may be from 1 to 30 characters long). In addition, the label is preceded by a colon (:) at the point where it is being used as a destination address.

 

 

 

image\glfwater_shg.gif
Tutor Example continued...

In our sample script, we move the timing loop to the bottom of the script, add a label marked :dumdedum above the timing script as the destination address. After EndWhile, add the statement, Return to allow the flow of control to return from the bottom of the GoSub.

1. Exit Solitaire.

2. Edit tutor.wbt-
Add :dumdedum label to bottom. Move timing loop below label.
Add Return after loop. After
Run statement add GoSub dumdedum.

3. Save the file.

4. Run the wbt by double-clicking on the filename.

image\golftee_shg.gif Next…

 

We'll add a GoSub statement in after the Run statement. The GoSub statement is saying, in English "go to the line marked :dumdedum, and continue processing from there, but remember where you came from." When Return is reached, control will be transferred back to the statement after the original GoSub.

 

Notice that the label dumdedum is preceded by a colon as the address, but not on the line where it follows the GoSub keyword. This is important. Although you can have multiple lines in your WIL program which say GoSub dumdedum, you can have only one line marked :dumdedum (just like you can have several people going to your house, but can have only one house with a particular address). Of course, you can use many different labels in a WIL program, just as you can use many different variables, as long as each has a unique name.

 

 

image\gflag12_shg.gif
Tutor Example continued...

1. Exit Solitaire.

 

In addition to changing the message displayed in the "mins=AskLine" statement, a default time has been added. The value returned from the new AskLine will need to be checked. In the example below, "!=" signifies "not equal to". Therefore the line reads, "If mins is not equal to zero then GoSub dumdedum."

 

 

If mins!=0 then GoSub dumdedum

 

 

2. Edit tutor.wbt -
Add check for return of the 2nd
AskLine.
Add
WinClose and Exit. (See code below)

3. Save the file.

4. Run the wbt by double-clicking on the filename.

image\golftee_shg.gif Next…

If a time is returned, GoSub will send execution to the :dumdedum label and the waiting process will begin again. After the time has elapsed, control will be returned to the statement following the GoSub.

The last thing we want to do is end the program with the WinClose function and display a final message.

The Exit command is used to keep the processing from "falling through" to the subroutine at the end of the program. In this case, the dumdedum subroutine sits at the end. Exit causes a WIL program to end immediately and not fall into the dumdedum loop.

 

 

image\gpitstop_shg.gif

 

 

Our altered script has the following appearance from the Run statement to the bottom.

 

Run("C:\Program Files\Microsoft Games\Solitaire\Solitaire.exe", "")

GoSub dumdedum

mins=AskLine("More Time?", "Enter additional minutes", 0, 0)
If mins!=0 then GoSub dumdedum

 

WinClose("Solitaire")
Message("Time's Up", "Get Back to Work!")

Exit

:dumdedum
goal = mins * 60
timer = 0

While timer < goal
 remain = goal - timer
 WinTitle("Solitaire", "Solitaire (%remain% seconds left)")
 TimeDelay(10)
 timer = timer + 10
EndWhile

Return

 

image\golftee_shg.gifNext…

 

 

image\gflag13_shg.gif
Tutor Example continued...

 

 

The sample script could be considered complete at this point. However, the For command has yet to be discussed. The For command is more complex than the previous commands. It controls the looping of a block of code based on an incrementing index. This command is handy if you want to perform a specific code block a particular number of times. The statement says, "Repeat the block of code for each value of a variable from the initial value to the final value, incrementing the variable after each pass of the loop"

1. Edit tutor.wbt
Add code for the
For statement directly after the Run statement.

In the sample below, the size of the Solitaire window is manipulated and displayed 10 times before the window is zoomed to full screen. Each time the loop is executed, the coordinate and size variables (j and k) are altered, and then used in a WinPlace statement ( it's time to start looking up functions in the reference yourself now) to affect the position and size of the Solitaire window.

2. Save the file.

3. Run the wbt by double-clicking on the filename.

This concludes the first part of our tutorial. You now have the building blocks you need to create useful WIL programs. In the second part, which follows, we will look in more detail at some of the WIL functions which are available for your use.

See the image\golftee_shg.gifCompleted WIL file, tutor.wbt, at the end of the WIL TUTORIAL.