Showing posts with label Selection. Show all posts
Showing posts with label Selection. Show all posts

Thursday, January 17, 2013

! operator (NOT)



The exclamation character is the NOT operator !
The NOT operator is used to evaluate an expression to true or false.


Here's the NOT operator using a simple if statement format:

if (!(condition1)) {
   doSomething();
}

Here's the NOT operator using a if-then-else statement format:



if (!(condition1)) {
   doSomething();
}

else {
   doSomethingElse();
}


The method doSomething() will only execute if the condition in the if statement evaluates to false.

Let's take a look at another, more concrete example:


if (!(respawnRemaining >= 1)) {
   gameOver();
}


The method gameOver() will only execute if respawnRemaining is NOT greater than or equal to 1.

In the following example, we use an if-then-else statement:



if (!(respawnRemaining >= 1)) {
   gameOver();
}

else {
   respawn();
}

If respawnRemaining is NOT greater than or equal to 1, the gameOver method will execute. If it is greater than or equals to 1, the respawn method will execute.



Also see
&& Operator (AND)
|| Operator (OR)


Tuesday, January 8, 2013

&& operator (AND)


A double ampersand is the logical AND operator &&

The AND operator is used to evaluate multiple expressions during decision making with if statements.


Here's the AND operator using a simple if statement format:

if (condition1 && condition2) {
   doSomething();
}

Here's the AND operator using a if-then-else statement format:


if (condition1 && condition2) {
   doSomething();
}
else {
   doSomethingElse();
}


The method doSomething() will only execute if both of the conditions in the if statement evaluate to true.

Let's take a look at another, more concrete example:


if (maxDamage > 100 && ammoCount == 0) {
   gameOver();
}


The method gameOver() will only execute if maxDamage is greater than 100 AND ammoCount is equal to 0. If either condition does not evaluate to true, the gameOver() method will not execute.

In the following example, we use an if-then-else statement:


if (maxDamage > 100 && ammoCount == 0) {
   gameOver();
}
else {
   continue();
}


If both conditions are true, the gameOver() method will execute. If both of the conditions are false, the continue() method will execute.

Also see
|| operator (OR)
! Operator (NOT)

|| operator (OR)



A double pipe is the logical OR operator ||

The OR operator is used to evaluate multiple expressions during decision making with if statements.


Here's the OR operator using a simple if statement format:

if (condition1 || condition2) {
   doSomething();
}

Here's the OR operator using a if-then-else statement format:


if (condition1 || condition2) {
   doSomething();
}
else {
   doSomethingElse();
}


The method doSomething() will only execute if one or both of the conditions in the if statement evaluate to true.

Let's take a look at another, more concrete example:


if (maxDamage > 100 || ammoCount == 0) {
   gameOver();
}


The method gameOver() will only execute if either maxDamage is greater than 100 (and or) OR ammoCount is equal to 0. If either condition does not evaluate to true, the gameOver() method will not execute.

In the following example, we use an if-then-else statement:


if (maxDamage > 100 || ammoCount == 0) {
   gameOver();
}
else {
   continue();
}

If either or both of the conditions are true, the gameOver() method will execute. If both of the conditions are false, the continue() method will execute.

Also see
&& Operator (AND)
! Operator (NOT)


Sunday, March 18, 2012

if-then-else statements

"The if-then and if-then-else Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)" ( http://bit.ly/zflbUx )