Problem with an if statement inside a while loop

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Problem with an if statement inside a while loop

SikoraP13
This post was updated on .
Hi everybody.  I'm having a problem where Octave is giving me a parse error syntax error problem with my code.  I've copied the code below.  The error I'm receiving says

parse error near line __ of file....

syntax error

>>> else
           ^


Can anyone provide any guidance as to what the problem might be? The problem area in the code is denoted.  I want it to go through each point in commandList, and change the X and Y positions and the angle accordingly and add all of those points to the vector.




clear()
init='LFL+F+LFL'; %First Iteration (AXIOM)

L='-RF+LFL+FR-';  %INPUT GENERATORS HERE
R='+LF-RFR-FL+';

it= 4;   %desired # of iterations
angle(1)= 0; %Initial Angle
scalingFactor=4; %Scaling Factor
angleInc=90;     %Angle Increment

pos=[0,0,0] %Initial X, Y, and Angle


i=1;
currentString(i)=cellstr(init);

for i=1:it-1
        newStringL= strrep(currentString(i),'L', 'C');
        newStringR= strrep(newStringL, 'R', R);
        newString= strrep(newStringR,'C', L);
        currentString(i+1)=newString;

endfor

commandList=currentString(1,it);
commandList=char(commandList);

ii=2;

%%%%%%%%%%%%%%%
%%%%PROBLEM%%%%
%%%%%%%%%%%%%%%

