Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a flag variable?

Recently I came across flag variables, but I have no idea what they do.

I am little unsure about when to use a flag variable and how to go about it.

I Googled it but there weren't any specific examples related to my context (of JavaScript).

like image 786
HIRA THAKUR Avatar asked Jul 01 '13 10:07

HIRA THAKUR


People also ask

What is a flag in programming?

In programming, a flag is a predefined bit or bit sequence that holds a binary value. Typically, a program uses a flag to remember something or to leave a sign for another program.

What is a flag in Python?

flags defines a distributed command line system, replacing systems like getopt() , optparse , and manual argument processing. Rather than an application having to define all flags in or near main() , each Python module defines flags that are useful to it.

What does it mean to flag data?

Data flags are commonly used to signify either bad data or missing data in a dataset. In MATLAB the official flag value is NaN (not a number). More typically a number larger than any possible value of the variables in the dataset is used such as 1.0e32, or 99999.

What is a Boolean flag variable?

 A Flag is a boolean variable that signals when some condition exists in a program.  When a flag is set to true, it means some condition exists  When a flag is set to false, it means some condition does not exist. if(score > 95) highscore = true;  Here, highscore is a flag indicating that the score is above 95.


1 Answers

Flag Variables Defined and Uses says:

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.

As an example:

// errors is the flag variable
var errors = 0;

for(var i = 0; i < 10; i++) {
  if(i == 6) {  // Your error condition
    errors++;
  }
}

if(errors) {  // Is the flag "up"? (i.e. > 0)
  alert("There was a problem!");
}
like image 198
Danny Beckett Avatar answered Oct 24 '22 09:10

Danny Beckett