Showing posts with label if. Show all posts
Showing posts with label if. 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, June 3, 2012

if then else statements

DOWNLOAD THE CODE: UsingIfThenElse.zip

<<< back to if statements

In the previous post on if statements, we saw how we could use an if statement to detect if a condition was true and then execute statements accordingly. An if-then-else statement allows us to branch off in multiple directions.

The example below determines if the user has entered an age that is equal to or greater than 18. If this condition evaluates to true, the user receives the message "You are 18 or over". If the age is anything else, the user receives the message "You are less than 18".



Here's the output:




In the next example, we have multiple if's to determine the entered age:


Note that each if section is contained within curly braces.

Here is the output:





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

if statements


Is used for implementing selection algorithms. Executes a certain code section only if a condition is true.

Format:

if (expression) {
   'Statements.
}

The expression to be evaluated is placed within parenthesis. Curly braces are used to open a block, immediately after the expression. If the result of evaluating the expression is true, all statements within the curly braces will execute. If the expression returns false, the statements within the curly braces will be skipped.

An expression is a combination of literal values or variables, and operators.

In the example below, the user is asked to enter a number. The if statement determines if the number is less than (less than operator < ) 100 and prints a message to screen, if it is. It then goes on to ask the user to enter their name. The if statement determines if the name has a length (String length) of more than 5 characters and prints a message to screen if it does.


Note that the if statement opens a code block with curly braces.

To recap, if the user enters a number that is less than 100, they will get the message "The number you entered was less than 100" and if the user enters a name that has a length greater than 5, they will get a message saying "The length of your name is longer than 5 characters long".

Here is the output:




Evaluating multiple expressions in an if statement

We can use the logical AND operator && to evaluate multiple expressions.


In this example, if the user enters a number that is greater than 50 AND less than 100, they will get the message "The number you entered was greater than 50 AND less than 100" and if the user enters a name that has a length greater than 5 AND less than 20, they will get a message saying "The length of your name is longer than 5 characters long AND shorter than 20".





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




Here's a cat...







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 )