What is the nature of the following code? [code]j=0;i=1; while(j>5) for i=1:8 j=j+i; end end[/code]

Hưng Thịnh

New member
What is the nature of the following code?
Code:
j=0;i=1;
while(j>5)
for i=1:8
j=j+i;
end
end
A. j=0 & i=1
B. j=1 & i=0
C. i=8 & j=36
D. Error
 
Answer: a
Explanation: We find that the inner while loop goes into an infinite loop. MATLAB is unable to compute the value so it returns j=0 as was initialized. The same goes for i=1. If there was no while loop, option i=8 & j=36 would have been the output. There is no error in logic.
Output: No output will be shown since we have placed a semi-colon after j=j+i. But the workspace will store i & j as 1 & 0 respectively.
 

Tìm kiếm

Back
Top