answersLogoWhite

0


Best Answer

By changing the loop counter variable within the loop can cause the loop to exit early or to loop more times than the limit.

The is nothing that says that you can not change the loop counter variable within the loop, you may indeed want to do that. Just be careful and be sure of what will happen.

User Avatar

Wiki User

13y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

6y ago

Counted loops are best implemented as for loops because this allows us to make the control variable local to the loop:

for (int x=0; x<10; ++x) {

// ...

}

x = 42; // error: x is no longer in scope...

We can also declare the loop using a while or do loop, however this means the control variable must be declared outside the loop, widening the scope of the variable:

int y=0;

while (y++<10) {

...

}

y = 42; // ok

int z=0;

do {

} while (z++<10);

z = 42; // ok

Regardless of which version we use, the control variable is always within the scope of the loop, which means that any statement in the loop body can change its value. However, if the control statement also changes the value, as it does in all three examples using the increment operator (++), we'd be changing the value twice on each iteration, which would naturally change the number of iterations:

for (int x=0; x<10; ++x) {

// ...

++x; // really?

}

Here, the loop will execute just 5 times because at the start of each successful iteration x would be 0, 2, 4, 6 and 8. On the next iteration x is 10, thus x<10 is false and the loop terminates. This is probably not what we wanted because the forstatement clearly tells us that we expect this loop to execute exactly 10 times.

Note that it is not unusual for loop bodies to modify a control variable, hence it has to be accessible from within the loop body, but with counted loops we should avoid doing this as it could adversely affect the count. If we need to modify a counter variable (without affecting the loop count), then we should copy it and modify the copy instead:

for (int x=0; x<10; ++x) {

int y {x}; // copy x

++y; // modify the copy

// use y...

}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why should you be careful not to place a statement in the body of a for loop that changes the value of the loop's counter variable?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Print 1-10 numbers using while loop?

public static void main(String args){ int counter = 0; //initialize the counter variable to 0 while (counter &lt; 10){ //while the counter variable is less than 10... counter ++; //increase the counter variable by 1 System.out.println(counter); //print the counter variable } }


Does every statement have a counter example?

Only some statements have both examples and counter examples. A sufficiently clear and unambiguous statement would not have counter examples.


A loop control variable that is incremented a specific number of times is known as?

A loop control variable is widly known as a "counter".


What is a musical statement followed by a repeat of that statement and a counter statement called?

binary form


Is it counter-productive to change or update you personal mission statement?

s it counter-productive to change or update you personal mission statement?


What is counter loop?

A counted loop is a loop that executes the loop's statement a pre-determined number of times. The count represent the exit condition of the loop. A loop that is not counted is an infinite loop.


What is the correct way to add 1 to the count variable?

A counter variable is "incremented" (the step number, 1 in this case, is added to it) in any of the following four ways: $counter = $counter + 1;$counter += 1; //this is shorthand for the above $counter++; //postfix increment operator $counter = 0;echo $counter++;The output would be 0++$counter; //prefix increment operator $counter = 0; echo ++$counter;The output is 1


Can you display the contents of the counter variable in the body of a loop?

Yes.


What is a statement that shows conjecture is false?

A counter example is a statement that shows conjecture is false.


What is the if-then form of A counter example invalidates a statement?

You figure it out!


What does a counter withdrawal on your bank statement mean?

it is william


How do you display 1 to 100 using loops in c?

To display 1 to 100 using loops in C, you must first declare a variable. This variable will be the one to be printed it's increasing values. The variable must increment by 1 every time the loop loops. While the loop counter does not exceed 100, the loop will continue. Example code: int counter = 0; int value = 0; for (counter = 0; counter &lt;= 100; counter++) { value++; // (increment) increase value of variable "value" by 1 printf("%d\n", value); }