Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

REPEAT UNTIL loop misprint?

Status
Not open for further replies.

MrDEB

Well-Known Member
been doing some reading on picbasic/picbasicpro by Dogan Ibrahim
It has an example of a REPEAT UNTIL loop that looks like a misprint? or am I not understanding it correctly?

k=0
Sum=Sum + 1
Cnt = Sum
k = k + 1
UNTIL k < 10



The book states that the routine is executed 10 times
BUT
k will never be less than 10 if I am interpreting this right?
Isn't it supposed to be UNTIL k > 10
 
Last edited:
Hi, Mr DEB

Not Joking ...

You'll find the answer ... in the PBP Manual !!!

very similar example from " the source "

Alain
 
Do not have a PicBasicPro manua
Only a book on PBP
Yes I realize 0 to 9 is 10 counts but if k < 10 then shouldn't it jump from the loop once it hits the UNTIL statement the first time?
 
Do not have a PicBasicPro manua
Only a book on PBP
Yes I realize 0 to 9 is 10 counts but if k < 10 then shouldn't it jump from the loop once it hits the UNTIL statement the first time?

Yep. Unless there's something else going on, that will execute once and then exit the loop once it realizes that k == 1, and 1 < 10. :) Looks like a typo to me. I also suspect it should be "UNTIL k >= 10" to avoid a fencepost error.


Cheers,

Torben
 
MUST be a typo
I read it several times and the way I posted it is exactly the way its in the book
 
There also seems to be a missing REPEAT. I suspect it should read,
Code:
k=0
[COLOR="red"]REPEAT[/COLOR]
    Sum=Sum + 1
    Cnt = Sum
    k = k + 1
UNTIL k [COLOR="red"]=[/COLOR] 10

If it was UNTIL k>10 then it would repeat 11 times.

Edit, just realised what "fencepost error" means.:eek:

Mike.
 
Last edited:
If you need 10 fence panels then you need 11 fenceposts. Similarly, 5 to 7 is 3 iterations, not (7-5)=2.

Mike.
 
If you need 10 fence panels then you need 11 fenceposts. Similarly, 5 to 7 is 3 iterations, not (7-5)=2.

Mike.

Yup. I used the term loosely--if you Google it* you'll find a more strict definition than what I meant, but essentially it's an off-by-one error. Like Mike said, in the case of the loop you posted, it will iterate 11 times instead of 10 because of a logic error.

(* When you encounter a new term like "fencepost error", a good idea is to Google something like "wiki fencepost error" to see the Wikipedia article on it. It's usually faster and more accurate than waiting for a reply to a forum post, I find.)

The loop checks the value of k at the end of the loop instead of at the beginning. Because of this (and because k starts at 0, not 1), it must exit as soon as k = 10. Otherwise, when it sees that k is 10, and tests whether k < 10, it sees that k is not greater than 10, and does an extra iteration.

So you either need to use k = 10 (or k >= 10, which is safer but perhaps costlier in terms of an extra cycle or two), or else check if k > 10 at the beginning of the loop:

PHP:
while ($k < 10) {
    $sum = $sum + 1;
    $cnt = $sum;
    $k = $k + 1;
    printf("%10d %10d %10d\n", $k, $sum, $cnt);
}

I don't know if your language has the while() loop, but it probably has something like it. The above code is in PHP but should be similar in many languages.


Good luck,

Torben
 
Last edited:
I would guess it's while....wend. So it would be,
Code:
k=0
WHILE k<10
    Sum=Sum + 1
    Cnt = Sum
    k = k + 1
WEND
But an even simpler way is to use a for....next loop.
Code:
FOR k=0 to 9
    Sum=Sum + 1
    Cnt = Sum
NEXT
Takes two less lines to do the same thing.

Mike.
 
I would guess it's while....wend. So it would be,
Code:
k=0
WHILE k<10
    Sum=Sum + 1
    Cnt = Sum
    k = k + 1
WEND
But an even simpler way is to use a for....next loop.
Code:
FOR k=0 to 9
    Sum=Sum + 1
    Cnt = Sum
NEXT
Takes two less lines to do the same thing.

Mike.

Agreed (though I don't know Picbasic). But there are a few more things wrong with the original code, anyway. One botched example isn't enough to go by, but after an example like that I'd tend to recommend turning to another book. ;)


Torben
 
Hi, Mr DEB

Generally PBP is sold with its manual ...

But don't loose time with such an uninteresting detail ...

get the 2.50 Version Manual HERE:

https://melabs.com/downloads/pbpm108.pdf

it's free !!! *** smiles ***

Alain ... happy PBP user for ... about 10 years.

BTW: Dogan's books are " not so terrible " ( never found any error ...) , so ... let's bet for a typo !!!
 
Last edited:
Status
Not open for further replies.

Latest threads

Back
Top