Need Help With goto Statement

Hello, this is my first post on this fourm. I am programming my Pololu 3pi robot so when it loses the line it will slowly do larger circles until it reads the line, when it will do a goto statement and start running the program that will follow the line. I just need help on how to make it start running at an earlier point in the program.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
while(1)
    {
        // Get the position of the line.  Note that we *must* provide
        // the "sensors" argument to read_line() here, even though we
        // are not interested in the individual sensor readings.
        unsigned int position = read_line(sensors,IR_EMITTERS_ON);
		
 
        if(position < 1000)
        {
            // We are far to the right of the line: turn left.
 
            // Set the right motor to 100 and the left motor to zero,
            // to do a sharp turn to the left.  Note that the maximum
            // value of either motor speed is 255, so we are driving
            // it at just about 40% of the max.
            set_motors(5,75);
 
            // Just for fun, indicate the direction we are turning on
            // the LEDs.
            left_led(1);
            right_led(0);
        }
        else if(position < 3000)
        {
            // We are somewhat close to being centered on the line:
            // drive straight.
            set_motors(75,75);
            left_led(1);
            right_led(1);
        }
		else if(position == 4000)
		{
			set_motors(5,75);
			delay_ms(1000);
			set_motors(10,75);
			delay_ms(1000);
			set_motors(15,75);
			delay_ms(1000);
			set_motors(20,75);
			delay_ms(1000);
			set_motors(30,75);
			delay_ms(1000);
			set_motors(40,75);
			delay_ms(1500);
			set_motors(50,75);
			delay_ms(1500);
			set_motors(60,75);
			delay_ms(10000);
			if(position < 4000)
			{
				goto while(1);


So i pretty much just want to make this goto statement go back up to the while loop. Can anyone help?
Thanks,
pieceOfPi
You could always just use continue instead of a goto. If you really want to use a goto you have to create a label first then goto the label. Anyways it is advised to stay away from goto statements. The main reason is so your code is more readable and easier to follow what is going on.

[edit]http://www.cplusplus.com/doc/tutorial/control/ --towards the bottom
Last edited on
Thanks!
Topic archived. No new replies allowed.