Exit when with 2 for loops
Author |
Message |
threatbinder
|
Posted: Sun Dec 20, 2009 8:06 pm Post subject: Exit when with 2 for loops |
|
|
for a : 1 .. 3
for b : 1 .. 3
put i, b
end for
end for
Ok so this will obviously output 11 12 13 21 22 23 31 32 33, but I need it to output just 11, 22, 33. How could I use an exit when for it to output what I want? |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
yumrum
|
Posted: Sun Dec 20, 2009 8:15 pm Post subject: RE:Exit when with 2 for loops |
|
|
i don't think "exit when" is what u want because the stuff u want to eliminate isn't at the end but thats just me i think u need to make it just not print out the 12,13,21,23,31,32
if there's no limit on code u could do
for a : 1..3
for b : 1..3
if x = 12 or x = 13 or x = 23 or x = 31 or x = 32 then
else put a,b
end if
end for
end for |
|
|
|
|
![](images/spacer.gif) |
threatbinder
|
Posted: Sun Dec 20, 2009 8:21 pm Post subject: Re: RE:Exit when with 2 for loops |
|
|
yumrum @ Sun Dec 20, 2009 8:15 pm wrote: i don't think "exit when" is what u want because the stuff u want to eliminate isn't at the end but thats just me i think u need to make it just not print out the 12,13,21,23,31,32
if there's no limit on code u could do
for a : 1..3
for b : 1..3
if x = 12 or x = 13 or x = 23 or x = 31 or x = 32 then
else put a,b
end if
end for
end for
how's that possible? you can't have 2 integers with one variable; the only reason 1 and 2 are side by side is because there's no space inbetween the two numbers
if i misunderstood could you please further elaborate? |
|
|
|
|
![](images/spacer.gif) |
Tony
![](http://wiki.compsci.ca/images/f/f4/OniTony.gif)
|
Posted: Sun Dec 20, 2009 8:23 pm Post subject: RE:Exit when with 2 for loops |
|
|
That code is obviously not supposed to work, as there is no variable x.
Though it should give you an idea of how to use conditionals to decide if you should or should not print output.
P.S. you can do this with less than 5 comparisons. |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
![](images/spacer.gif) |
threatbinder
|
Posted: Sun Dec 20, 2009 8:29 pm Post subject: Re: RE:Exit when with 2 for loops |
|
|
Tony @ Sun Dec 20, 2009 8:23 pm wrote: That code is obviously not supposed to work, as there is no variable x.
Though it should give you an idea of how to use conditionals to decide if you should or should not print output.
P.S. you can do this with less than 5 comparisons.
edit: nvm i'll just try with conditionals, but is there no way with exit when's? |
|
|
|
|
![](images/spacer.gif) |
yumrum
|
Posted: Sun Dec 20, 2009 8:30 pm Post subject: RE:Exit when with 2 for loops |
|
|
Thanks Tony thats basicly what i meant.. i was just showing u what u should sorta do so u can figure it out a learn.. |
|
|
|
|
![](images/spacer.gif) |
threatbinder
|
Posted: Sun Dec 20, 2009 8:42 pm Post subject: Re: RE:Exit when with 2 for loops |
|
|
for a:1..3
for b:1..3
if (a not=1 and b not=2) and (a not=1 and b not=3) and (a not=2 and b not=3) and (a not=3 and b not=1) and (a not=3 and b not=2) then
put a, b
end if
end for
end for
I don't really get it.. how do I use conditionals in this case? because the above I tried and many others don't work. and I really need an efficient way to do this because for my program I'm doing this with 1..16 and 1..16 which is 256 possibilities (and I gotta do that with 4 more (4 quadrants), multiply that by 2 because there's 2 teams which is 2048 >.<![Wink Wink](http://compsci.ca/v3/images/smiles/icon_wink.gif) |
|
|
|
|
![](images/spacer.gif) |
Tony
![](http://wiki.compsci.ca/images/f/f4/OniTony.gif)
|
Posted: Sun Dec 20, 2009 8:51 pm Post subject: Re: RE:Exit when with 2 for loops |
|
|
You are checking too many conditions. Try to think about the patterns in numbers.
Tony @ Sun Dec 20, 2009 8:23 pm wrote: P.S. you can do this with less than 5 comparisons.
... For any size of for-loops. |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
threatbinder
|
Posted: Sun Dec 20, 2009 9:13 pm Post subject: Re: RE:Exit when with 2 for loops |
|
|
Tony @ Sun Dec 20, 2009 8:51 pm wrote: You are checking too many conditions. Try to think about the patterns in numbers.
Tony @ Sun Dec 20, 2009 8:23 pm wrote: P.S. you can do this with less than 5 comparisons.
... For any size of for-loops.
oh okay, i figured it out, but i realized it doesn't quite work in my program.
if a=b then
put a, b
end if
that would work for the bad example i gave, but what about the example below:
for a : -3 .. 6
for b : 3 .. 12
end for
end for
the first for-loop and second for-loop will always be the same inside/outside "boundaries" (e.g., -3 to 6 is 9, and 3 to 12 is 9).
but how do i output -3, 3, -2, 4, -1, 5, ... 5, 11, 6, 12? that means i have to exit once after the second for-loop is done, but i can't do an "exit" after the second-for loop because then when the new value of the first for-loop is -2, the value of the second for-loop will go back to 3 (basically i need the b value to increase/decrease as well after the exit). is this possible? |
|
|
|
|
![](images/spacer.gif) |
Euphoracle
![](http://compsci.ca/v3/uploads/user_avatars/11170373664bf5f25f636f1.png)
|
Posted: Sun Dec 20, 2009 10:35 pm Post subject: RE:Exit when with 2 for loops |
|
|
From our discussion on IRC:
var a := -3
var b := 3
var max := 9
for inc : 0 .. (max - 1)
put a + inc
put b + inc
% This will output -3, 3; -2, 4; -1, 5; etc.
% Consider:
var x := a+inc
var y := b+inc
doSomething(x,y)
end for
Continuing discussion:
Turing: | % this is where your peice will start
var row := 4
var column := 3
% this is the direction we want to move in
var row_direction := 1
var column_direction := -1
loop
% check our current position
if non_empty_position(row, column) then
% something here to tell our program the move is invalid
invalid_move
end if
% move our position in the direction we want to go
row := row + row_direction
column := column + column_direction
% now we've moved 1 square in the direction we want to go
% we need to know when to stop--that is, when we've hit the
% square we want to move to or the end of the board or something
% i'll let you be creative about this
exit when exit_condition_goes_here
end loop |
|
|
|
|
|
![](images/spacer.gif) |
|
|