This site requires JavaScript, please enable it in your browser!
Greenfoot back
lm_s
lm_s wrote ...

2019/1/21

How can I restart a loop early?

lm_s lm_s

2019/1/21

#
Hello, I have a while-loop
public void function()
    {
        while(//condition)
        {
            //do something
            
            if(//special condition)
            {
                //interrupt the while-loop
            }
      
            //do something

        }
    }
and at a special condition, I want to restart the entire while-loop. I tried
public void function()
    {
        loop:
        while(//condition)
        {
            //do something
            
            if(//special condition)
            {
                //interrupt the while-loop
                continue loop;
            }
     
             //do something

        }
    }
but it does not work. Is it even possible to restart a loop and if it is possible, how can I do this?
Super_Hippo Super_Hippo

2019/1/21

#
Unless you have a loop into a loop, you can simply use "continue" without naming the loop "loop".
lm_s lm_s

2019/1/21

#
Thank you for your answer, but it does not work. So, this is the code:
loop:
    while(hillExisting("front"))
    {
        var = false;
        if(hillExisting("front") && hillExisting("right"))
        {
            turn("left");
            while(hillExisting("right"))
            {
                drive();
            }
                    
            if(hillExisting("front"))
            {
                continue loop;
            }
                    
            if(!hillExisting("right"))
            {
                turn("right");
                drive();
                while(hillExisting("right"))
                {
                        drive();
                }
                turn("right");
            }   
                    
        }else if(hillExisting("front") && hillExisting("left"))
        {
            turn("right");
            while(hillExisting("left"))
            {
                drive();
            }
                    
            if(hillExisting("front"))
            {
                continue loop;
            }
                    
            if(!hillExisting("left"))
            {
                turn("left");
                drive();
                while(hillExisting("right"))
                {
                    drive();
                }
            }
        }
                
    }   
and i want to restart the first while-loop.
danpost danpost

2019/1/21

#
I think you may have a misunderstanding about the way the act method works. You need to program it for what you want done at each instance of time. The act method is repeatedly called while your project is in a running state -- which is like the initial animation loop. Assuming that there will always be a right or left path to go if not able to go forward:
drive();
if (hillExisting("front")) hillExisting("left") ? turn("right"); : turn("left");
should be sufficient. A choice would need to be made if left and right were both possible ways to go. You can repeat line 2 (copy/paste as line 3) if you have dead ends.
You need to login to post a reply.