We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › if boolean=true : boolean=false (+reverse)
Page Index Toggle Pages: 1
if boolean=true : boolean=false (+reverse) (Read 1508 times)
if boolean=true : boolean=false (+reverse)
Oct 21st, 2009, 6:26am
 
I have a problem...
I have a boolean and if the boolean (b1) is true, I want to set the boolean=false
And if the boolean is false, I want to set the boolean=true
I've tried:
Code:

void mousePressed() {
if (b1=true) {
    b1=false;
  }
  else
    b1=true;
}

But this doesn't work...
Has anybody an idea?
Please Help! Undecided
Re: if boolean=true : boolean=false (+reverse)
Reply #1 - Oct 21st, 2009, 6:28am
 
http://processing.org/reference/equality.html

use == not = in conditions
Re: if boolean=true : boolean=false (+reverse)
Reply #2 - Oct 21st, 2009, 6:32am
 
Thank you for your quick reply, but
Syntax error on token "==", invalid AssignmentOperator
== ist only if you have an number, isn't it?

EDIT:
Oh... Sorry... I saw:
Parameters    value1     int, float, char, byte, boolean
but how does it work?
An error was occured...
Re: if boolean=true : boolean=false (+reverse)
Reply #3 - Oct 21st, 2009, 6:49am
 
He meant "in conditions".
if (b1 == true)
but it is simpler (and in some cases more readable for me) to just write:
if (b1)
although the simplest way to get want you want is to write:
Code:
void mousePressed() {
b1 = ! b1;
}

The exclamation sign here means "not", ie. b1 becomes the inverse of what it was.
Re: if boolean=true : boolean=false (+reverse)
Reply #4 - Oct 21st, 2009, 9:11am
 
Ok...Thank you
I've tried
Code:

b1 != b1

but this doesn't work...
now I've tried your code, but it doesn't work anyway!  Cry
I've tried it in a new sketch and that works...
I don't know why it doesn't work with my other program? I've searched for the name from the boolean, but there's nothing, that can change the boolean = true everytime?
there are only
Code:

boolean b1=true; //before setup()
//-----
if (b1=true) { // in draw()
image1(...);
}
if (b1=false) { // in draw
image2(...);
}
//----- and your code
b1 =!b1; // in mousePressed

thats it!
After changing the boolean, I have
Code:

println(b1);

that I can see, if the boolean had changed and it hadn't... Huh
whats now?
Re: if boolean=true : boolean=false (+reverse)
Reply #5 - Oct 21st, 2009, 9:48am
 
you realised that he wrote b1 = !b1 and you wrote b1 != b1 ?
Re: if boolean=true : boolean=false (+reverse)
Reply #6 - Oct 21st, 2009, 10:03am
 
Yes...
I've tried != before I asked in this forum...
and then I tried the code from PhiLho with =!
Tthank you anyway...
Re: if boolean=true : boolean=false (+reverse)
Reply #7 - Oct 21st, 2009, 10:11am
 
Again: use == in conditionals.
Don't write:
if (b1=true) nor if (b1=false)
but
if (b1 == true) or if (b1 == false)
or just
if (b1) or if (!b1)

Java inherited from the same issue than C, allowing assignments in conditionals. The scope is more limited because unlike in C, zero and not zero are not boolean, so you cannot do by error if (a = 1) but you just hit the case were the compiler won't complain.
Re: if boolean=true : boolean=false (+reverse)
Reply #8 - Oct 21st, 2009, 10:35am
 
Aah...ok...i got it...thank you very much...
Re: if boolean=true : boolean=false (+reverse)
Reply #9 - Oct 22nd, 2009, 7:55am
 
Meuchelfix77 wrote on Oct 21st, 2009, 10:03am:
Yes...
I've tried != before I asked in this forum...
and then I tried the code from PhiLho with =!
Tthank you anyway...


b1 != b1; (boolean expression: b1 not equal to b1, which will always return false, or maybe an error if b1 hasn't been initialised) is not the same as b1 = !b1 (b1 is assigned the logical negation of b1)

Think:

Code:
newvalue = (!oldvalue) 



Also:

Code:
if (b1) // meaning "if (b1) evaluates as true"
 b1 = false;
else
 b1 = true;

And, just to confuse you...

Code:
b1 = b1 ? false : true; 



-spxl  Cheesy
Page Index Toggle Pages: 1