By the end of this reading you should be able to answer the following questions (all within the context of Revolution):
A boolean value is simply a value of either true or false. Sometimes boolean data are referred to as binary data, because they can only have one of two values. In Revolution you can create a boolean variable simply by putting a boolean value into it:
put true into answerIsCorrectBoolean Operators
It sometimes becomes necessary to compare values within Revolution. Boolean operators symbolize the relationship between values that can be evaluated to true or false. Transcript recognizes standard algebraic comparison symbols:
=,<>('is not equal to',)<,>,<=('less than or equal to',)>=('greater than or equal to'); as well as the key wordsis(same as '=') andis not(same as '<>') as boolean operators.Boolean Expressions
The word boolean is also used to describe an expression that results in either a true or false condition. You will use boolean expressions constantly in Revolution programming. They are the basis for making decisions in an if-then structure, for instance.
Here are some examples of boolean expressions:
the date = "10/13/06" myName is "John" --(i.e., is the contents of the variable exactly the same as the contents of the literal?) field "score" > 80 myScore <> yourScoreComparing Strings:The comparison of words or strings of letters is based on alphabetical order. Consequently, a word that comes before in alphabetical order is considered less than a word that comes after:
- "aaa" is less than "bbb"
- case is ignored: "AAA" is equal to "aaa"
- accents are not ignored: "aaa" is not equal to "äãâ"
- numbers are less than letters: "a1" is less than "aa"
Complex Expressions: The logical operators and, or, and not can be used to build complex logical expressions. Use parentheses to nest or group expressions as necessary. For example:
x = 20 and y < 45 or z >= 90In this example, because the statement is processed from left to right, the entire statement would resolve to true if the first two expressions were both true or if the third expression were true. If the third expression were false and one of the first two were false, then the whole statement above would resolve to false. Now consider this statement:
x = 20 and (y < 45 or z >= 90)Since the parentheses force the second group of boolean expressions to be evaluated first, the entire statement would only be true if the first expression were true and at least one of the other expressions were true.
If Statement
An if-statement is a conditional control structure that allows for checking a certain condition before taking an action. Boolean Operators are an essential ingredient in these types of statements. Revolution has several forms of the if-statement.Basic Form
The most basic form of the if statement has a beginning, a middle, and an ending, much like a handler:
if condition then statement end ifwhere
conditionis an expression that resolves to true or false.
statementis any valid Transcript command or statement.In a handler an if-then statement would look something like this:
on mouseUp put field "answer 1" into x if x = 3 then put "Your answer is correct!" into field "response" end if end mouseUpThis handler takes the content of a field and puts it into a variable. It then checks to see if the variable's content is equal to 3. If this is true, then an appropriate response is put into another field. If the variable does not contain 3, then the statement is false and the transcript statement within the
if...end ifstructure is not executed.If/Then/Else Form
Often you will want to perform one action if a condition is true and another action is it is false. For this you use the if-then-else form, which simply adds another clause, or section, to the structure:
if condition then statement else statement end ifwhere
conditionis an expression that resolves to true or false.
statementis any valid Revolution command or statement.Here is an example of an if-then-else structure in a handler:
on mouseUp put field "answer 1" into x if x = 3 then put "Your answer is correct!" into field "response" else put "Your answer is incorrect! Try again." into field "response" end if end mouseUpExpanding the example used above, the
elseportion of this statement provides for everything else the variable may contain besides 3. Consequently, for everything but 3 it will indicate that the response is incorrect.Even though it may seem like extra typing, beginning programmers should get into the habit of using this "complete" form, because it keeps the statement very clean and unambiguous. As you will see later, there are other forms that Revolution allows. For now, however, just stick to the basic format shown here.
Multiple Statement in if-then Structures
The first two examples of the if-statement only showed one statement in each clause. However, you may include as many statements as you need in both theifand theelseclauses:if condition then statement statement ... else statement statement ... end ifwhere
condition is an expression that resolves to true or false.
statement is any single Transcript statement.In a handler, this form of the if/then/else-statement would look something like this:
on mouseUp put field "answer 1" into x if x = 3 then show image "smileyFace" put "Your answer is correct!" into field "response" wait 2 seconds hide image "smileyFace" put empty into field "response" else beep put "Your answer is incorrect! Try again." into field "response" wait 2 seconds put empty into field "response" end if end mouseUpBuilding upon the example used previously, this handler now does more depending upon the contents of the variable x. If x contains 3 then an image is shown and text is put into a field. The computer waits a few seconds, then hides the image and erases the text. If x contains anything other than 3, then the computer plays the alert sound and gives an appropriate textual response, erasing the text after a few seconds.
The results of a conditional statement or logical expression can be placed into a variable. This consequently makes the variable contain either true or false (hence, becoming a boolean), enabling the variable to be checked much like a conditional statement. This is desirable in many cases where you would want to confirm a certain condition as being met without evaluating a conditional statement each time. All that needs to be done would then be to check if the variable itself contained either true or false.
To see this in action, imagine that you have some type of exam that contains a listening component. The instructor wishes the students to begin with the listening section before being able to view the pertinent questions. Once they leave the card that gives them access to the sounds, they should not be able to return to listen to it again. This could be accomplished in this manner:
on mouseUp global heardSound if heardSound then beep put "You've already listened to the sounds." into field "response" wait 2 seconds put empty into field "response" go card "Questions 1-12" else go card "Listening Comprehension" end if end mouseUpPreviously when the stack was opened, the global variable had been given a value with a statement like this: put false into heardSound (this variable would then change depending upon the actions of the user). This handler then accesses the global variable and checks its value. Note how this is done (as opposed to if heardSound = true). Appropriate action then takes place depending upon the value within the variable. As stated before, it is often more appropriate (and legible) to use a boolean variable in this manner rather than employing a conditional statement.
Again, the if-statement is a powerful tool for getting the stack to execute functions on a conditional basis, i.e., when certain conditions are met.
Other Acceptable Forms
Revolution tries to provide more flexibility to the programmer as compared to some other languages. Accordingly, there are "simpler", single line forms for the if/then statement. However, these forms can often become confusing, especially when embedding if-then structures within other if-then structures. For the beginning programmer we recommend sticking to the "complete" format shown above, at least until you become very familiar with how if-then structures work. Because you may encounter these single-line forms, they are included for your information.
Single statement form
This form of the if-statement may only be used to execute a single command when a condition is true. Its syntax is:
if condition then statement
where
condition is an expression that resolves to true or false.
statement is any single Transcript statement.Practically speaking, the single-line if-then statement would look something like this within a handler:
on mouseUp put field "answer 1" into x if x = 3 then put "Your answer is correct!" into field "response" end mouseUpIf/Then/Else Single-statement Form
This expanded form of the single-line if-statement provides an alternate else-clause for a statement to be executed when the condition is false. The syntax is similar:if condition then statement
else statementwhere
condition is an expression that resolves to true or false.
statement is any single Transcript statement.In a handler, the single-line if/then/else-statement would look something like this:
on mouseUp put field "answer 1" into x if x = 3 then put "Your answer is correct!" into field "response" else put "Your answer is incorrect! Try again." into field "response" end mouseUp
The Repeat statement is Transcript's loop structure that allows a series of commands to be repeated under various conditions. When used wisely and judiciously, a repeat loop can be an incredible time-saving control structure as it allows for the quick execution of repetitive tasks.
The general form of the Repeat statement consists of the key word repeat at the beginning and a matching end repeat at the end, with any number of Transcript statements between.
repeat loop form statement ... end repeat
where
loop form is an expression that determines the type of loop structure.
statement is any single Transcript statement.
There are four basic variations of the loop form:
on mouseUp
repeat for 4 times
show image "Wow!"
wait 10
hide image "Wow!"
wait 10
end repeat
end mouseUp
Within this handler, this repeat structure would show and hide the image specified (with pauses between each) four times, creating a blinking image. The repeat structure will execute its statements exactly the number of times specified.
on mouseUp
repeat with count = 1 to 10
put count into field "timer"
wait 1 second
end repeat
beep
end mouseUp
This handler utilizes the nature of the repeat loop form to change the content of a field. The repeat control structure creates a variable called "count" and initializes it to 1. The first time through the loop it puts the contents of the variable (the value 1) into a field and waits one second. The repeat structure then increments the contents of count by 1. This value (now 2) is then displayed in the field, and so on up until count contains 10, which is the last value displayed in the field. The repeat loop then finishes and the computer plays an alert sound. This particular handler creates a type of ten-second timer that displays the amount of time passed and alerts when ten seconds have passed.
on mouseUp
put 1 into count
repeat while count <= 10
put count into field "timer"
wait 1 second
add 1 to count
end repeat
beep
end mouseUp
This handler accomplishes the same end as the example given above. However, in this case it is necessary to manually initialize the variable count outside the loop and manually increment it inside the loop for it to function as we would like.
Note: If "<" were used instead of "<=" within the loop form, then the loop would execute only 9 times instead of 10.
Here is an example of the repeat until-statement:
on mouseUp
put 1 into count
repeat until count > 10
put count into field "timer"
wait 1 second
add 1 to count
end repeat
beep
end mouseUp
Again, this accomplishes the same end as the previous examples. It is still necessary to manually initialize and increment the variable.
Note: If ">=" were used instead of ">" within the loop form, then the loop would execute only 9 times instead of 10.
This is similar to the Repeat With structure, except that instead of incrementing a variable, this repeat structure allows one to step through the various elements within a container (we will discuss later the exact nature of these elements called "chunks"). This structure is useful if you need to manipulate or perform an action on each element within a container, as it is much faster and easier to work with than the repeat with structure. For example:
on mouseUp
repeat for each word thisOne in field "fullText"
put thisOne & return after field "wordList"
end repeat
end mouseUp
This handler allows us to take the text of a given field, step through it one word at a time, and then place each word on a separate line in another field. While the practical uses of this structure may not be immediately evident, its usefulness cannot be underestimated.