|

In the if statement's condition, we use a String.Upper action to convert the contents of strMsg to all uppercase characters, and then compare that to "HELLO WORLD!".


We've added an "else" keyword after the first Dialog.Message action. It basically divides the if statement into two parts: the "then" part, that only happens if the condition is true; and the "else" part, that only happens if the condition is false. In this case, the else part uses a Dialog.Message action to tell the user that they didn't type the right message.


6 ) P rev i e w t h e p r oj ec t . T ry c li ck ing on t h e bu tt on a nd t y ping di ff ere n t me ss a g e s in t o t h e inpu t f i e ld.

Depending on what you type, you'll either see the "hello world!" message, or "You didn't type Hello World!".


Note that if you type some variation of "hello world!", such as "hello world!", or "hEllO WorlD!", the capitalization that you type will be preserved in the message that appears. Even though we used a String.Upper action to convert the message string to all uppercase letters in the if statement's condition, the actual contents of the variable remain unchanged.


When the String.Upper action converts a value to all uppercase letters, it doesn't change the value in the variable...it just retrieves it, converts it, and passes it along.


7 ) E x i t t h e p rev i e w.

Now you know how to compare two strings without being picky about capitalization. Programmers call this a case-insensitive comparison, whereas a comparison that only matches perfect capitalization is considered case-sensitive.


Tip: You can also use a String.CompareNoCase action to perform a case-insensitive comparison.


image

Testing a Numeric Value

Another common use of the if statement is to test a numeric value to see if it has reached a certain amount. To demonstrate this, let's create another button that displays a message after you click on it 5 times.


1 ) A dd a no t h er bu tt on obj ec t t o t h e p a g e .

It doesn't matter what button you choose, or where you put the button on the page. We just need something to click on, so we can trigger an On Click event and perform some actions.

2 ) A dd t h e f ollowing s cr ip t t o t h e bu tt on obj ec t ' s On C li ck eve n t: n C li ck s = n C li ck s + 1 ;

B u tt on. S e t T ex t(t hi s , " C li ck c oun t: " .. n C li ck s ) ; i f n C li ck s > 4 t h e n

D i a log. M e ss a g e (" H oo ray !" , " Y ou c li cke d " .. n C li ck s .. " t i me s . " ) ;

e nd


Double-click on the new button you added, then click on the Script tab and type the above script into the On Click event.


It should look like this when you're done:


image


The first line adds 1 to the current value contained in a variable named nClicks. (We'll add some script to the page's On Preload event in the next step, to set this variable to 0 when the page loads.)


The second line uses a Button.SetText action to change the button object's text to "Click count:

n", where n is the current value of nClicks.


The Button.SetText action takes two parameters. The first parameter is a string containing the name of the object that you want it to operate on. In this case, we used the special variable this, which contains the name of the object that the On Click event was triggered on. This is essentially the same as using the name of the object, like so:


Button.SetText("Button2", "Click count: " .. nClicks);


The Button.SetText action's second parameter is a string containing the text that you want to display in the button object. We used the concatenation operator to join the current value of nClicks to the end of the string "Click count: ". Note that this string has a space at the end of it, so there will be a space between the colon and the value of nClicks. (The resulting string just looks better that way.)


The concatenation operator consists of two periods, or dots (..). In fact, it's often referred to as the "dot-dot" operator. It is used to join two strings together to form a new, combined string. It's kind of like string glue.


image

Note: Technically, the value inside nClicks isn't a string-it's a number. That doesn't really matter, though. When you're doing concatenation, AutoPlay will automatically convert a number into the equivalent string, as required.


|