What is boolean?

what is boolean and how do they work in processing

1 Like

a variable can be of type

boolean diag = true; // false; // global setting if need diagnostic printing to console

and later in the program anywhere can use

if ( diag ) println(" functionname: variablename: "+variablename);

and when all your code is tested and no diagnostic needed, set it to false


also the result of any comparison will create a boolean value
( a == b )
is true or false.

1 Like

can you talk about it in a more simpler way thanks

hmm, or you tell us in what context you ask that question,
so we better understand what level you are on and how we should respond?

talking in general in processing

A boolean is a type of primitive variable.

It can either be set to true or false. This is pretty much a binary variable (yes/no, on/off, true/false).

In other programming languagues it could also be represented by 0 and 1, but in Java (which Processing is built with) a boolean is represented only by true and false (So you can‘t set it in another way, unlike how you could do with char for example).

Also, booleans are pretty much the only Form of conditionals (if statements, switch statements and even for loops all are based on boolean values) :

if (isThisTrue) { //if the variable isThisTrue is true, the Code in the statement is executed. 
//Code to execute if condition is true

} else if (!isThisTrue) { // using ! negates the boolean value (!true == false, !false == true). 

}

for (int i = 0; i < 100; i++) { //the i < 100 part will be true while i < 100 and thus will repeat until then. 
}
3 Likes

thanks alot that is simpler way to understand so what is string? and how is so different to boolean?

String is any Type of Text. The difference is pretty big.

boolean b = true/false; //this is all that boolean could ever be (true or false)
String s = „You can write any Type of Text Here“ // This is basically what a String is (String is a primitive too, at least in Java)

And a String is basically nothing Else then a char array :

char[] ca = [‚H‘, ‚e‘, ‚l‘, ‚l‘, ‚o‘];
String s = „Hello“;
//A String is basically made up of a char[]. So
s.getCharAt(3); // would return L (using capital L to make clear that it‘s L)
ca[3]; // is also L
1 Like

so its same as boolean

Yes. “boolean” is a also a string, because it is between quotation marks.
Another example:.

boolean bln;
String txt = "I am a troll";
if (txt.length() == 12) {
  bln = true;
} else {
  bln = false;
}
println(txt+" that is "+bln);
2 Likes
String True = "true";
boolean False = true;
if ( boolean(True) == False ) println("@noel is wrong");
1 Like

Interesting,learning while trolling.

int True = 5; 

try {
  println(boolean(True/0));
}
catch(Exception e) {
  println("Not a boolean afterall");
}
2 Likes

What do you mean?

  • isThisTrue is a boolean

  • String is not

See reference for all of this

1 Like

How did you come to the conclusion that it‘s the same?

There‘s a big difference, as i said.
Boolean can ONLY be true or false. And true or false are not Strings, so this doesn‘t work :

String a = „true“;
boolean b = a; // this does not work. 
boolean b = boolean(a); //apparently you can do this, but this is still not a String being read as a boolean!
// It can be basically seen as : 
boolean boolean(String s) {
   if (s.equals(„true“)) {
      return true;
   } else if (s.equals(„false“)) {
      return false;
   } else {
      return null;
   }
}
1 Like