Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
138
rated 0 times [  141] [ 3]  / answers: 1 / hits: 87590  / 11 Years ago, mon, july 1, 2013, 12:00:00

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).


More From » variables

 Answers
91

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!);
}




[#77285] Saturday, June 29, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
andreasn

Total Points: 238
Total Questions: 107
Total Answers: 111

Location: Ghana
Member since Sun, Mar 27, 2022
2 Years ago
;