Login Page - Create Account

Support Board


Date/Time: Fri, 29 Nov 2024 02:40:58 +0000



Post From: canceled stop order continues to have status SCT_OSC_OPEN

[2023-03-09 18:03:41]
ondafringe - Posts: 286
Let's see if I can explain this in language that can be understood. :)

Consider this code:


if (whatever)
{
int test = 10;
//"test" variable was created here, and is said to be "in scope"
}

//"test" variable goes out of scope and no longer exists

The variable "test" was created within the if-construct and is only alive and readable within that construct. Once execution leaves that construct, the "test" variable is automatically destroyed. So within that construct, the variable is said to be "in scope."

================

Now consider this code:


int test = 0;
//"test" variable was created here and is said to be "in scope" for this construct and any sub-constructs contained within the broader construct

if (whatever)
{
test = 10;
//test variable is still in scope and can be read here
}

//test variable is still in scope, still readable, and will retain its value until explicitly changed or until, it goes out of scope

The "test" variable was created outside of the if-construct, so it now has a broader scope and can be read within that broader scope and also within any sub-scope, such as the if-construct, as long as the sub-construct is contained within the broader construct.

============

Now consider this code:


if (whatever)
{
int test = 0;
//"test" variable was created within the first if-construct, and is said to be "in scope" for that and any sub-constructs contained within the first if-construct.

if (whateverelse)
{
test = 10;
//test variable is still in scope and can be read here
}

//test variable is still in scope and can be read here
}

//test variable goes out of scope and no longer exists

And on and on it goes, where it ends, only the programmer knows.

Hopefully, I got all that right. :)
Date Time Of Last Edit: 2023-03-09 18:16:40