What will be the output for the following code? [code]k=0;i=0; while(k<1 &#038;&#038; i<1) k=k+1; i=i+1;i=i-k; end [/code]

What will be the output for the following code?
Code:
k=0;i=0;
 while(k<1 &#038;&#038; i<1)
k=k+1;
i=i+1;i=i-k;
end
A. i=0,k=1
B. i=0,k=0
C. i=1,k=0
D. i=1,k=1
 
Answer: a
Explanation: We find that i become 0 at the end of the first iteration while k becomes 1. Since k<1 is a condition for the while loop to terminate, the while loop gets terminated. In case of multiple condition, the while loop will be terminated even if one condition is satisfied.
 

Tìm kiếm

Back
Top