Braking out of if statement

Hey people

I am writing a program in which i am using a if statement to check if some condition is true, if that condition indeed is true I increment a variable by 1 which essentially works as a counter. The problem is that as soon as the statement is true the variable either gets incremented endlessly or by random number.

I have been trying to use some clause to break out of this statement if condition meet but with no luck

my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
    if(res_vect_angle >=60 && res_vect_angle <=100 && left_mag_b >100)
    								{
    									
    									//line(drawing, *iter_s, *(iter_s -1),  Scalar( 255, 255, 255 ), 2,8 );
    									left_hook_count++;
    									cout<<"Left Hook:..........................!!! "<<left_hook_count<<endl;
    									
    									if(left_hook_count++ == true)
    									{
    										break;
    									}


								}


Can anyone suggest a solution.....?

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
53
54
55
56
57
58
59
60
61
62
OK this is the fool code concerning the issue:

	float M1, M2;
	float A1, A2;
	double left_mag_a, left_mag_b;
	double res_vect_angle;

	int i = 0;
	
		for(vector<Point>::iterator iter_lh = Leftarm.begin(); iter_lh != Leftarm.end(); ++iter_lh)		
		{		
			if(iter_lh->y <=240 && iter_lh->y >=60 && iter_lh->x >=340 && iter_lh->x <=680)
			{
				left_detect.push_back(*iter_lh);
				
				if(i % 4 == 0)
				{
					if(left_detect.size()>4)
						{
							for(vector<Point>::iterator iter_s = left_detect.begin()+3; iter_s != left_detect.end(); ++iter_s, i++)
							{
								//Resultant Magnetude
								M1 = pow((double) iter_s->x + (iter_s -2)->x,2);
								M2 = pow((double) iter_s->y + (iter_s -2)->y,2);
			
								left_mag_a = (M1 + M2);
			
								left_mag_b = sqrt(left_mag_a);
					
								//Resultant Angle
								A1 = abs(iter_s->x - (iter_s -2)->x);
								A2 = abs(iter_s->y - (iter_s -2)->y);

								res_vect_angle = abs(atan2(A1,A2) * 180 /PI);
								//cout<<"LEFT HOOK ANGLE IS: "<<res_vect_angle<<endl;							
								



								if(res_vect_angle >=60 && res_vect_angle <=100 && left_mag_b >100)
								{
									
									//line(drawing, *iter_s, *(iter_s -1),  Scalar( 255, 255, 255 ), 2,8 );
									left_hook_count++;
									cout<<"Left Hook:..........................!!! "<<left_hook_count<<endl;
									
									if(left_hook_count++ == true)
									{
										break;
									}
								}
								
								

							
							}

						}
				}
			}
			
		}
Last edited on
You can only break out of loops I believe. If is not a loop statement.

if you're in a function, you can return;. Or you can set a boolean predicate to false, which then gets checked by the counter incrementor if. Or you can jump out of the statement using a goto.
closed account (N85iE3v7)
If your code lies within a while loop

1
2
3
4
5
6
7
8
while( condition == true )
{
     // ....
     if ( condition2 == true )
     {
          // a break here will get you out of the loop
      }
}
don't use a goto like bourgound said. but yeah use a return or break depending on the situation. and im a little confused on your if statement..you increment it by 1 and then you put an ifstatement saying if it is true it gets incremented then to break some loop? maybe just put another statement in line 1 like if(res_vect_angle >=60 && res_vect_angle <=100 && left_mag_b >100 && left_hook_count != 1) //or what ever value it is when it is incremented
thanks for you suggestions guys, Maybe my question is misunderstood but the problem i had before i added the break; statment was that whenever the if statement if(res_vect_angle >=60 && res_vect_angle <=100 && left_mag_b >100) was true the left_hook_count++; variable was endlessly incremented.

Now i tryied to fix this by adding a break, how i want this break to work is, whenever the statement is true add 1 to the left_hook_count++; variable and then break.

Regards and really sorry if my question was unclear
the only reason it would do that is if you have it inside of a loop. so right after you increment put a break; not inside of another mystery if statement.

EDIT:
I see you updated your code. if you want to break from the for loop on line 20 get rid of lines 47-50 and put break;

ps on line one it is "full" not "fool" =p fool is like someone who doesn't know any better or to trick someone.
Last edited on
Topic archived. No new replies allowed.