question about for-loop Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
joefiesta
Posts: 497
Joined: 24 Jan 2016, 13:54
Location: Pa., USA

question about for-loop  Topic is solved

16 Apr 2021, 09:19

The doc for for-loop includes the following
colours := Object("red", 0xFF0000, "blue", 0x0000FF, "green", 0x00FF00)
; The above expression could be used directly in place of "colours" below:
for k, v in colours
s .= k "=" v "`n"
MsgBox % s


I added msgbox, as follows:
colours := Object("red", 0xFF0000, "blue", 0x0000FF, "green", 0x00FF00)
; The above expression could be used directly in place of "colours" below:
for k, v in colours
{
s .= k "=" v "`n"
msgbox %s%
}
MsgBox % s

The object, colours, has 3 keys and 3 values. Why is the first msgbox displayed only once, and why does S at the end not include "red=" and "green=" values?
gregster
Posts: 9014
Joined: 30 Sep 2013, 06:48

Re: question about for-loop

16 Apr 2021, 09:27

Can't reproduce your findings on v1.1.33.06.

I get 4 msgboxes in total (3 from the loop) - the last one (with the same contents like the third one from the loop) also includes "green=" and "red=", and their values.
joefiesta
Posts: 497
Joined: 24 Jan 2016, 13:54
Location: Pa., USA

Re: question about for-loop

16 Apr 2021, 09:38

Sorry, gregster.

My bad.

I was looking at https://www.autohotkey.com/boards/viewtopic.php?f=14&t=89397
trying to figure it out and inadvertantly left the "UNTIL CLAUSE" in my test, as follows

colours := Object("red", 0xFF0000, "blue", 0x0000FF, "green", 0x00FF00)
; The above expression could be used directly in place of "colours" below:
for k, v in colours
{
s .= k "=" v "`n"
msgbox %s%
}
until true
MsgBox %s


I think the real problem--which I will relate elsewhere--is the doc for For-loop. It mentions you can have a block. But, you look up BLOCK and it, as well as FOR-LOOP, says NOTHING about including a until or while. Thus, I thought the UNTIL clause was INVALID, and that was what I was really testing.
gregster
Posts: 9014
Joined: 30 Sep 2013, 06:48

Re: question about for-loop

16 Apr 2021, 09:39

Jftr, that linked bug report relates to the latest v2 version... a131
User avatar
boiler
Posts: 16955
Joined: 21 Dec 2014, 02:44

Re: question about for-loop

16 Apr 2021, 09:59

joefiesta wrote: But, you look up BLOCK and it, as well as FOR-LOOP, says NOTHING about including a until or while.
That's because there is typically no reason to include until or while in a for loop. You would only add an until if you wanted to potentially stop looping before you've cycled through all the elements of the object. Is that what you were intending to do? That is different than the point of the bug report.
joefiesta
Posts: 497
Joined: 24 Jan 2016, 13:54
Location: Pa., USA

Re: question about for-loop

16 Apr 2021, 12:27

I'm not trying to do anything. I'm simply mentioning that the documentation is lacking. Just because you normally would not want to do something doesn't mean it should not be documented. And, yes, I realize I have to enter a doc report issue or HOPE someone who reads the problem issue referenced realizes the doc issue (which really won't happen).
User avatar
boiler
Posts: 16955
Joined: 21 Dec 2014, 02:44

Re: question about for-loop

16 Apr 2021, 12:55

It says that they are used in loop statements. While is a type of loop statement. Until is used with loop statements. They don’t exist in any other context. In fact, if you click on the loop statements link in the documentation for Block that you referenced, it describes the various types of loops including While and describes that Until is used to terminate a loop.

If each section of the documentation had to list the various forms of loop statements and related terms whenever they were referenced (and did the same for other aspects of the language), it would become unwieldy and make it hard to follow. The fact that a link is provided when it mentions them is more than adequate in my opinion.
joefiesta
Posts: 497
Joined: 24 Jan 2016, 13:54
Location: Pa., USA

Re: question about for-loop

16 Apr 2021, 13:27

BLOCK documentation does NOT describe a WHILE or UNTIL parameter. It only references the WHILE and UNTIL commands.
User avatar
boiler
Posts: 16955
Joined: 21 Dec 2014, 02:44

Re: question about for-loop

16 Apr 2021, 13:36

What parameters are you talking about?
User avatar
boiler
Posts: 16955
Joined: 21 Dec 2014, 02:44

Re: question about for-loop

16 Apr 2021, 13:48

FYI - A block doesn't have parameters. It is used to indicate a group of lines of code that are linked to the various types of loops, if statements, etc. That includes While and the associated loop control Until (While and Until normally wouldn't be used together, though, as they largely do the same thing). As I said before:

While is a type of loop. It is only a command, it is not a parameter to a block or anything else. It is often used with blocks, not as a parameter to the block, but as a type of loop command.

Until puts a condition on a loop. It is not a command unto itself, and it is not a parameter to a block or anything else. It is often used with a block to indicate when the looping of the code within that block should terminate based on its condition.
joefiesta
Posts: 497
Joined: 24 Jan 2016, 13:54
Location: Pa., USA

Re: question about for-loop

16 Apr 2021, 14:01

i don't know why i bother arguing with you. But, WHILE can be a parameter, as in the FOR COMMAND shown below:

