• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Things I thought I knew, but didn't - variable scope in repeat loops
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Things I thought I knew, but didn't - variable scope in repeat loops


  • Subject: Re: Things I thought I knew, but didn't - variable scope in repeat loops
  • From: "Mark J. Reed" <email@hidden>
  • Date: Fri, 26 Dec 2008 19:10:14 -0500

I think the OP might have been confused by the fact that the loop
control variable is stateless in Applescript.  That is, when you do
this:

repeat with i from 1 to 10

Applescript builds a list of values (or an iterator/generator) and
sets i to the next value at the top of the loop; it doesn't look at
the current value of i to determine what the next value of i should
be.   So you can do anything you want to i inside the loop and it
won't affect the loop control logic:

repeat with i from 1 to 10
   set i to "hi, mom!"
   say i
end repeat

Some languages, on the other hand, implement counting loops by
incrementing the value of the counter (or, as in the case of C, leave
it up to the programmer, who usually chooses to use an increment
step).  In such a language, this set of nested loops would only
execute the outer loop once:

for i=1 to 3
    for i=1 to 3
        print i
    rof
rof

Other languages, like Pascal, treat loop control variables specially
and won't even compile an attempt to use the same variable; BASIC uses
the variable to pair the top and bottom of the loop, so hitting the
bottom of the outermost loop will generate a runtime error.
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Follow-Ups:
    • Re: Things I thought I knew, but didn't - variable scope in repeat loops
      • From: Richard Rönnbäck <email@hidden>
    • Re: Things I thought I knew, but didn't - variable scope in repeat loops
      • From: Ed Stockly <email@hidden>
References: 
 >Re: Things I thought I knew, but didn't - variable scope in repeat loops (From: has <email@hidden>)

  • Prev by Date: Re: Things I thought I knew, but didn't - variable scope in repeat loops
  • Next by Date: Re: Things I thought I knew, but didn't - variable scope in repeat loops
  • Previous by thread: Re: Things I thought I knew, but didn't - variable scope in repeat loops
  • Next by thread: Re: Things I thought I knew, but didn't - variable scope in repeat loops
  • Index(es):
    • Date
    • Thread