+ 1
What is the exact use of boolean variables in C++???
2 Antworten
+ 1
Before understand the use of Boolean variable,you need to understand what it is?
I would like to explain this through example:
boolean g=false;
boolean g=true;
boolean has only two values true and false.If you don't define boolean variable's value then by default it will be "false".
Now coming back to your question,What is the USE?
Well,imagine you want to create a loop and you want to control that loop from inside not from the condition part then it can be helpful.
Example: do{ int i=0;
if(i>6)
{
break;
}
i++;
}while(true); //Here i set the value "true",so no matter what it will be enter the loop(but within the loop i set my own condition which can break the loop when i>6.
Hope,you understand the concept.
P.S. It is not just for c++,it is applicable for every language. :)
0
These variables are used to store 0 or 1 which is nothing but true or false.
This can be used in if loop to execute when the condition is satisfied.
Example -
boolean someFunction(){
//code for function
return true/false; //depending on your need
}
In main function
boolean a = someFunction ();
if( a ){
//code
}