Page 1 of 1

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

Posted: 18 Dec 2018, 15:20
by oakkitten
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! 🧠🤓

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

Posted: 18 Dec 2018, 15:36
by wolf_II
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:

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

Posted: 18 Dec 2018, 15:38
by gregster
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
}

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

Posted: 18 Dec 2018, 15:38
by Helgef
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.

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

Posted: 18 Dec 2018, 16:32
by oakkitten
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?)

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

Posted: 18 Dec 2018, 20:06
by gregster
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 %

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

Posted: 20 Dec 2018, 15:02
by oakkitten
ayy it's all clear! thanks a bunch mr black faced sheep!

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

Posted: 24 Dec 2018, 07:03
by Arkan
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)