Sololearn: Learn to Code
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5
Any variable used to see the state of some operation during execution can be called a flag. It is used in wide range of applications, such as marking some operation as done in a loop (Suppose you need to do some extra calculation only when some condition is true, for a series of operations, based on the previous result of the operations), or marking the state of some operation (like a button, was it pressed or not?). A simple example in C++: To check for prime : int no; cin>>no; bool flag=0; //My flag, to mark if the number is divisible by something or not. for(int i=0;i<sqrt(no);i++) { if(n%i==0) flag = 1; //Something divided my number. //The flag is set to one as a result. else continue; // Continue to next iteration. } if(flag) //If the flag was true? cout<<no<<"is composite"<<endl; else cout<<no<<"is prime"<<endl; Thats it. A flag just helps mark a state. An advanced use will be in a button, where you need to roll some changes on screen only when the button is pressed in a separate message box, dialog box, or window.
15th Oct 2017, 1:54 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar