C loops-OK to change counter or state in a loop?

Several books show me how to write, and also make loops. Many online articles compare them to each other.

But I did not find a place that tells me what not to do. For example, if I changed the value of a counter or a condition variable inside a loop?

I need an answer that is machine independent.

+8
c
source share
10 answers

Yes, you can change the counter in a loop, and sometimes it can be very useful. For example, when parsing command line arguments, where there is an option flag followed by a value. An example of this is shown below:

Enter the following command:

program -f filename -o option -p another_option

the code:

 #include <string.h> int main(int argc, char** argv) { char *filename, *option, *another_option; if(argc > 1){ for(int i=1; i<argc; i++){ if(strcmp(argv[i],"-f")==0){ filename = argv[++i]; } else if(strcmp(argv[i],"-o")==0){ option = argv[++i]; } else if(strcmp(argv[i],"-p")==0){ another_option = argv[++i]; } else { printf("Option \"%s\" not recognized, skipping\n",argv[i]); continue; } } } /* end if argc > 1 */ return 0; } 

The sample program automatically increments the counter to access the correct command line. Of course, there are ways to enable counters, etc., but in this case they will make the code more cumbersome.

As others have pointed out, this is what many people write errors, and you need to be careful when adding counters to loops, especially when the loop depends on the value of the counter.

+7
source share

Invalid change of loop counter inside loop in C.

However, this probably confuses future readers, and this is a good reason not to.

+7
source share

It depends on what you mean by screw.

If you know what you are doing, you can change the counter. The language has no restrictions.

+4
source share

Changing the counter variable in a loop is allowed, but be careful to know what you are doing so as not to create infinite loops, decreasing the variable when you shouldn't be.

Some algorithms really benefit from this, but of course, if you do, it will make your code less readable, so be sure to comment on what you are doing.

+4
source share

Yes, you can change counter variables and conditions. They will simply be evaluated, followed by a loop iteration.

+3
source share

Determine what you can. But be careful not to create a cycle mess. Changing a connector in a loop happens very often ... while and while.

 do{ counter++; some expressions; } while(counter < SOMEVALUE); 
+2
source share

Yes, in C / C ++ / C # you can change the counter, etc. in a loop.

+1
source share

Like many other methods, as long as you know what you are doing, that’s fine.

For example, this code:

 int i; for (i=0;i<5;i++) printf("%d\n",i--); 

is an infinity loop, but this version is like bubbles:

 int *arr,n; //allocate memory, assign values, and store the length of the array in n int i; for (i=0;i<n-1;i++) if (arr[i]>arr[i+1]) { int temp=arr[i]; arr[i]=arr[i+1]; arr[i+1]=temp; if (i) i-=2; } 

OK. (This is not just sorting bubbles. Instead of using nested loops, I return to the array after replacing the members in it)

+1
source share

Be careful when changing the counter variable inside the loop, not all loops are the same, while the loop behaves differently. see this example?

No, this is NOT an infinite loop (on some compilers). run it and see ...

 i=5; while (i--) { i=100; printf("%d \n",i) } 
+1
source share

We can change the counter value inside the loop, but the final counter value will not be reflected, because the for loop does not replace the counter value. here is an example in a QTP VB script.

 iLast = 4 For i= 1 to iLast iLast=2 Next 

Here, the for loop should only run 2 times, since the iLast value is updated to 2 inside the loop, but 4 times.

0
source share

All Articles