Which variables can be referred to as flag variables?
Every now and then I come across the term flag
in the context of variables and I have开发者_运维知识库 my own understanding of it, but still, I'd like to see a clear definition.
This is usually a private variable that is used by other members (properties, methods) as a convenient way to track and determine state for choosing among more than one course of action.
These are often, but not always, boolean or enumeration values.
For example, you may have a boolean "flag" called IsDataLoaded
, and a method called ShowResults()
. In the body of the ShowResults()
method, you would check the value of the IsDataLoaded
flag, and if it is true
, just show the results. If false
, you'd first call a LoadData()
method. Once the data has been loaded, the LoadData()
method would change the flag from false
to true
.
Using enumerations and bitmasks, "flags" can be a bunch of settings or options contained in a single value. For example, if you define the following (C#):
enum foo
{
CaseSensitive = 1,
Use24HourTime = 2,
IgnoreRegistrySetting = 4,
LoadFoosAndBarsSeparately = 8
}
You'd be able to specify the settings with a simple value like 13
, or specify the "flags" using CaseSensitive | IgnoreRegistrySetting | LoadFoosAndBarsSeparately
.
In .NET, there is a System.FlagsAttribute
attribute:
System.FlagsAttribute: Indicates that an enumeration can be treated as a bit field; that is, a set of flags.
Often but not necessarily a Boolean, a flag variable is used to determine which of a finite, discrete group of sets a particular piece of data is a member of.
In normal language you might flag a piece of data to say it's a cat. Or flag a job application to say that it's incomplete.
In C and C-derived languages, a flag variable is often an enumerated type.
On CPUs you usually get single bit status flags that are set by earlier operations. E.g. you might add two numbers together, then afterwards the 'carry' flag will be set or unset depending on whether a bit should have been carried from the topmost bit.
A flag variable, in its simplest form, is a variable you define to have one value until some condition is true, in which case you change the variable's value. It is a variable you can use to control the flow of a function or statement, allowing you to check for certain conditions while your function progresses.
Computer Implementation
Any variable or constant that holds data can be used as a flag. You can think of the storage location as a flag pole. The value stored within the variable conveys some meaning and you can think of it as being the flag.
An example might be a variable named: gender which is of the character data type. The two values normally stored in the variable are: 'F' and 'M' meaning female and male. Then, somewhere within a program we might look at the variable to make a decision:
if gender equals 'F'
display "Are you pregnant?"
get answer from user store in pregnant variable
Looking at the flag implies comparing the value in the variable to another value (a constant or the value in another variable) using a relational operator (in our above example: equality). Control structures are "controlled" by using a test expression which is usually a Boolean expression. Thus, the flag concept of "looking" at the value in the variable and comparing it to another value is fundamental to understanding how all control structures work.
This word is often used to describe bit fields: i.e. an integer variable of some sort, for which each bit has a particular meaning. The Windows platform API uses them extensively.
精彩评论