0
Can someone explain me use of boolean
better to be with an example code
3 Answers
+ 1
so a boolean is a type which can return True or False, like conditions. so for example, there is a loop of your program and you would like it to stop when somethibg happens, so you can do it like so:
bool IsDone = false;
while(!IsDone)
{
//do somerhing
if(some condition)
IsDone = true;
}
You can also use booleans as statementa in if/ else if when your conditions are getting out of your screen, so mayke something like that:
bool statement1 = (something != variable);
bool statement2 = (var < someint);
if(statement1 && statement2)
+ 1
The boolean data type is often used for making decisions. A boolean variable can only have two different values: true and false. There are different ways of comparing values. We can test if a number is greater or smaller than another number, if they are equal, if they are different. In each case a value of true or false is returned. We can print this value, or store it in a variable of type boolean.
The same way numbers can be added, substracted and multiplied, boolean values (true and false) can be combined by using AND, OR and NOT. Imagine we have two boolean values called A and B. Each can be either true or false. A AND B is true only if both variables are true. A OR B is true if any of the two variables is true. When writing programs you don't write AND, OR or NOT. You use &&, || and ! instead. For example if(x > 10 && x < 20) { print("x is a number between 10 and 20"); }
/******* example ********/
boolean friendHasMoney;
boolean iHaveMoney;
friendHasMoney = false;
iHaveMoney = false;
if(iHaveMoney || friendHasMoney) {
print("We will buy ice cream");
} else {
print("We can't buy ice cream");
}
0
e = 0
print('0') if not e else print('true')