您在這裡

Computer Implementation

6 二月, 2015 - 17:28

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:

Example 13.4: flag controling 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.