Programming Fundamentals/Flag Concept

From Wikibooks, open books for an open world
Jump to navigation Jump to search


Checkered flags

Flags are commonly used to control or to indicate the intermediate state or outcome of particular operations.[1]


Physical flags are used to give information about groups and individuals. There are many kinds of physical flags including, state and country flags, checkered flags for races, rainbow flags for lgbt friendly locations, and even red flags with a white stripe used by scuba divers to indicate they're underwater.

Computer programming uses the concept of a flag in the same way that physical flags are used.

Computer Implementation[edit]

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: marital_status which is of the character data type. The two values commonly stored in the variable are: ‘S’ and ‘M’, meaning single and married. Then, somewhere within a program we might look at the variable to make a decision:

flag controlling an if then control structure

if marital_status equals 'M'
    display "Are you filing your taxes jointly?"
    get answer from user store in filing_jointly 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[edit]

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[edit]

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[edit]

A variable used to store information that will normally be used to control the program.