[Solved]Need some help with a simple loop

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
sanyang
Posts: 2
Joined: 15 Jan 2018, 16:24

[Solved]Need some help with a simple loop

15 Jan 2018, 16:45

Greetings,
I'm trying to do a simple loop which gives "op" as 1 2 3 1 2 3 1 2 3 ... when I press G and displays it in a window. So this is my solution:

Code: Select all

temp = 1
op:= temp-Floor(temp/3)*3
Gui, Add, Text, Center , Op = %op% Temp = %temp%
Gui,Show,AutoSize Center,AlwaysOnTop Window

g::
temp:=temp+1
op:= temp-Floor(temp/3)*3
if op = 0
{
op:=3
}
Gui, Destroy
Gui, Add, Text, Center, Op = %op% Temp = %temp%
Gui,Show,AutoSize Center,AlwaysOnTop Window
return
I need some advice to improve my code: Is there any easier way to do this loop? And how can I change the number in the window without closing and opening new one? I'm very new to coding and AHK so I want to learn from experienced members of AHK community. Thank you in advance. :wave: :D
Last edited by sanyang on 16 Jan 2018, 06:56, edited 1 time in total.
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: Need some help with a simple loop

15 Jan 2018, 17:01

Welcome

GuiControl will update the number for you; introduce gui variables: Gui, Events

However, is there a reason for your maths? Shouldn't you use op:=op+1 to get op if it is cyclic as 1, 2, 3, 1, 2, 3, 1, 2, 3 ... ? Also, that can be shorted as just op++ where the ++ operator means add one to the variable.

Also, you mention it as a loop, but there's nothing particularly looping unless you keep pressing g. You may want the Loop command or SetTimer command. Either way, I'd recommend adding in a hotkey like ^Esc::ExitApp to close the script in case things get out of hand on accident - useful when working with potentially never-ending commands like Loop and SetTimer.
sanyang
Posts: 2
Joined: 15 Jan 2018, 16:24

Re: Need some help with a simple loop

15 Jan 2018, 17:40

Thank you for your advice. With that I've made some changes:

Code: Select all

op = 1
Gui, Add, Text, Center , Op = %op%
Gui,Show,AutoSize Center,AlwaysOnTop Window

g::
op++
if op > 3
{
op = 1
}
GuiControl,,Static1,Op = %op%
return

Esc::
ExitApp
That was silly to use divide 3 for this. There was no reason at all :lol: Thanks again and have a nice day.
teadrinker
Posts: 4325
Joined: 29 Mar 2015, 09:41
Contact:

Re: Need some help with a simple loop

15 Jan 2018, 17:52

You could use this way for such purposes:

Code: Select all

op = 1
Gui, Add, Text, Center w100, Op = 1
Gui, Show
Return

g:: GuiControl,,Static1, % "Op = " . mod(op++, 3) + 1

Esc::
ExitApp

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: mikeyww, OrangeCat, ShatterCoder and 86 guests