Posts
60
Comments
36
Trackbacks
18
Conditions in Java

Conditions in Java are a true/false representation of some case.  Some of the most popular places to see conditions would be if statements,  for, while, do..while  loops or boolean variables can also be declared with some sort of condition.  Here we look over a short excerpt of some of the ways you might see conditions in Java.

Note: Comparing variables in Java require the use of operands, which are different then math expressions.  Equals (=) makes a variable equal to the equivalent of the left side of the equation.  Operands would be (==) meaning "equals" which will test whether the two are identical or note, (<=) "less than equals" which tests whether one value is less than or equal, (>=) "greater than equal", (>) "greater than", (<) "less than", (!=) "not equal", etc.

Additionally, if you tack on a ! to the beginning of a condition, it returns true if the condition evaluates false.  The best way to say this in English is NOT.  If I see an exclamation, I just speak the word "NOT" out loud.  here:

if (!condition) {...}

I would speak this aloud as "if NOT condition"...

Basic if statement:

if (condition) {...}

Often you will see the condition being something similar to "(i == 2)" or "(text.equalsIgnoreCase("hello"))".  The basic rule here is that the condition statement must represent some boolean expression.  A boolean is either true OR false, therefore a condition is always true OR false.

Basic for condition:

for (int i = 0; condition; i++) {...}

Notice here, our condition is the middle of the for statement.  The right hand side of a Java for statement is the control, the left hand side tells Java what to do after every iteration of the for loop, and the middle is a condition.  To read this aloud in English, I might say:

"for i = 0 to condition, i goes up by 1 each time through"

Basic while condition:

while (condition) {...}

Here it is important to realize that if your condition is false, the {...} will never be executed.  Also, if entering your while loop with a condition of true, and to avoid a forever loop, you must reach a point at which this condition can be false inside of your curls {...}.

Basic do...while condition:

do {...} while (condition)

In a do...while, your code will always be executed once.  When we reach the while, if the condition evaluates to true then we will execute the block of code again starting at the do.

Some examples of conditions:

(i <= 10), i.e.

for (int i = 0; i <= 10; i++)
{
    System.out.println("Hello: " + i);
}
 
//Outputs
//Hello: 1
//Hello: 2
//Hello: 3
//...
//Hello: 10

(text.equalsIgnoreCase("hello"))

String text = "HELLO";
if (text.equalsIgnoreCase("hello")
{
    System.out.println(text + " equals hello, ignoring case");
}
//Output:
//HELLO equals hello, ignoring case
 
(!text.equalsIgnoreCase("hello"))
String text = "HELLO1";
if (!text.equalsIgnoreCase("hello")
{
    System.out.println(text + " NOT equals hello, ignoring case");
}
//Output:
//HELLO NOT equals hello, ignoring case

 

Here we have looked at conditions and loops.  Please feel free to leave additional questions in the comments section here.

Technorati Tags:
posted on Tuesday, December 30, 2008 1:02 PM Print
Comments
No comments posted yet.

Post Comment

Title *
Name *
Email
Url
Comment *  
Please add 5 and 7 and type the answer here:
News
My Developer Notebook! This also happens to be my opinion place. Thanks for coming by.