Computer Science Canada loop help please |
Author: | jayshaw94 [ Fri Apr 08, 2011 10:51 am ] |
Post subject: | loop help please |
Hello compsci once again i come with another problem I am having trouble using a loop statement. Here is the problem. If the user enters a number that is greather than 2 a message will come up stating "Error please try again". I have that message working but it won't exit the loop if the number is less than equal or less than 2. I tried if statements aswell with no luck. Any help would be great. I bolded the the area i am having issues with var h1, h2, h3, w1, w2, w3, choice, HOF1, HOF2, WOF1 : int var RailA := 46 var RailB := 41 procedure Form1 if choice = 1 then HOF1 := (h1) - (RailA) put "Height of frame = ", HOF1, " Width of Frame ", w1 else HOF2 := (h1) - (RailB) put "Height of Frame = ", HOF2, " Width of Frame ", w1 end if end Form1 procedure RailChoice put "Rail A (1)Set on the Floor" put "Rail B (2)Set into the Floor" get choice loop if choice > 2 then put "Error please try again" RailChoice end if end loop end RailChoice procedure hwconfig put "Enter lowest room height" get h1 put "Enter second lowest room height" get h2 put "Enter third lowest room height" get h3 put "Enter lowest room width" get w1 put "Enter second lowest room width" get w2 put "Enter third lowest room widht" get w3 if (h1) - (h2) > 12 then put "Exceeds 12mm limit Rail B is the default" elsif (h2) - (h3) > 12 then put "Exceeds 12mm limit Rail B is the default" elsif (h3) - (h1) > 12 then put "Exceeds 12mm limit Rail B is the default" elsif (h1) - (h3) > 12 then put "Exceeds 12mm limit Rail B is the default" end if end hwconfig RailChoice hwconfig Form1 |
Author: | goroyoshi [ Fri Apr 08, 2011 11:01 am ] |
Post subject: | RE:loop help please |
try using "exit when" |
Author: | jayshaw94 [ Fri Apr 08, 2011 11:16 am ] |
Post subject: | RE:loop help please |
thanks tried exit when before it wasnt working but i tried again i guess i was initially doing it wrong thanks! |
Author: | Insectoid [ Fri Apr 08, 2011 11:19 am ] | ||
Post subject: | RE:loop help please | ||
Not related to your question, but
This can be cleaned up, a LOT. Note that this will *always* fail (you'll always get the message 'Exceeds 12mm limit....'). |
Author: | Raknarg [ Fri Apr 08, 2011 3:18 pm ] | ||||
Post subject: | RE:loop help please | ||||
You have a few problems here.
First of all, there's no exit statement. So if your answer is less than 3, it will loop for all eternity. Then you seem to try and fix it with recursion. All this will do is call itself inside itself, like this:
and so on. It also creates an infinite loop, but of endless procedures. What you need to do is put everything inside the procedure in the SAME loop, and use an exit statement. And do not call itself, it's unnescessary with what you're doing. |
Author: | TokenHerbz [ Fri Apr 08, 2011 7:19 pm ] |
Post subject: | RE:loop help please |
i just want to point out, if there is no "loop" then theres no required "exit" for that. One could think of it as a "call word" get word "if word = this -> then that -> else go onwards. you could also include the recursion setup into this. proc test get "tester" if tester < 2 then test %again end if %got test and do test etc, end test ^^the above isn't tested but would work ![]() |