Programming Fundamentals/Flag Concept

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

Overview[edit | edit source]

Checkered flags

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

Discussion[edit | edit source]

A flag reveals whether a data structure is in a possible state range and may indicate a bit field attribute, which a user needs special permission to access. A processor has many states that store multiple flag values that may help with post-processing. One example is arithmetic overflow. If the memory exceeds the limit like dividing by 0, the flag jump in to help out.

An example of the flag concept is a switch in which a parser is set at the beginning of a command line program. These switches are turned into flags which are then processed.

Computer Implementation =[edit | edit source]

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

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

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

flag
A variable used to store information that will normally be used to control the program.
Flag of Oceania

References[edit | edit source]