Is changing the currentLoc variable with an IF statement a bad way to terminate this loop below? I'm told:

"...something in the loop body must cause the expression to become false at some time. One of the things you do not do ever, is write an if statement to change the loop control variable to make the condition false."

Any info would be appreciated. And I apologize beforehand is this format is wrong. I've read the rules, but I *think* this is my first post here.

while (currentLoc != 1 && currentLoc != 8) // 
{
	((rand() % 100) < chanceNum ) ? currentLoc-- : currentLoc++;
		if ( currentLoc == 1)
			pubCount++;
		else if (currentLoc == 8)
			homeCount++;
			blocksWalked++; 
}

>> "...something in the loop body must cause the expression to become false at some time. One of the things you do not do ever, is write an if statement to change the loop control variable to make the condition false."


Not sure who you're quoting, but I don't see why anything wrong with writing an if statement to change the loop control variable to make the condition false. I do it all the time...

bool userTerminate = false;
int num;
while(!userTerminate)
{
   cout << "Enter a number, 0 to terminate : ";
   cin >> num;
   if(num == 0)
   {
      userTerminate = true;
   }
   else
   {
      // do whatever I'm going to do
   }
}

What's wrong with that?

It's hard to comment on your code without seeing more of it, but I for one don't see anything in it that jumps out and says "Don't do this."

commented: Helped me much regarding a c++ concern. +0

In this case it's perfectly fine. You don't really have what's called a loop counter. You have variables that are tested in the while statement.

A "loop control variable" is used in a for statement. That's where you should not change the value to exit the loop.

>>A "loop control variable" is used in a for statement. That's where you should not change the value to exit the loop.


To clarify, you're saying...

for(int i = 0; i < 10; i++)
{
    // code
    i = /* whatever */  // <--- bad
    // code
}

Correct?

>>A "loop control variable" is used in a for statement. That's where you should not change the value to exit the loop.


To clarify, you're saying...

for(int i = 0; i < 10; i++)
{
    // code
    i = /* whatever */  // <--- bad
    // code
}

Correct?

IMO, yes. Using the OP's question directly, the loop would be:

for(int i = 0; i < 10; i++)
{
    // code to calculate val
    if (val == 15) i = 11;
    // code Keep going
}

is better served by

for(int i = 0; i < 10; i++)
{
    // code to calculate val
    if (val == 15) break;
    // code Keep going
}

Thank you for the answers. I didn't add more code because the program runs perfectly. The while loop is inside a for loop, but the for loop terminates properly. I forgot to add more to the quote I pasted above, the instructor mentions this about if statements that change control variables.

"That is considered a go-to, and a go-to is a grievious error "

After seeing WaltP's first code snippet, I can see how doing so would behave as a goto statement.

Thank you all for the info. I truly appreciate it. I will mark this as Solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.