Hello everyone I am learning about the declaration of variables in C++.
Now please tell me my mistake here. Why is it bad to declare your variable twice?
int fly = 0;
for(int fly = 0; fly < 10; fly++) {
cout << "This is a kite flying" << fly << endl:
}
These are two seperate variable because they are declared in a different scope. The scope of a variable is the "area" of code in which it's visible.
As a simple rule of thumb, any place where curly brackets are, or could be placed, is a new scope. The fly
inside the for loop, overrides the other fly
variable. If it wasn't declared, or declared under a different name. The original variable would still be accessible.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With