The following SAS program is submitted:
data work.clients;
calls = 6;
do while (calls le 6);
calls + 1;
end;
run;
Which one of the following is the value of the variable CALLS in the output data set?
A. 4
B. 5
C. 6
D. 7
Click Comment link to get answer
D
ReplyDeleteD
ReplyDeleteHi....Why is it 7 ?? calls i not less than 6 ??
ReplyDeleteDO WHILE (evaluates at the top of the loop) Now, calls=6 is equal to 6 therefore calls le 6 is TRUE. DO LOOP is executed. So, calls = calls +1 = 7
DeleteNow, calls=7 is greater than 6 therefore calls le 6 is FALSE. DO LOOP is not executed.
Hence, the value of calls remains 7
le means "less than or equal to"
Deleteplz write the comments
ReplyDeletethe answer is D, because 'le' means "less than or equal".
ReplyDeleteCalls=6
ReplyDeleteSo the program will execute once, and add 1 to calss to get to 7, because the condition says: While calls LE 6.
Calls = 7
ReplyDeleteanswer = D
A while condition isn't checked until the end of the loop, so the first time through, +1 is added to calls, resulting in 6- then the condition is checked and it stops.
ReplyDeleteAnswer is D
you are confused.
Deletedo until is not evaluated until the bottom of the loop
and
do while is evaluated at the top of the loop
Sakar Sham;
ReplyDeleteSo in "while" condition it checks up to 6 then it stops. We would not forget it. Thank you.
How about if replaced by "until"? It must be in the question somewhere?
Answer is D. 7
ReplyDeletehere, note that the operator is LE (less than or equal to). So the condition is true the first time.
Calls = 6 and it becomes Calls + 1 => 6+1 = 7.
The loop stops executing as the value of calls becomes greater than 6. Thus, output is 7.
Please note the difference between do while and do until:
ReplyDeleteDO WHILE
The while test is evaluated at the top of the loop.
DO UNTIL
The until test is evaluated at the bottom of the loop.
Reference: http://www2.sas.com/proceedings/forum2007/067-2007.pdf
The change should be change in the following manner to obtain the same results using a do until loop:
data clients;
calls = 6;
do until (calls GE 6);
calls + 1;
end;
run;
Not that the difference here is DO WHILE uses Calls LE 6 and DO UNTIL uses Calls GE 6.
The example provided in the reference helps understand this better.
D
ReplyDeleteD, 7
ReplyDeleteHello!! I'am glad to read the whole content of this blog and am very excited.Thank you.
ReplyDeleteตารางคะแนน
do while loop is looking for where to stop which is when it counters false and that is when calls +1 < 6 (simple math) and that would be when calls =7 and that should be the desired output(checking when to stop).
ReplyDelete*encounters
Delete