Loop Value Calculation Help

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
TheDewd
Posts: 1513
Joined: 19 Dec 2013, 11:16
Location: USA

Loop Value Calculation Help

03 Apr 2024, 10:20

I'm trying to define pixel coordinates on the x-axis, starting with 0, and advancing by 32 pixels for each iteration of the loop.

I do the calculation of 32 * A_Index, however I want the initial value to begin at 0.

To achieve this, I'm subtracting 32 each time.

Is there a better way to do this, instead of having to always subtract 32?

Below is an example loop code, and the expected results.

Code: Select all

Loop 20 {
	MsgBox (32 * A_Index) - 32
}

Code: Select all

1/20 = 0
2/20 = 32
3/20 = 64
4/20 = 96
5/20 = 128
6/20 = 160
7/20 = 192
8/20 = 224
9/20 = 256
10/20 = 288
11/20 = 320
12/20 = 352
13/20 = 384
14/20 = 416
15/20 = 448
16/20 = 480
17/20 = 512
18/20 = 544
19/20 = 576
20/20 = 608
User avatar
mikeyww
Posts: 27271
Joined: 09 Sep 2014, 18:38

Re: Loop Value Calculation Help

03 Apr 2024, 10:43

There are other ways, like subtracting one from your index, but you will need to judge them and decide which is the best!
Rohwedder
Posts: 7742
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Loop Value Calculation Help

04 Apr 2024, 03:12

Hallo,
addition is easier than multiplication:

Code: Select all

Coord := -32
Loop 20
	MsgBox Coord += 32

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: No registered users and 46 guests