Script [13.3.1]:
set condition to false
repeat while condition is false
-- commands
-- make condition true to stop repeat
end repeat
Explanation: What if I have a condition which is either true or false? And I want the repeat section run as long as that condition holds true or false.
Then you can do that by using a variable. I have used variable condition and set it to false. Then I ask repeat block to execute till the condition is false.
In such repeat blocks, it is important to make change the condition, to stop the execution of repeat block.
Script [13.3.2]:
set condition to true
repeat until condition is false
-- commands
-- make condition false to stop repeat
end repeat
Explanation: This is another method of using condition based repeat blocks. The only difference is that, the repeat block will execute as long as a particular condition is not met. In the above example I have set condition to true. So the repeat block will execute as long as condition is not false.
e.g. Script [13.3.3]:
set condition to false
repeat until condition is true
set temp to display dialog "Enter age" default answer ""
set x to text returned of temp
try
set x to x as integer
on error
display dialog "Please enter a number"
end try
if class of x is integer then
set condition to true
if x is greater than or equal to 18 then
say "Eligible to vote"
else
say "Not eligible to vote"
end if
end if
end repeat
Explanation: I have used until condition based loop. In the above example I have set condition variable as false. So as long condition is false, the repeat block will get executed.
I use variable x to take user input for age. Then I perform coercion. If coercion fails then the on error block will get invoked where a dialog will popup asking user to enter a number.
If coercion succeeds, then class of x will be checked. If class in integer, then condition will be set to true.
Next I checked whether x>=18. If it is >=18 then he will be eligible to vote, else he won’t be eligible to vote.
Figure 13.3.3 Successful Run
Figure 13.3.3-2 On Failurer, User Has to Enter Age Again