-5

i wrote a code structured like this

int function(){
        int i, counter = 0;

        for(i=INTEGER; i>0; ++counter, --i){
               if(condition){
                   //do stuff
                   i+=2;
                   continue;
               }
               if(condition){
                   //do stuff
                   i+=35;
                   continue;
               }
               if(condition){
                   //do stuff
                   continue;
               }
               if(condition){
                   //do stuff
                   continue;
               }
         }
         return counter;
    }

I have problems to fully understand the mechanism of increment of the variables as parameters of for cicle. In the case above:

  • How it's possible that the i variable increases and decreases work well, instead the counter variable at the return has only 1 increase?
  • It's possible?
wing
  • 57
  • 1
  • 9
  • 3
    didn't understand your question. Please clarify! – Raman Jul 13 '15 at 13:50
  • @ARBY if INTEGER define value is 10, as exemple, at the return the variable `counter` should have the value of 10, right? Instead his value is 1. – wing Jul 13 '15 at 13:53
  • 4
    Please post real code that anyone can run. Please also add output (`printf` statements) to the code to demonstrate the problem. – anatolyg Jul 13 '15 at 13:53
  • 2
    he is saying that even though the loop executes multiple times, the variable "counter" gets incremented only once. He is wondering if this is possible, and if it is because of his use of "continue". –  Jul 13 '15 at 13:54
  • @anatolyg - the code is plenty good for us to understand his problem and answer the question. –  Jul 13 '15 at 13:58
  • 1
    @Anon316 In that case, we would like to get the answer from you. Frankly speaking, I also did not understand the question. My English skill is horrible, BTW :-) – Sourav Ghosh Jul 13 '15 at 14:01
  • @SourabGhosh - posted below. basically the comma operator combines the two increments/decrements into one operation, so that if one executes, the other also has to execute. What he thought he was seeing is not possible. –  Jul 13 '15 at 14:06

3 Answers3

1

Variable counter is increased in the for statement the same number of times as variable i is decreased in the for statement

for(i=INTEGER; i>0; ++counter, --i){

After the continue statement this part of the for statement

++counter, --i

is executed.

In fact this loop with the continue statement

    for(i=INTEGER; i>0; ++counter, --i){
           if(condition){
               //do stuff
               i+=2;
               continue;
           }
           //...
     }

is equivalent to the following

    for(i=INTEGER; i>0; ++counter, --i){
           if(condition){
               //do stuff
               i+=2;
               goto Label;
           }
           //...
     Label:;
     }
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • ok, this is what i knows too. But when i execute my code i've only one increase. The variable `counter` isn't used in some other place in my code. This is weird. – wing Jul 13 '15 at 13:57
  • @wing Are you sure that the loop has more than one iteration? Maybe due ti changing of variable i within the body of the loop the loop has only one iteration. – Vlad from Moscow Jul 13 '15 at 14:01
  • But in your code, you modify `i` in the loop body, too, whereas you increase the counter only in the `for` head, unless you touch `counter` in the parts that you've hidden behind `// do stuff`. How do you know that your loop is entered more than once? – M Oehm Jul 13 '15 at 14:01
0

OK guys i debug my code, MISSED the return 0 at the end of main. This caused the wrong return value.

wing
  • 57
  • 1
  • 9
  • 6
    Then please delete you question because it is a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting. – moffeltje Jul 13 '15 at 14:04
  • it dont let me delete the question, i tried right now – wing Jul 13 '15 at 14:07
  • @moffeltje - except that a proper understanding of the comma operator might be useful to other programmers. Wouldn't this make it valuable to keep? –  Jul 13 '15 at 14:07
  • @Anon316 Try imagine you are searching for that particular information, and you would come to this question where the question is unclear and everyone is confused about the question (except you). Would you keep reading or just look at a much [clearer question](http://stackoverflow.com/questions/19236658/is-it-possible-to-do-multiple-operations-in-increment-part-of-for-loop-in-c-c) or even an easy [tutorial](http://www.c4learn.com/c-programming/c-for-loop-types/)? – moffeltje Jul 13 '15 at 14:14
  • @moffeltje - I see your point. You are right. –  Jul 13 '15 at 14:29
0

What you are saying is happening, should not be happening. The comma operator combines two statements so that they are executed as a single statement. If the --1 executed, the ++counter should also be executing at the same time. Something else is going on.

  • Sorry sir, I misunderstood the meaning of `as`. I am not trying to get into any argument over here. I removed my comments. If I had hurt you in any way, sorry for that. I was just trying to be correct. That's all. – Sourav Ghosh Jul 13 '15 at 14:55
  • 1
    No offense taken, and I hope no offense given. I deleted my comments too. "All's well that ends well." By the way, your English is outstanding. –  Jul 13 '15 at 15:39