I am having issues with while loops ( or do while loops I think is another name for them). While loops seem so easy and I feel like I get them until I have to do problems on my own.
Can anyone explain them to me?
here is a problem from my homework:
which values of b do the following algorithm terminate?
while k<5 do
Now the answer I get is that it terminates at 3
b k k<5
3 5 False (cause k cannot = 5 right?)
but the answer in the back of the book is b>(or equal to)=2.
What am I doing wrong?!?
Also I get confused when another step is added in the while loop, such as:
while k<(or equal to) 4 do
I am just not getting these. I think I may be over complicating them.
Help me uncomplicate the whole thing. Please!!!!
k becomes 1, which is printed, then k becomes 2.
We now return to the top of the loop with k=2, which is <=4 so the code
inside the loop is again executed. k becomes k^2 or 4, which is printed,
then k becomes 5.
We now return to the top of the loop with k=5, which fails the test so
we exit the loop with k=5.