[英]How to “continue” in a loop in smalltalk
I have following code where I want to 'continue' if i is less than 5: 我有以下代码,如果我小于5,我想“继续”:
1 to: 10 do: [ :i |
i < 5 ifTrue: [ continue ].
'Square of i = ', (i * i) printNl.
]
'continue' in above code is obviously not working. 上面代码中的“继续”显然不起作用。 I know that exit
can be used to break out of a loop. 我知道exit
可以用来打破循环。 But how to continue
? 但是如何continue
? Thanks for your help. 谢谢你的帮助。
In your case you can simply use ifFalse:
: 在您的情况下,您可以简单地使用ifFalse:
::
1 to: 10 do: [ :i |
i < 5 ifTrue: [
"Any code you need"
] ifFalse: [ 'Square of i = ', (i * i) printNl ].
]
The following code will probably work only in Pharo. 以下代码可能仅在Pharo中有效。 (it will not work in GNU Smalltalk, in Smalltalk/X it could work if you use correct modulo. The %
returns complex number): (它在GNU Smalltalk中不起作用,在Smalltalk / X中,如果使用正确的模,它可能会起作用。 %
返回复数):
1 to: 10 do: [ :i |
[ :continue |
i % 5 = 0 ifTrue: [
Transcript show: i; cr.
continue value ].
Transcript
show: i;
show: ', '.
] valueWithExit.
]
The valueWithExit
the implementation in Pharo: valueWithExit
在Pharo中的实现:
valueWithExit
self value: [ ^nil ]
The meaning: 含义:
The receiver must be block of one argument. 接收者必须是一个参数的块。 When it is evaluated and is passed a block, when a value
message is send will exit the receiver block (returning nil
in Pharo). 当对其求值并通过一个块时,发送value
消息时将退出接收器块(在Pharo中返回nil
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.