QUIZ TIME! Сan you guess what this script will show? Topic is solved

Discuss Autohotkey related topics here. Not a place to share code.
Forum rules
Discuss Autohotkey related topics here. Not a place to share code.
oakkitten
Posts: 3
Joined: 18 Dec 2018, 14:42

QUIZ TIME! Сan you guess what this script will show?

18 Dec 2018, 15:20

Let's play a game! 🎲🎲🎲 Guess what the following script will show to you:

Code: Select all

one() {
	if (0) return
	if (0) return
	msgbox one
}

two() {
	if (0) return
	if (0) 
		return
	msgbox two
}

three() {
	if (0) return
	if (0)
		return
	if (0) return
	msgbox three
}

four() {
	if (0) return
	if (0) 
		return
	if (0) return
	winget foo, id, a
	msgbox four
}

one()
two()
three()
four()
Did you guess right❓ If you did, you get to explain to me what the heck is going on here! 🧠🤓
wolf_II
Posts: 2685
Joined: 08 Feb 2015, 20:55

Re: QUIZ TIME! Сan you guess what this script will show?

18 Dec 2018, 15:36

Try this: ( i removed stuff that is ignored, then fixed indentation to make it readable )

Code: Select all

one() {
    if (0)
        if (0)
            msgbox one
}

two() {
    if (0) return
        if (0)
            return

    msgbox two
}

three() {
    if (0)
        if (0)
            return

    if (0)
        msgbox three
}

four() {
    if (0)
        if (0)
            return

    if (0)
        winget foo, id, a

    msgbox four
}

one()
two()
three()
four()
I first guessed, this was for v2 and was surprised to see it works with v1
Hi, Helgef :wave:
Last edited by wolf_II on 18 Dec 2018, 15:39, edited 1 time in total.
gregster
Posts: 2799
Joined: 30 Sep 2013, 06:48

Re: QUIZ TIME! Сan you guess what this script will show?

18 Dec 2018, 15:38

Everything as expected...
if (0) return and if (0) both are always false. You cannot use return in the same line here - it's a blank variable here.
Your indentation does not always reflect what is really going on here.

Code: Select all

one() {
	if (0) return		; false	
		if (0) return	; false
			msgbox one
}
Last edited by gregster on 18 Dec 2018, 15:38, edited 1 time in total.
Helgef
Posts: 3891
Joined: 17 Jul 2016, 01:02
Contact:

Re: QUIZ TIME! Сan you guess what this script will show?  Topic is solved

18 Dec 2018, 15:38

Notice that all return on the same line as an if are simply blank variables, so all if (expression) are false. Then it is easy to see the outcome.
You can read it like this,

Code: Select all

one() {
	if ((0) return) {
		if ((0) return)
			msgbox one
	}
}
Cheers.
oakkitten
Posts: 3
Joined: 18 Dec 2018, 14:42

Re: QUIZ TIME! Сan you guess what this script will show?

18 Dec 2018, 16:32

Thanks for participation everyone! Y'all win, but I've marked Helgef's answer as the best one as it also explains the behavior of stuff like:

Code: Select all

if (0) foo 00 bar
     msgbox, this won't show

if (0) foo bar baz 1
    msgbox, this will
now the only remaining question is, why "(0) foo bar baz 1" evaluates to "01" (and what is the type of this?)
gregster
Posts: 2799
Joined: 30 Sep 2013, 06:48

Re: QUIZ TIME! Сan you guess what this script will show?

18 Dec 2018, 20:06

oakkitten wrote:
18 Dec 2018, 16:32
now the only remaining question is, why "(0) foo bar baz 1" evaluates to "01" (and what is the type of this?)
(0) evaluates to 0 which would mean false in a boolean sense, but it gets concatenated with the blank variables foo, bar, and baz (the result is still 0 and hence, false, while 000 is also seen as 0)
It also gets concatenated with the number 1 (which itself means true, because everything else than 0 and blank means true - that's why the resulting string 01 also means true)

Compare https://autohotkey.com/docs/Variables.htm#Expressions :
Boolean values: When an expression is required to evaluate to true or false (such as an IF-statement), a blank or zero result is considered false and all other results are considered true. For example, the statement if ItemCount would be false only if ItemCount is blank or 0. Similarly, the expression if not ItemCount would yield the opposite result.
Also

Code: Select all

if (0) foo bar baz "a"					; --> 0a
    msgbox, % "this will show: " (0) foo bar baz "a"	; force expression mode with %
oakkitten
Posts: 3
Joined: 18 Dec 2018, 14:42

Re: QUIZ TIME! Сan you guess what this script will show?

20 Dec 2018, 15:02

ayy it's all clear! thanks a bunch mr black faced sheep!
User avatar
Arkan
Posts: 22
Joined: 27 Nov 2018, 03:12

Re: QUIZ TIME! Сan you guess what this script will show?

24 Dec 2018, 07:03

oakkitten wrote:
20 Dec 2018, 15:02
ayy it's all clear! thanks a bunch mr black faced sheep!
There's only one mr. Sheep and a bunch of mister HotASheep)

Return to “General Discussion”

Who is online

Users browsing this forum: No registered users and 1 guest