data WORK.PRODUCTS;
Prod=1;
do while(Prod LE 6);
Prod + 1;
end;
run;
What is the value of the variable Prod in the output data set?
A. 6
B. 7
C. 8
D. . (missing numeric)
Answer: B
本文属于博客:http://crackman.net/ 版权归作者所有,欢迎转载!如有转载,请务必注明出处!未经本文作者同意
不得用于商业应用。
这道题考察的是DO WHILE语句中循环执行条件的判断时点。
到底是先判断再执行还是先执行在判断?
DO WHILE是先判断然后执行,也就是说PROD=1,先判断 PROD LE 6是否为TRUE,然后决定是否执行循环语句。
循环每次执行完,PROD自动加1,也就是当PROD LE 6的为TRUE时,PROD最大值就是6,超过6就不执行循环内的语句,就是PROD加1了,
但是PROD等于6的时候,依然要执行循环,LE就是小与等于的意思。所以最终PROD=7。
如果改成:
data WORK.PRODUCTS;
Prod=1;
do until(Prod LE 6);
Prod + 1;
end;
run;
那么PROD=2,为什么等于2?
因为DO UNTIL是执行后再判断,PROD执行一次之后变成2,2 LE 6是true,所以终止执行!。UNTIL里面的表达式是循环终止的判断条件
,如果UNTIL里面为真,那么就终止执行DO LOOP;而WHILE的表达式是循环继续的判断条件,为真继续执行DO LOOP。
英文解释:
The DO UNTIL statement executes statements in a DO loop repetitively until a condition is true, checking the
condition after each iteration of the DO loop. The DO WHILE statement evaluates the condition at the top of the loop;
the DO UNTIL statement evaluates the condition at the bottom of the loop.