1. ## While Loops! Help me please!!!!

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?

begin
k:=b
while k<5 do
k:=2k-1

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:

begin
k:=1
while k<(or equal to) 4 do
k:=k^2
print k
k:=k+1
end

I am just not getting these. I think I may be over complicating them.

Help me uncomplicate the whole thing. Please!!!!

2. Originally Posted by speakeasy
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?

begin
k:=b
while k<5 do
k:=2k-1
On entry to the look k=3, so the code is executed and k becomes 5.
We return to the top of the loop and perform the test k<5?, which fails
so we exit the loop with k=5.

RonL

3. Originally Posted by speakeasy
Also I get confused when another step is added in the while loop, such as:

begin
k:=1
while k<(or equal to) 4 do
k:=k^2
print k
k:=k+1
end
On entry to the loop k=1, so the code between the do and end is executed;
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.

RonL

4. ## Thanks

Thank you so much Captain Black. I think I got it.