BSalunke
BSalunke

Reputation: 11727

How to change the variable value declared before for loop and tried to change value in for loop in C++?

The following is my code:

bool rValue = false;
for(int i=0; i < 10 ; i++)
{
  some code...
  ...
  ...
  rValue  = true

}

std::cout << "rValue is " << rValue << std::endl;

Output of the above code is rValue is false. Why? Why didn't the value of the rValue variable change?

Upvotes: 0

Views: 243

Answers (1)

Neel Basu
Neel Basu

Reputation: 12904

I suspect somewhere inside your loop you have declaration bool rValue. So its changing the rValue under loop scope. not the parent scope one.

and also If you quit the loop before reaching the inner assignment statement it will not effect. You can check that in a debugger or place a std::cout before assignment

Upvotes: 3

Related Questions