colours := Object("red", 0xFF0000, "blue", 0x0000FF, "green", 0x00FF00)
for k, v in colours
{
s .= k "=" v "`n"
msgbox %s%
} while true

MsgBox % s


Th command is FOR. that's it. WHILE is parameter in the above.
I don't care if it is useful, or if it makes no sense. IT IS VALID (and the WHILE actually does do something in this case).
User avatar
boiler
Posts: 16955
Joined: 21 Dec 2014, 02:44

Re: question about for-loop

16 Apr 2021, 14:04

It is not valid like that. You put while at the top of a loop. Until goes at the bottom. And it is not a parameter of for. You can use whatever terminology you want. It is not correct and no one here will agree with you on that.
User avatar
boiler
Posts: 16955
Joined: 21 Dec 2014, 02:44

Re: question about for-loop

16 Apr 2021, 14:09

Do you know what while true does in your code? It makes an infinite loop out of your last MsgBox command, because you made the condition always true. It has nothing to do with the for loop above it. You can run it since you don't believe what I say.
joefiesta
Posts: 497
Joined: 24 Jan 2016, 13:54
Location: Pa., USA

Re: question about for-loop

16 Apr 2021, 14:09

did you try it?

my bad. you are right....

makes no sense to me, though. why is the WHILE command consider a new command when it is on the same line as the block?
Last edited by joefiesta on 16 Apr 2021, 14:13, edited 1 time in total.
User avatar
boiler
Posts: 16955
Joined: 21 Dec 2014, 02:44

Re: question about for-loop

16 Apr 2021, 14:13

Yes, it runs the last line over and over because you made it subject to an infinite while loop. It has nothing to do with the for loop above it. The for loop finishes the three loops based on the for, and then your MsgBox loops forever. Check the ListLines if you don't understand what's happening and don't want to believe me.
User avatar
boiler
Posts: 16955
Joined: 21 Dec 2014, 02:44

Re: question about for-loop

16 Apr 2021, 14:21

joefiesta wrote:
16 Apr 2021, 14:09
my bad. you are right....

makes no sense to me, though. why is the WHILE command consider a new command when it is on the same line as the block?
No problem. It's not always easy to see some of these concepts at first.

The reason it's on the same line is because of the style called One True Brace or OTB. It is described in the Block documentation.
joefiesta
Posts: 497
Joined: 24 Jan 2016, 13:54
Location: Pa., USA

Re: question about for-loop

16 Apr 2021, 14:26

sorry I was so argumentative. And WRONG!

The fact--and, yes, it's documented--that another command (although the doc says STATEMENT, I guess there's a difference) can follow the closing brace is SO UNINTUITIVE. Why ever allow that? I guess it makes the parsing easier.
User avatar
boiler
Posts: 16955
Joined: 21 Dec 2014, 02:44

Re: question about for-loop

16 Apr 2021, 14:41

joefiesta wrote: sorry I was so argumentative. And WRONG!
No worries.
joefiesta wrote: The fact--and, yes, it's documented--that another command (although the doc says STATEMENT, I guess there's a difference) can follow the closing brace is SO UNINTUITIVE. Why ever allow that? I guess it makes the parsing easier.
When you say follow the closing brace, do you mean until in particular? Or perhaps else? The way to look at it when the block doesn't share lines with anything else is to consider the replacement for a single command. For example, here is an if/else without blocks:

Code: Select all

if (x = 3)
	MsgBox, X has the value of 3
else
	MsgBox, x does not equal 3

You can replace the single MsgBox statements/commands with code blocks:

Code: Select all

if (x = 3)
{
	MsgBox, X has the value of 3
	return
}
else
{
	MsgBox, x does not equal 3
	ExitApp
}

Then you can employ OTB if you want to make it more compact, but still is the same logically as above:

Code: Select all

if (x = 3) {
	MsgBox, X has the value of 3
	return
} else {
	MsgBox, x does not equal 3
	ExitApp
}

Same for while:

Code: Select all

x := 1
while x < 3
	MsgBox, % x++
Or with a block:

Code: Select all

x := 1
while x < 3
{
	x++
	MsgBox, % x
}
Or:

Code: Select all

x := 1
while x < 3 {
	x++
	MsgBox, % x
}
User avatar
boiler
Posts: 16955
Joined: 21 Dec 2014, 02:44

Re: question about for-loop

16 Apr 2021, 15:06

Oh, one last thing. I guess you were asking why the while worked when it was on the end of the closing brace of a block like this: } while true. That does look strange and is just how block markers are allowed to share lines with other code not even related to loops and such. It's probably necessary to allow OTB to work. As the documentation says:
Block documentation wrote:Although blocks can be used anywhere, currently they are only meaningful when used with...
As evidence they can be used anywhere even when they're not meaningful, the following works:

Code: Select all

{ } MsgBox, hello
joefiesta
Posts: 497
Joined: 24 Jan 2016, 13:54
Location: Pa., USA

Re: question about for-loop

16 Apr 2021, 15:12

thanks. that last example is a real doozy. I love AHK, but I hate the command structure. It seems like it an amalgam of styles from 2 or 3 languages. Probably most notably C++, which I never learned. Me, I'm an old mainframer who used IBM stuff which was documented EVER so precisely--probably way before you were born!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Dewi Morgan and 393 guests