Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C loops-OK to change counter or condition within loop?

Tags:

c

Several books show me how to correctly write for, while, and do loops. Lots of online articles compare them to each other.

But I haven't found any place that tells me what not to do. For example, would it screw things up if I change the value of the counter or condition variable within the loop?

I would like an answer that is not machine dependent.

like image 926
j yrez Avatar asked May 30 '12 00:05

j yrez


People also ask

Can you modify for loops?

You can modify the loop variable in a for loop, the problem is that for loops in Python are not like "old-style" for loops in e.g. Java, but more like "new-style" for-each loops.

Is it necessary to give condition in while loop?

Since there's no such requirement for while loop (if the condition is omitted), I believe, it's left to the implementation of compiler. Show activity on this post. There is no technical reason explaining why one works and the other doesn't.

WHY ARE FOR loops more efficient than counter loops?

It limits the scope of counter variables better than a while loop , hence it helps in better memory management.

Can you use the same control variable for the inner loop as you do for an outer loop?

Yes you can use the same counter variable name for an inner for loop as for the outer for loop.

What are the different types of looping statements in C?

Depending upon the position of a control statement in a program, looping statement in C is classified into two types: 1. Entry controlled loop 2. Exit controlled loop In an entry control loop in C, a condition is checked before executing the body of a loop. It is also called as a pre-checking loop.

What is a counter controlled loop in C?

Counter Controlled Loop A counter controlled loop is also known as definite repetition loop, since the number of iterations is known before the loop begins to execute. The counter-controlled loop has the following components:

What is a while loop in C++?

It is an entry-controlled loop. In while loop, a condition is evaluated before processing a body of the loop. If a condition is true then and only then the body of a loop is executed.

What is a control statement in a loop?

A loop consists of two parts, a body of a loop and a control statement. The control statement is a combination of some conditions that direct the body of the loop to execute until the specified condition becomes false.


3 Answers

Yes you can change the counter within a loop and it can sometimes be very useful. For example in 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

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 example program automatically increments the counter to access the correct command line string. There are of course ways to incorporate counters etc., but they would only make the code more cumbersome in this case.

As others have pointed out, this is where many people write bugs and one must be careful when incrementing counters within loops, particularly when the loop is conditional upon the counter value.

like image 142
Mosby Avatar answered Oct 05 '22 22:10

Mosby


It is not invalid to change a loop counter inside a loop in C.

However, it is probably confusing to future readers and that's a good reason not to do it.

like image 22
Greg Hewgill Avatar answered Oct 05 '22 20:10

Greg Hewgill


It depends on what you mean by "screw things up".

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

like image 21
Soufiane Hassou Avatar answered Oct 05 '22 22:10

Soufiane Hassou