A simple "exit when" question
Author |
Message |
bigbut600
|
Posted: Wed Oct 08, 2008 9:34 pm Post subject: A simple "exit when" question |
|
|
I need to exit a loop when the last number is finally outputted.
But I seem to be doing something wrong because I am expecting the number 1 to be listed in the execution, but also I need to exit the loop and still have that number 1 there.
If I put, exit when factor = 1 it will exit the loop, but the number 1 won't even be outputted.
I don't even think it is the write way to exit.
Any suggestion would be great.
Thanks!
Here's my work. Look near the end.
code: |
var integer: int
var count: int := 0
var factor: real
put "Enter an integer between 1 and 50."
get integer
put "The factors of ", integer, " are:"
loop
count:= count + 1
factor:= (integer div count)
if integer = factor*count
then put factor
end if
%I NEED AN EXIT WHEN LINE HERE
%MY LAST ONE WAS...see below
% if you take out that line, everything works but
% it is not completed
exit when factor = 1
end loop
|
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
The_Bean
|
Posted: Wed Oct 08, 2008 9:45 pm Post subject: Re: A simple "exit when" question |
|
|
Mod Edit: that was a complete solution to the assignment, not an answer relating to the question. |
|
|
|
|
|
gitoxa
|
Posted: Wed Oct 08, 2008 9:57 pm Post subject: RE:A simple "exit when" question |
|
|
A simpler way to find out if a number is a factor would be when this statement is true
code: | (Number mod PossibleFactor) = 0 |
The reason your program isn't outputting 1 is because the statement 50 div 40 is equal to 1. The best way to figure out a problem like this is to echo all variables that could cause the problem to the screen. If you do this, you'll see your program exits the loop long before the count is up to 50. |
|
|
|
|
|
|
|