RobotStudio event

Is there a similar instruction to Break in C prog

Options
Hi,
   I would like to know if there is an equivalent statement to that of Break statement in c language?

For example consider the for loop in a c program

for(i=0;i<10;i++)
{
   if(i==5)
   break;
}
when i is equal to 5 the for loop exits.How can I do the same in a RAPID program?

Comments

  • RussD
    Options
    RETURN;
    Russell Drown
  • ravi
    Options
    Hi,
        I am using it in Main routine.As soon as return is executed the program pointer goes to the first instruction of the main.What I want is when return is executed it has to exit the for loop.
    For example consider the below mentioned program

    1    MODULE TestProgram
    2    !This program for testing purpose
    3    var num i1;
    4    var num j1;
    5    var num ww{3,4};
    6    PROC main()
    7    TPErase;
     
    8    FOR j1 FROM 1 TO 5 DO
    9    if j1=3 THEN

    10  TPWrite"first for loop"NUM:=j1;
    11  RETURN;
    12  endif
    13  if j1=4 THEN
    14  TPWrite"second for loop"NUM:=j1;
    15   ENDIF
    16  ENDFOR
    17  TPWrite""Num:=j1;
    18  ENDPROC
    19  ENDMODULE

    If i use a  return statement in line number 11,the program goes to the first instruction.But my requirement is that i has to exit the for loop when the instruction at line number 11 is executed.After that instruction at line number 17 has to be executed.Is there a way to do this??
    Ravi


  • RussD
    Options

    Break your program up into procedure or function calls that return you back into main.

    Russell Drown
  • Try

    1    MODULE TestProgram
    2    !This program for testing purpose
    3    var num i1;
    4    var num j1;
    5    var num ww{3,4};

        PROC Loop()
         FOR j1 FROM 1 TO 5 DO

           if j1=3 THEN

            TPWrite"first for loop"NUM:=j1;

            RETURN;

          endif

          if j1=4 THEN

            TPWrite"second for loop"NUM:=j1;

          ENDIF

        ENDFOR

       ENDRPOC

    6    PROC main()
    7    TPErase;
          Loop;
    17  TPWrite""Num:=j1;
    18  ENDPROC
    19  ENDMODULE

    Main routine will call "loop" routine.
    When j1 = 3 the "loop" will reutrn PP to Main,  line "17".
    J1 will never reach 4 thus never display "second for loop"

    /eiven

    Eiven
    Win 7-64
    RS 5.15RC1
    Q9950 16gb
  • Marcel
    Options

    Hi

    The j1 is declaired two times. Once inside the FOR ... ENDFOR and once outside with var num j1. So you will not see the the last value of j1 from inside the loop in line 17. It will only show the start value, witch is not declaired.

    The solution with RETURN should work.

     

    An other solution:

         FOR j1 FROM 1 TO 5 DO
           IF j1<=3 THEN
             if j1=3 THEN
              TPWrite"first for loop"NUM:=j1;
             endif

             if j1=4 THEN
               TPWrite"second for loop"NUM:=j1;
             ENDIF
           ENDIF
        ENDFOR

     


    An other solution:

         IF j1=0;
         WHILE j1<5 DO

             j1:=j1+1;
             if j1=3 THEN
              TPWrite"first for loop"NUM:=j1;

              j1:=5;
             endif

             if j1=4 THEN
               TPWrite"second for loop"NUM:=j1;
             ENDIF
        ENDWHILE

    Best regards

    Marcel

  • lemster68
    lemster68 ✭✭✭
    Options

    Greetings all,

    I think that the critical part to getting this to work past line 11 is the fact that the value of j1 is only present within the FOR NEXT loop.  Each time you reenter the loop, the value of j1 is reinitialized.  You should then maybe look at storing the j1 value in another persistant variable before exiting and then upon reentry, initialize j1 as the value of the persistant variable.  Good luck,
    Lee Justice