Kenneth Leroy Busbee
Overview
Flags are commonly used to control or to indicate the intermediate state or outcome of particular operations.[1]
Discussion
For centuries flags have been used as a signal to let others know something about the group or individual that is displaying, flying or waving the flag. There are country flags and state flags. Ships at sea flew the flag of their country. Pirates flew the skull and crossbones. A yellow flag was used for quarantine, usually the plague. Even pirates stayed away. Today, some people might recognize the flag used by scuba divers. The Presidents of most countries have a flag. At a race car event, they use the checkered flag to indicate the race is over.
Computer programming uses the concept of a flag in the same way that physical flags are used. A flag is anything that signals some information to the person looking at it.
Computer Implementation
Any variable or constant that holds data can be used as a flag. You can think of the storage location as a flagpole. 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 commonly 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:
flag controlling an if then control structure
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.
Two Flags with the Same Meaning
Sometimes we will use an iteration control structure of do while to allow us to decide if we want to do the loop action again. A variable might be named “loop_response” with the user prompted for their answer of ‘y’ for yes or ‘n’ for no. Once the answer is retrieved from the keyboard and stored in our flag variable of “loop_response” the test expression to control the loop might be:
simple flag comparison
loop_response equals 'y'
This is fine but what if the user accidentally has on the caps lock. Then the response of ‘Y’ would not have the control structure loop and perform the action again. The solution lies in looking at the flag twice. Consider:
complex flag comparison
loop_response equals 'y' or loop_response equals 'Y'
We look to see if the flag is either a lower case y or an upper case Y by using a more complex Boolean expression with both relational and logical operators.
Multiple Flags in One Byte
Within assembly language programming and in many technical programs that control special devices; the use of a single byte to represent several flags is common. This is accomplished by having each one of the 8 bits that make up the byte represent a flag. Each bit has a value of either 1 or 0 and can represent true and false, on or off, yes or no, etc.
Key Terms
- flag
- A variable used to store information that will normally be used to control the program.