Need help understanding boolean
Author |
Message |
ericnewman
|
Posted: Thu Jan 17, 2008 7:31 pm Post subject: Need help understanding boolean |
|
|
I don't quite understand how this procedure works.
proc findprime (num : int)
var counter := 0
var n : real
for decreasing i : num-1 .. 2
n := num / i
if round (n) = n then
counter += 1
else
counter := counter
end if
end for
if counter = 0 then
primes (num) := true
else
primes (num) := false
end if
end findprime |
|
|
|
|
|
Sponsor Sponsor
|
|
|
StealthArcher
|
Posted: Thu Jan 17, 2008 8:13 pm Post subject: RE:Need help understanding boolean |
|
|
It's saying if "f" doesn't have any decimal overflow, than true. |
|
|
|
|
|
Clayton
|
Posted: Thu Jan 17, 2008 9:45 pm Post subject: RE:Need help understanding boolean |
|
|
Why am I seeing this so much lately? What on earth is that supposed to accomplish? |
|
|
|
|
|
Euphoracle
|
Posted: Thu Jan 17, 2008 10:01 pm Post subject: RE:Need help understanding boolean |
|
|
People think that they are accomplishing something by using superfluous code in an else block. |
|
|
|
|
|
LaZ3R
|
Posted: Thu Jan 17, 2008 10:21 pm Post subject: RE:Need help understanding boolean |
|
|
People love complicating things for themselves. I'll never quite understand why people don't just try to start simple and if no simple solution is available, move up to the next level and see if there's a slightly harder but more efficient approach. |
|
|
|
|
|
Zampano
|
Posted: Thu Jan 17, 2008 11:12 pm Post subject: Re: Need help understanding boolean |
|
|
I guess being unhelpful is also a fad of late. And a merry fad it is!
You highlighted some text. The text starts a forloop for the number entered into the procedure. Each time through. If num divided by i is equal to i rounded, then counter is up one. Effectively, it is asking whether i can factor without any decimals into num. If it was true, then the number would be composite (that's the word, correct) as opposed to prime, because it has a number below it which divides evenly into it besides 1 (which is excluded for the forloop, wisely). If it were to be false every time, then counter would still be 0 and the number is prime.
About boolean values: they are variables which can only hold two states, true or false. Like an int which you only intend to held 0 or 1. They are very useful, and and could be used again in this procedure to replace counter. Having more than two states for counter is redundant as it only needs to know if there was a factored number or not. In fact, you could put an exit statement and change it to a normal loop. |
|
|
|
|
|
|
|