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
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.
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.