Steven P 发表于 2022-7-5 16:23:30

While循环

早上好
 
 
如果达到条件,是否有类似于while循环的东西会中途退出循环?
 
 
例如

(setq test 0)
(while (<= test 10)
   (setq test (1+ test))
   (princ test)
   (setq test (1+ test))
   (princ test)
   (setq test (1+ test))
   (princ test)
)

Tharwat 发表于 2022-7-5 16:34:14

(repeat 10
         (......)
         )

 
发生的情况是,每次检查都要将值增加3次,我认为每次检查都必须增加一次值:
 

(while <test statement> ; if true, do the expressions below, if false - don't evaluate the expressions/stop the loop
<expressions> ; evaluate the expressions, and run the <test statement> again
)
 
或者检查每个子增量,这令人沮丧:
 

(setq n 0)
(while (< n 10)
(setq n (1+ n))
(print n)
)

Grrr 发表于 2022-7-5 16:46:42

谢谢李,像往常一样,这是一个很好的解决方案,我从来都不知道你可以像那样在while命令中添加“and”

Steven P 发表于 2022-7-5 16:56:47

 
欢迎您,Steven-任何表达式都可以在一段时间内构成测试表达式。

Lee Mac 发表于 2022-7-5 17:08:32

Steven P 发表于 2022-7-5 17:21:04

Thanks Lee, as always a great solution, I never knew you could add 'and' to a while command like that

Lee Mac 发表于 2022-7-5 17:29:08

 
You're welcome Steven - any expression can constitute the test expression for a while loop.
页: [1]
查看完整版本: While循环