2024-03-29 10:49 AM
Hi,
I am writing C++ program on Visual Basic first, once it is compiled I will port it into STM32 Cube IDE environment. I wrote a simple for loop. When I run this code, I get very strange value for z out of the loop that is 1971940899.
You can see the terminal output as below.
when z is equal to 10, the loop will be broken out
5
when z is equal to 10, the loop will be broken out
6
when z is equal to 10, the loop will be broken out
7
when z is equal to 10, the loop will be broken out
8
when z is equal to 10, the loop will be broken out
9
when z is equal to 10, the loop will be broken out
1971940899
Could you please help me resolve ?
Thanks.
for(int z=5;z<15;z++)
{
cout<<"when z is equal to 10, the loop will be broken out\n";
if(z==10)
{
break;
}
cout<<z<<endl;
}
cout<<z<<endl;
Solved! Go to Solution.
2024-03-29 11:35 AM
Because it's out-of-scope, and may be a register or stack variable
{ // SCOPE A
int z;
for(z=5;z<15;z++)
{ // SCOPE B
cout<<"when z is equal to 10, the loop will be broken out\n";
if(z==10)
{
break;
}
cout<<z<<endl;
} // END SCOPE B
cout<<z<<endl;
} // END SCOPE A
2024-03-29 11:35 AM
Because it's out-of-scope, and may be a register or stack variable
{ // SCOPE A
int z;
for(z=5;z<15;z++)
{ // SCOPE B
cout<<"when z is equal to 10, the loop will be broken out\n";
if(z==10)
{
break;
}
cout<<z<<endl;
} // END SCOPE B
cout<<z<<endl;
} // END SCOPE A