In: Computer Science
Study the following code with a while-loop and convert it to a for-loop (fill in the blanks).
int i=4, result=1; while(i>1) { result *= i; i--; }
The following for-loop performs the same functionality:
int result=1; for (__________ i=4; i _________1;____________) { result *= i; }
Given Code with while-loop
int i=4, result=1;
while(i>1)
{
result *= i;
i--;
}
It is clear from the value of i that the while loop terminates after i-1 numer of iterations, and performs the product of all the values of i
The for loop representation of the same will be like
int result=1;
for(int i=4;i>1;i--)
{
result*=i;
}