while ii<=length(commandList)+1
        if commandList(1,ii-1)=="+"
                (
                  %angle(i+1)=angle(i)+angleInc
                  pos(ii,:)=pos(ii-1,:)+[0,0,90]
                )
        elseif commandList(1,ii-1)=="-"
                (
                        pos(ii,:)=pos(ii-1,:)-[0,0,90]
                  %angle(i+1)=angle(i)-angleInc
                 
                )
        elseif commandList(1,ii-1)=="F"
                (
                        pos(ii,:)=pos(ii-1,:)+1./scalingFactor.^it.*[cos([pos(ii-1,3),sin([pos(ii-1,3),0]
                        %angle(i+1)=angle(i)
                )
               
        else
                (
                        pos(ii,:)=pos(ii-1,:)
                        pos(ii-1,:)+1
                )
        endif
ii++
end
Reply | Threaded
Open this post in threaded view
|

Re: Problem with an if statement inside a while loop

James Sherman
On Thu, Aug 1, 2013 at 9:51 AM, SikoraP13 <[hidden email]> wrote:
Hi everybody.  I'm having a problem where Octave is giving me a parse error
syntax error problem with my code.  I've copied the code below.  The error
I'm receiving says

parse error near line __ of file....

syntax error

>>> else
           ^


Can anyone provide any guidance as to what the problem might be? The problem
area in the code is denoted.  I want it to go through each point in
commandList, and change the X and Y positions and the angle accordingly and
add all of those points to the vector.




clear()
init='LFL+F+LFL'; %First Iteration (AXIOM)

L='-RF+LFL+FR-';  %INPUT GENERATORS HERE
R='+LF-RFR-FL+';

it= 4;                          %desired # of iterations
angle(1)= 0;            %Initial Angle
scalingFactor=4;        %Scaling Factor
angleInc=90;            %Angle Increment

pos=[0,0,0]                     %Initial X, Y, and Angle


i=1;
currentString(i)=cellstr(init);

for i=1:it-1
        newStringL= strrep(currentString(i),'L', 'C');
        newStringR= strrep(newStringL, 'R', R);
        newString= strrep(newStringR,'C', L);
        currentString(i+1)=newString;

endfor

commandList=currentString(1,it);
commandList=char(commandList);

ii=2;

%%%%%%%%%%%%%%%
%%%%PROBLEM%%%%
%%%%%%%%%%%%%%%

while ii<=length(commandList)+1
        if commandList(1,ii-1)=="+"
                (
                        %angle(i+1)=angle(i)+angleInc
                        pos(ii,:)=pos(ii-1,:)+[0,0,90]
                )
        elseif commandList(1,ii-1)=="-"
                (
                        pos(ii,:)=pos(ii-1,:)-[0,0,90]
                        %angle(i+1)=angle(i)-angleInc

                )
        elseif commandList(1,ii-1)=="F"
                (

pos(ii,:)=pos(ii-1,:)+1./scalingFactor.^n.*[cos([pos(ii-1,3),sin([pos(ii-1,3),0]
                        %angle(i+1)=angle(i)
                )

        else
                (
                        pos(ii,:)=pos(ii-1,:)
                        pos(ii-1,:)+1
                )
        endif
ii++
end




--
View this message in context: http://octave.1599824.n4.nabble.com/Problem-with-an-if-statement-inside-a-while-loop-tp4656310.html
Sent from the Octave - General mailing list archive at Nabble.com.
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave

Without actually testing it to be sure, but I believe the problem isn't with your if statement, its with the parentheses that you're using like {} in c.  Try deleting those () and see what happens.

Hope this helps,

James Sherman

_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Problem with an if statement inside a while loop

Markus Bergholz
In reply to this post by SikoraP13



On Thu, Aug 1, 2013 at 3:51 PM, SikoraP13 <[hidden email]> wrote:
Hi everybody.  I'm having a problem where Octave is giving me a parse error
syntax error problem with my code.  I've copied the code below.  The error
I'm receiving says

parse error near line __ of file....

syntax error

>>> else
           ^


Can anyone provide any guidance as to what the problem might be? The problem
area in the code is denoted.  I want it to go through each point in
commandList, and change the X and Y positions and the angle accordingly and
add all of those points to the vector.




clear()
init='LFL+F+LFL'; %First Iteration (AXIOM)

L='-RF+LFL+FR-';  %INPUT GENERATORS HERE
R='+LF-RFR-FL+';

it= 4;                          %desired # of iterations
angle(1)= 0;            %Initial Angle
scalingFactor=4;        %Scaling Factor
angleInc=90;            %Angle Increment

pos=[0,0,0]                     %Initial X, Y, and Angle


i=1;
currentString(i)=cellstr(init);

for i=1:it-1
        newStringL= strrep(currentString(i),'L', 'C');
        newStringR= strrep(newStringL, 'R', R);
        newString= strrep(newStringR,'C', L);
        currentString(i+1)=newString;

endfor

commandList=currentString(1,it);
commandList=char(commandList);

ii=2;

%%%%%%%%%%%%%%%
%%%%PROBLEM%%%%
%%%%%%%%%%%%%%%

while ii<=length(commandList)+1
        if commandList(1,ii-1)=="+"
                (
                        %angle(i+1)=angle(i)+angleInc
                        pos(ii,:)=pos(ii-1,:)+[0,0,90]
                )
        elseif commandList(1,ii-1)=="-"
                (
                        pos(ii,:)=pos(ii-1,:)-[0,0,90]
                        %angle(i+1)=angle(i)-angleInc

                )
        elseif commandList(1,ii-1)=="F"
                (

pos(ii,:)=pos(ii-1,:)+1./scalingFactor.^n.*[cos([pos(ii-1,3),sin([pos(ii-1,3),0]
                        %angle(i+1)=angle(i)



1st is wrong! [cos([pos(ii-1,3),sin([pos(ii-1,3),0]
take a look at the brackets (count it!). but i don't know what you're trying to do there.
furthermore n is undefined!
and ii++ don't work in octave/matlab! and you don't need the brackets for if elseif and else.

this works fine for me! but you have to define n in line 35 as your needs and fix the calculation in line 42.
















 
                )

        else
                (
                        pos(ii,:)=pos(ii-1,:)
                        pos(ii-1,:)+1
                )
        endif
ii++
end




--
View this message in context: http://octave.1599824.n4.nabble.com/Problem-with-an-if-statement-inside-a-while-loop-tp4656310.html
Sent from the Octave - General mailing list archive at Nabble.com.
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave



--
icq: 167498924
XMPP|Jabber: [hidden email]

_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Problem with an if statement inside a while loop

Richard Kirk
In reply to this post by SikoraP13
Try it without the brackets...

while ii<=length(commandList)+1
        if commandList(1,ii-1)=="+"
                %angle(i+1)=angle(i)+angleInc
                pos(ii,:)=pos(ii-1,:)+[0,0,90]
        elseif commandList(1,ii-1)=="-"
                pos(ii,:)=pos(ii-1,:)-[0,0,90]
                %angle(i+1)=angle(i)-angleInc
        elseif commandList(1,ii-1)=="F"
                pos(ii,:)=pos(ii-1,:)+1./scalingFactor.^n.*[cos([pos(ii-1,3),sin([pos(ii-1,3),0]
                %angle(i+1)=angle(i)
        else
                pos(ii,:)=pos(ii-1,:)
                pos(ii-1,:)+1
        endif
ii++
end

Cheers.
Richard Kirk

_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Problem with an if statement inside a while loop

SikoraP13
In reply to this post by James Sherman
Thanks for the response, James.  I just tried that and I am still getting the same error.  I've also tried putting the if statements in a for loop, but get another problem.  It gives me:

syntax error

>>>  angle(i+1)=angle(i)
               ^

however if I comment out all of the pos items, it runs fine.  This leads me to believe there may be something with my indexing, but I'm not completely sure what it is.  Either the for or the while will work for what I need it to, so any help fixing either solution would be appreciated.


I essentially tried replacing the while loop with this statement:
for i=1:length(commandList)

                if commandList(1,i)=="+"
                        pos(i+1,:)=pos(i,:)+[0,0,90]
                  angle(i+1)=angle(i)+angleInc
                 
                elseif commandList(1,i)=="-"
                        pos(i+1,:)=pos(i,:)+[0,0,90]
                  angle(i+1)=angle(i)-angleInc
                 
                elseif commandList(1,i)=="F"
                        pos(i+1,:)=pos(i,:)+1./scalingFactor.^n.*[cos([pos(i,3),sin([pos(i,3),0]
                        angle(i+1)=angle(i)
                       
                else
                        pos(i+1,:)=pos(i,:)
                        angle(i+1)=angle(i)
                                       
                end
Reply | Threaded
Open this post in threaded view
|

Re: Problem with an if statement inside a while loop

andy buckle
In reply to this post by Markus Bergholz


and ii++ don't work in octave/matlab!


i++ does work in Octave

(sorry I have not follosed the other details of this thread)

--
/* andy buckle */

_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Problem with an if statement inside a while loop

SikoraP13
In reply to this post by Markus Bergholz
This works perfectly! Thank you so much for your help.  
Reply | Threaded
Open this post in threaded view
|

Re: Problem with an if statement inside a while loop

Markus Bergholz
In reply to this post by andy buckle



On Thu, Aug 1, 2013 at 4:31 PM, Andy Buckle <[hidden email]> wrote:


and ii++ don't work in octave/matlab!


i++ does work in Octave

(sorry I have not follosed the other details of this thread)

uh you're absolutly right. that was new to me because in matlab it don't work, so i never used it :D


 


--
/* andy buckle */



--
icq: 167498924
XMPP|Jabber: [hidden email]

_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Problem with an if statement inside a while loop

Markus Bergholz
In reply to this post by SikoraP13



On Thu, Aug 1, 2013 at 4:32 PM, SikoraP13 <[hidden email]> wrote:
This works perfectly! Thank you so much for your help.


if this belongs to my paste, i hope you've recognized that i've removed one part of your calculation whitch  made the error. so you have to fix this part and add it again. otherwise the solution may be wrong.
 



--
View this message in context: http://octave.1599824.n4.nabble.com/Problem-with-an-if-statement-inside-a-while-loop-tp4656310p4656317.html
Sent from the Octave - General mailing list archive at Nabble.com.
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave



--
icq: 167498924
XMPP|Jabber: [hidden email]

_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave