What is a flag variable?











up vote
6
down vote

favorite
3












Recently I just came across this term and I had no idea as to what purpose it serves?



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



I Googled it but there were not any specific examples related to it (in the context of JavaScript).










share|improve this question
























  • Asked and answered here: stackoverflow.com/questions/1626263/enum-flags-in-javascript
    – Bill Gregg
    Jul 1 '13 at 10:23










  • my first hit in google javascriptkit.com/javatutors/valid2.shtml
    – Vignesh Vino
    Jul 1 '13 at 10:24










  • look at that example,its pretty confusing!!!
    – JETHALAL
    Jul 1 '13 at 10:24















up vote
6
down vote

favorite
3












Recently I just came across this term and I had no idea as to what purpose it serves?



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



I Googled it but there were not any specific examples related to it (in the context of JavaScript).










share|improve this question
























  • Asked and answered here: stackoverflow.com/questions/1626263/enum-flags-in-javascript
    – Bill Gregg
    Jul 1 '13 at 10:23










  • my first hit in google javascriptkit.com/javatutors/valid2.shtml
    – Vignesh Vino
    Jul 1 '13 at 10:24










  • look at that example,its pretty confusing!!!
    – JETHALAL
    Jul 1 '13 at 10:24













up vote
6
down vote

favorite
3









up vote
6
down vote

favorite
3






3





Recently I just came across this term and I had no idea as to what purpose it serves?



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



I Googled it but there were not any specific examples related to it (in the context of JavaScript).










share|improve this question















Recently I just came across this term and I had no idea as to what purpose it serves?



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



I Googled it but there were not any specific examples related to it (in the context of JavaScript).







javascript variables flags






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 9 '14 at 19:46









Danny Beckett

13.5k1875119




13.5k1875119










asked Jul 1 '13 at 10:20









JETHALAL

8,129143665




8,129143665












  • Asked and answered here: stackoverflow.com/questions/1626263/enum-flags-in-javascript
    – Bill Gregg
    Jul 1 '13 at 10:23










  • my first hit in google javascriptkit.com/javatutors/valid2.shtml
    – Vignesh Vino
    Jul 1 '13 at 10:24










  • look at that example,its pretty confusing!!!
    – JETHALAL
    Jul 1 '13 at 10:24


















  • Asked and answered here: stackoverflow.com/questions/1626263/enum-flags-in-javascript
    – Bill Gregg
    Jul 1 '13 at 10:23










  • my first hit in google javascriptkit.com/javatutors/valid2.shtml
    – Vignesh Vino
    Jul 1 '13 at 10:24










  • look at that example,its pretty confusing!!!
    – JETHALAL
    Jul 1 '13 at 10:24
















Asked and answered here: stackoverflow.com/questions/1626263/enum-flags-in-javascript
– Bill Gregg
Jul 1 '13 at 10:23




Asked and answered here: stackoverflow.com/questions/1626263/enum-flags-in-javascript
– Bill Gregg
Jul 1 '13 at 10:23












my first hit in google javascriptkit.com/javatutors/valid2.shtml
– Vignesh Vino
Jul 1 '13 at 10:24




my first hit in google javascriptkit.com/javatutors/valid2.shtml
– Vignesh Vino
Jul 1 '13 at 10:24












look at that example,its pretty confusing!!!
– JETHALAL
Jul 1 '13 at 10:24




look at that example,its pretty confusing!!!
– JETHALAL
Jul 1 '13 at 10:24












3 Answers
3






active

oldest

votes

















up vote
11
down vote



accepted










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 var
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!");








