+ 2
I don't understand booleans very well. Can someone help me?
4 Respostas
+ 5
Booleans are a way programs talk to us.
We usually use booleans on IF statements:
if(true) then print "hello"
So if the Boolean is true then the program should print "hello"
In Java, C#, php and C++ the Boolean oporators are as follows:
Let's say 'number' equals 4.
if(number == 4)
== is an equals oporator
if(number <= 4)
<= Smaller-Equal oporator
So if number is smaller than 4 or = to for then it's true
if(number >= 4)
>= Bigger-Equal oporator
So if number is bigger or equals to 4 then it's true
if(number < 4)
< smaller oporator
So if number is smaller than 4 then it's true
if(number > 4)
> bigger oporator
So if number is bigger than 4 then it's true
if(number != 4)
!= Not Equal oporator
So the ! inverts the value that it would be without the !
ex: if(!false)
if not false (which makes it true) then execute code.
I hope this clears things up a bit
+ 3
Boolean type variables has two values. True or false. True equals to 1. False equals to 0. For example:
boolean_1=true
boolean_2=false
print(boolean_1) //it will give to you "true", shortly it will give 1.
print(boolean_2) ////it will give to you "false", shortly it will give 0.
When we come to compare to bool variables, we look at the same numbers.For example:
//I take variable of example of above.
print(boolean_1==boolean_2) //It will give 0 or false to us. Because we checked that 1==0 ?? As everybody can know that they are different from each other.
print(boolean_1!=boolean_2) //Now we are checking that Is 1 not equal to 0? And we know that yes they are not equal to each other. It means we will get true or 1.
When we came to variable compares , there are not 0 or 1 any more.For example:
variable_1="ship"
print(variable_1=="ship") //We are asking to computer Is variable_1 equal to "ship", and the computer is saying yes they are equal to each other. Because variable_1 has "ship" value.
I don't know can I tell to you what is the boolean. But probably you will learn something if you will try to understand.
+ 2
thanks a lot
+ 1
A boolean is a type with only two common value : true, false.
General concept :
I'am a human. True because I'm speaking to you.
In coding :
if (I'm speaking to you):
I'm human.
The if statement evaluate the condition with the following operators : < > == != <= >= or with predeterminate functions to return a boolean.
1<2 -> true
1>2 -> false
Beware of type specifications like String in java wich can't compare two value with == because of character definition. Use functions like equals().