user1190778
user1190778

Reputation:

Convert a while loop to for loop in pseudo code

I need to convert this loop to a for loop.

Input:A number k ≥ 0 
Output: Output ?? 
x←0
y←0
while x≤k do 
x←x+1 
y←y+3
return y

Also can you describe me the output of this?

Thank you.

Upvotes: 0

Views: 3393

Answers (1)

amaurea
amaurea

Reputation: 5067

You didn't describe what language you were thinking of, and I didn't recognize the syntax of your while example. But this C code should be a for-loop equivalent of that code.

for(x=0, y=0; x <= k; x++) y += 3;

Of course, if you only care about the result, this could be replaced by

y = 3*(k+1);

Edit: Ok, so as pseudo-code, this could be something like

y←0
for each x from 0 to k inclusive do
    y←y+3
end do
return y

But I find the proper C code much clearer, myself.

Upvotes: 2

Related Questions