share|improve this answer






























    up vote
    2
    down vote













    A flag is a variable used to have some value until some condition becomes true,then we change it to false and print the output (Initially flag considered as true)






    share|improve this answer

















    • 5




      Please expand and give a code example.
      – Dementic
      Jan 2 '14 at 12:13


















    up vote
    0
    down vote














    Flag variables are the same for all languages, whether it's RUBY or Python or JavaScript or C++ or even Microsoft® Small Basic™.




    A flag variable is usually given two values 0 and 1. So, its used as a Boolean variable where the result toggles between 0 (False) and 1 (True) or as used by the programmer. Some prefer flag=1 and change it to flag=0 in the program to perform an action.






    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














       

      draft saved


      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f17402125%2fwhat-is-a-flag-variable%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      11
      down vote



      accepted










      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 var
      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!");








      share|improve this answer



























        up vote
        11
        down vote



        accepted










        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 var
        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!");








        share|improve this answer

























          up vote
          11
          down vote



          accepted







          up vote
          11
          down vote



          accepted






          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 var
          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!");








          share|improve this answer














          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 var
          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!");








          // errors is the flag var
          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!");





          // errors is the flag var
          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!");






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 11 at 0:29

























          answered Jul 1 '13 at 10:23









          Danny Beckett

          13.5k1875119




          13.5k1875119
























              up vote
              2
              down vote













              A flag is a variable used to have some value until some condition becomes true,then we change it to false and print the output (Initially flag considered as true)






              share|improve this answer

















              • 5




                Please expand and give a code example.
                – Dementic
                Jan 2 '14 at 12:13















              up vote
              2
              down vote













              A flag is a variable used to have some value until some condition becomes true,then we change it to false and print the output (Initially flag considered as true)






              share|improve this answer

















              • 5




                Please expand and give a code example.
                – Dementic
                Jan 2 '14 at 12:13













              up vote
              2
              down vote










              up vote
              2
              down vote









              A flag is a variable used to have some value until some condition becomes true,then we change it to false and print the output (Initially flag considered as true)






              share|improve this answer












              A flag is a variable used to have some value until some condition becomes true,then we change it to false and print the output (Initially flag considered as true)







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Jan 2 '14 at 11:55









              sajin

              211




              211








              • 5




                Please expand and give a code example.
                – Dementic
                Jan 2 '14 at 12:13














              • 5




                Please expand and give a code example.
                – Dementic
                Jan 2 '14 at 12:13








              5




              5




              Please expand and give a code example.
              – Dementic
              Jan 2 '14 at 12:13




              Please expand and give a code example.
              – Dementic
              Jan 2 '14 at 12:13










              up vote
              0
              down vote














              Flag variables are the same for all languages, whether it's RUBY or Python or JavaScript or C++ or even Microsoft® Small Basic™.




              A flag variable is usually given two values 0 and 1. So, its used as a Boolean variable where the result toggles between 0 (False) and 1 (True) or as used by the programmer. Some prefer flag=1 and change it to flag=0 in the program to perform an action.






              share|improve this answer

























                up vote
                0
                down vote














                Flag variables are the same for all languages, whether it's RUBY or Python or JavaScript or C++ or even Microsoft® Small Basic™.




                A flag variable is usually given two values 0 and 1. So, its used as a Boolean variable where the result toggles between 0 (False) and 1 (True) or as used by the programmer. Some prefer flag=1 and change it to flag=0 in the program to perform an action.






                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote










                  Flag variables are the same for all languages, whether it's RUBY or Python or JavaScript or C++ or even Microsoft® Small Basic™.




                  A flag variable is usually given two values 0 and 1. So, its used as a Boolean variable where the result toggles between 0 (False) and 1 (True) or as used by the programmer. Some prefer flag=1 and change it to flag=0 in the program to perform an action.






                  share|improve this answer













                  Flag variables are the same for all languages, whether it's RUBY or Python or JavaScript or C++ or even Microsoft® Small Basic™.




                  A flag variable is usually given two values 0 and 1. So, its used as a Boolean variable where the result toggles between 0 (False) and 1 (True) or as used by the programmer. Some prefer flag=1 and change it to flag=0 in the program to perform an action.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 30 at 19:43









                  Aryan Beezadhur

                  817




                  817






























                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f17402125%2fwhat-is-a-flag-variable%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Full-time equivalent

                      Bicuculline

                      さくらももこ