[try Beta version]
Not logged in

 
For loop adding to a variable

Apr 18, 2013 at 12:50am
I am trying to create a loop that adds 1 to a int variable every time the if statement is true

But while testing the code even though the if statement is true the variable is not incremented, as if the my for loop is not incremented at all....

Code sample:


1
2
3
4
5
6
7
8
9
10
int left_jab_count;
    if(area >=100000 && area1 <100000)
    {
        cout<<"LEFT JAB HAS BEEN THROWN"" "<<area<<endl;
    
        for(int left_jab_count = 0; left_jab_count < 0 ;++left_jab_count)
        {
            cout<<"Left Jab :"<<left_jab_count<<endl;
    	}
    }


can anybody see where am going wrong here ?
Apr 18, 2013 at 12:53am
The loop condition is never true so the loop will never run.
Apr 18, 2013 at 12:55am
well, I can see that the left_jab_count < 0 makes your for loop not work at all because when you initialize left_jab_count to 0 to begin with, you are saying 0 < 0, which is false making it not work.
Apr 18, 2013 at 1:01am
OK thx for your replays guys......But what should i do to make it work in this case.
Apr 18, 2013 at 1:01am
The newly initialised left_jab_count variable in the for block overshadows the mors global one. So your loop doesn't work since 0 is not < 0

Thanks,
Aceix.
Apr 18, 2013 at 1:03am
Maybe change the 0 in the loop condition to the number of times you want the loop to run.
Apr 18, 2013 at 1:09am
Well I dont know how many times the loop should run I want it to be dynamic.......meaning that the if stetment might be meet on many occasions there fore I need left_jab_count variable to increment by one every time the condition is meet....And if i set the 0 in my loop condition to lets say 10 and if there is 11th or 12th occurrence nothing will happen...
Apr 18, 2013 at 4:03am
If all we want to do is increment left_jab_count when the if statement returns true, why is the loop necessary at all? It doesn't appear to actually do anything. Why not just increment, and be done with it?
Topic archived. No new replies allowed.