Jump to content

Trouble understanding the logic of this statement.

- - - - -

  • Please log in to reply
6 replies to this topic

#1
eindoofus

eindoofus

    Newbie

  • Members
  • Pip
  • 3 posts
Hello everyone. I'm newbie and new to this forum but I was hoping you guys would still be willing to help me out. I just came across this statement and I can't wrap my head around it:

turnLeft = (turnLeft == true ? false : true);

It was part of this code:

public void goLeftAndRight()

	{

		boolean turnLeft = true;

 

		while (true)

		{

			placeBeeperAndMove();

			if (turnLeft) 

				turnLeft();

			else 

				turnRight();

			if (frontIsBlocked()) break;

			move();

			if (turnLeft)

				turnLeft();

			else

				turnRight();

			[B]turnLeft = (turnLeft == true ? false : true);[/B]

		}

	}

I have no idea how to read the logic of that statement. Is true ? false : true an if, then statement? In which case does it mean:

if (true) {

   false

} else {

   true

}

Or is it read as:

if (turnLeft == true) {

   false

} else {

   true

}

In which case isn't it better to write it as: turnLeft = ((turnLeft == true) ? false : true);

Is it basically a toggle? I don't get it.

#2
existentialist

existentialist

    Newbie

  • Members
  • PipPip
  • 14 posts
Ternary operation - Wikipedia, the free encyclopedia

it's shorthand for
if (condition == true) assign something;
else assign somethingElse;
but i don't think it's necessary here, since if turnLeft is true, it's just reassigned true
and if false just reassigned false.

actually, it i read it too quick, it assigns true if it was false
and false if it was true.

your first code
turnLeft = (turnLeft == true ? false : true);
and your last one
turnLeft = ((turnLeft == true) ? false : true);
is the same, except for the extra parentheses, so it's better if it helps you understand it, i guess
i like to put extra spaces between paren's
turnLeft = ( (turnLeft == true) ? false : true);
or like this
System.out.println(something.toString() );

Edited by Alexander, 07 November 2010 - 12:43 PM.
Merge


#3
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
A simple way to understand it is..

turnLeft = (turnLeft == true ? false : true);

[FONT=Courier New]turnLeft = 
   does turnLeft equal true?
       assign false;
   else:
       assign true;[/FONT]
Imagine you are asking it a question.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#4
Vladimir

Vladimir

    Learning Programmer

  • Members
  • PipPipPip
  • 79 posts
My try:

turnLeft = (turnLeft == true ? false : true);

is shorthand for:

if (turnLeft == true) {

    turnLeft = false;

} else {

    turnLeft = true;

}

and it can be replaced with:

turnLeft = !turnLeft;



#5
existentialist

existentialist

    Newbie

  • Members
  • PipPip
  • 14 posts

Quote

turnLeft = !turnLeft
nice

#6
eindoofus

eindoofus

    Newbie

  • Members
  • Pip
  • 3 posts
Thanks guys. I especially like:

turnLeft = !turnLeft


#7
so1i

so1i

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 312 posts

Quote

turnLeft = !turnLeft;

That's a nice way of writing it.
My Company - My Homepage - My Twitter - My Google+ - My LinkedIn

"Things don’t have to change the world to be important.” - Steve Jobs




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users