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...







The concatenation operator

DOWNLOAD THE CODE: ConcatPkg

The concatenation operator is the addition symbol +.

Concatenation is the act of joining multiple values together to form a single String.


Here's the output:


Note! The addition symbol is also used as the addition operator! If the addition symbol is placed between 2 numbers, it will treat the expression as an addition expression!

Saturday, June 2, 2012

Escape Sequences

From "Characters (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)" ( http://bit.ly/LUgCyw )


Switch

CODE DOWNLOAD: UsingSwitch.zip


A Switch can be used to evaluate a number of numeric types such as byte, short, char & int.

Let's say you want to check the value of a single variable and based on this value, one of many things may happen.

In the example below, the user enters a number. This number is then checked by the switch. The first case to match, executes a statement - in this case, printing a message to let the user know what the entered value was.


If the user entered a value not listed in the cases, the default section executes. It is good practice to always include a default case in your switch.

Notice that the final statement in each case is break. This causes the switch to stop checking down the list.

We can also execute a common statement for multiple cases, as in the example below:


Of course, we could use a number of if then else statements to achieve the same result, however, as we are only checking the value of a single variable, the switch is a more elegant and readable solution (subjective).

Here's a cat:




"The switch Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)" ( http://bit.ly/y2TVYE )

int data type



An int is a numeric data type

An int is a whole number between -2,147,483,648 and 2,147,484,647 (a pretty big number). The number cannot contain a floating point (no decimal).

Where an int has not been assigned a value, by default it's value is 0.


The format for declaring an int variable is:

   Type identifier;

Where the Type is int and the identifier is maxDamage:


   int maxDamage;

...and where a value of 100 has been assigned:

   int maxDamage = 100;


You can declare an int in one statement and assign it a value in another statement:

   int maxDamage;
   // other statements...
   maxDamage = 100;



See also:

"Java For Complete Beginners - Variables: the int type" ( http://bit.ly/ZA1aE8 )

Tuesday, May 29, 2012