Making Decisions

 

Next Topic

 

The WIL Tutorial

§         Topic by Topic

§         The Tutorial Course

§         The Complete WIL Tutorial

 

Getting started

Using WIL

Reference

Notes

 

image\gflag7_shg.gif

 

WIL provides a way to conditionally execute a statement, and that is by using the If ... Endif command. Actually, there are several forms of the If statement -- the structured form and the single statement form.

Structured Forms

If expression
  series of statements
Endif

 

If expression
  series of statements
Else

  series of statements
Endif

 

Single Statement Forms

If expression  Then statement.

If expression  Then statement
  Else statement

 

(We refer to If ... Endif as a command, rather than a function, because functions are followed by parameters in parentheses, while commands are not. Commands tend to be used to control the WIL interpreter.)

 

 

Tutor Example continued...

1. Exit Solitaire.

2. Edit tutor.wbt -Add If/Endif command to check return of the variable.

3. Save the file.

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

 

The use of If ... Endif can easily be illustrated by going back to our WIL program and making these modifications:

 

response=AskYesNo("Really?", "Play Solitaire now?")

If response == @YES

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

Endif

 

However, as this example is a single statement, rather than a series of statements, the single statement structure is more appropriate. There are generally many different ways to perform any task in WIL. With experience you will be able quickly decide the best way to do any task.

 

response=AskYesNo("Really?", "Play Solitaire now?")

If response == @YES Then Run("C:\Program Files\Microsoft Games\Solitaire\Solitaire.exe", "")

 

 

Experiment with additional If structures below or image\golftee_shg.gif
continue on…

 

In this example, we are using If ... Then to test whether the value of the variable response is @YES. If it is @YES, we start Solitaire. If it isn't @YES, we don't. The rule is: if the condition following the If keyword is true or works out to a non-zero value, then the statement(s) following are performed. If the condition following the If keyword is false or works out to a zero value, then the statement(s) following are ignored.

There is something extremely important that you should note about the syntax of these If ... Endif commands: the double equal signs (==). In WIL, a single equal sign (=) is an assignment operator - it assigns the value on the right of the equal sign to the variable on the left of the equal sign. As in:

 

response = AskYesNo("Really?","Play Solitaire now?")

 

This is saying, in English: "Assign the value returned by the AskYesNo function to the variable named response." But in the statement:

 

If response == @YES Then Run("C:\Program Files\Microsoft Games\Solitaire\Solitaire.exe","")

 

we do not want to assign a new value to response, we merely want to test whether it is equal to @YES. Therefore, we use the double equal signs (==), which is the equality operator in WIL. The statement above is saying, in English: "If the value of the variable named response is equal to @YES, then run the program Solitaire.exe." If you used a single equal sign (=) here by mistake, you would get an error message:

WIL_TUTORIAL.png

 

 

 

Note:

(=) this IS that

(==) this EQUALS that

Which is WIL's way of telling you to re-check your syntax.

If you've become confused, just remember that a single equal sign (=) is an assignment operator, used to assign a value to a variable. Double equal signs (==) are an equality operator, used to test whether the values on both sides of the operator are the same.

If you have a problem with one of your WIL programs, make sure to check whether you've used one of these symbols incorrectly. It's a very common mistake, which is why we emphasize it so strongly!

We've seen what happens when the statement(s) following the If condition are true. But what happens when the condition is false? Remember we said that when the If condition is false, the following statement(s) are ignored. There will be times, however when we want to perform an alternate action in this circumstance. For example, suppose we want to display a message if the user decides that he or she doesn't want to play Solitaire. We could write:

 

response=AskYesNo("Really?","Play Solitaire now?")

If response == @YES

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

Else

  Display(5, "", "Game canceled")

Endif

 

Using the single statement If...Then...Else structure the same code would look like:

 

response=AskYesNo("Really?", "Play Solitaire now?")

If response == @YES Then Run("C:\Program Files\Microsoft Games\Solitaire\Solitaire.exe", "")

Else Display(5, "", "Game canceled")

 

When you have only single statements to execute when conditions are true or false, the single statement form may be preferred. However, what would happen if you had several functions you wanted to perform if the user answered Yes? You would end up with something unwieldy:

 

response=AskYesNo("Really?", "Play Solitaire now?")

If response==@YES Then Display(5,"","On your mark")

If response==@YES Then Display(5,"", "Get set ...")

If response==@YES Then Display(5, "", "Go!")

If response==@YES Then Run("C:\Program Files\Microsoft Games\Solitaire\Solitaire.exe", "")

If response==@NO Then Display(5,"","Game canceled")

 

Clearly, the best way of handling this is to use the If... Else... Endif structured form.

 

response=AskYesNo("Really?", "Play Solitaire now?")

If response == @YES

   Display(5, "", "On your mark ...")

   Display(5, "", "Get set ...")

   Display(5, "", "Go!")

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

Else

  Display(5, "", "Game cancelled")

Endif