As we know in Python, if the whole traverse is successfully finished, then the else block will run. It's different from the current Else syntax in AHKV2, but it is extremely useful in many situations.
Typically, this feature must be used to pop from the stack after the loop is normally finished each time, if you want to translate a recursion into loop with a stack.
[Code1:]
Code: Select all
Iter(eles*){
en:=eles.__Enum(1)
stack:=[en]
stack.capacity:=eles.capacity**2
arr:=[]
while stack.Length{
for x in en{
switch x.HasProp('__Enum') {
case 0:
arr.Push(x)
case 1:
stack.Push(en),en:=x.__Enum(1)
; break
goto breaked
}
}
en:=stack.Pop()
breaked:
;here you can doing somethings else, like out of the else block.
}
return arr
}
c:=Iter(1,2,[3,4,[5,6,[7,8,9],10],11,[12,13]],14,15)
for x in c{
OutputDebug(x)
}
[Code2:]
Code: Select all
Iter(eles*){
en:=eles.__Enum(1)
stack:=[en]
stack.capacity:=eles.capacity**2
arr:=[]
while stack.Length{
for x in en{
switch x.HasProp('__Enum') {
case 0:
arr.Push(x)
case 1:
stack.Push(en),en:=x.__Enum(1)
break
}
}else{
en:=stack.Pop()
}
;here you can doing somethings else, like out of the else block.
}
return arr
}
c:=Iter(1,2,[3,4,[5,6,[7,8,9],10],11,[12,13]],14,15)
for x in c{
OutputDebug(x)
}
[Code3:]
Code: Select all
arr:=['John','Smith','Micheara']
for x in arr{
if x=='David'{
MsgBox('I found David!')
}
}else{
MsgBox('I can not found David!')
}
[Code4:]
Code: Select all
arr:=[]
for x in arr.__Enum(1){
OutputDebug(x)
}else{
MsgBox('There is no break or countinue(2) encountered.')
if A_Index==0{
MsgBox('The loop had zero iterations.')
}
}
Code: Select all
arr:=[]
for x in arr.__Enum(1){
OutputDebug(x)
}else if !A_Index{
MsgBox('The loop had zero iterations.')
}
Therefore, I wish this feature could replace the current Else as soon as possible.
If you are concerning about the backward compatibility, it is absolutely worth considering introducing another keyword for this feature.