Monday, August 29, 2016

JAVA 7 - The conditional world of if's

Next off the rank is covering conditionality and the “if” statement.  Up until now, we’ve been creating programs where we execute every line of code.  Thats about to change!

Scene from Jim Henson's Labyrinth, where Jennifer Connolly must choose between two doors using the application of logic.

If statements are the flexibility and decision making in our program.  They allow the program to do assessments on data provided, and only execute a command if certain conditions have been met.

if (condition)


if (condition)

Above are two basic examples of "if" statements

  • if – defines that it is an if statement
  • condition – this is a logical expression which is contained within the () brackets.
  • action1 – this is a set of statements within the {} brackets which will activate if Condition is true
  • else – else is optional, but useful.  This allows you to run action2 if the Condition has returned false

Relational Operators

Relational operators in Java allow you to do a comparison for your if statement - we looked at some other operators previously here.

Say for instance we have two numbers num1 and num2, which could be integers or double decimals.

num1 == num2

This is an equivalence operator, which returns true if num1 and num2 are the same.  Note, this is a double equals sign, as a single instance equals,

num1 = num2

Is an assignment.  This statement will set num1 to the value of num2, not compare it.  In such cases it will always return true, unless num2 happens to be zero.

num1 < num2

Less than.  Returns true if num1 is less than num2.  If they are equal it returns false.

num1 > num2

Greater than.  Returns true if num1 is greater than num2.  If they are equal it returns false.

num1 <= num2

Less than equal to.  Returns true if num1 is less than num2.

num1 >= num2

Greater than equal to.  Returns true if num1 is greater than num2.

You can also choose to have more complex statements by chaining conditions together and using conditional AND "&&" or conditional OR "||".  Let's add another number, num3 ...

(num1 > num2) && (num1 < num3)

This statement returns true if num1 is greater than num2 AND less than num3.

(num1 < num2) || (num1 > num3)

This statement returns true if num1 is less than num2 OR greater than num3.

You can also choose to use within condition a function call, so long as it returns a true or false evaluated statement.

For example we have a String str1, then we can do the following conditions for example,


Will return true if the String contains the word "Mike".

str1.length() > 255

Will return true if str1 is longer than 255 characters.

Else if

In addition to just the a simple else, we can also have the else if command.

if (condition1)
else if (condition2)

If condition1 is false, the condition2 will be checked, and if this is true, action2 will be executed.

If condition1 is true, condition2 is hidden by the else clause, and will not be executed.

Example 1 - Temperature gauge

We need temperature function which raises alarms if a temperature exceeds a threshold,

  • Below 150C nothing happens
  • 150C and above to just under 200C, a warning is raised
  • 200C and above to just under 250C, a danger alarm is raised
  • 250C and above, critical warning is raised

My code for this, as always, can be found here on Github.

I've created two methods which do the same thing.  Here is a basic method which shows you it all in great detail ...

This method though is a little bit smarter, using else if and if ...

Example 2 - Learning to count with Big Brother

Mathematics is hard and complicated.  And the tool of the intellectual to undermine to plant doubt in minds against the sound logic of The Party.  And hence it is that maths can and should be simplified to make it easier to learn.

And hence Big Brother, in his infinite wisdom reminds you that 2 + 2 = 5.  Because 2 + 2 have always equalled 5.

2 + 2 = 4 is an easy mistake to make, but thanks to this program, one you need never make again.  With luck you'll never see inside Room 101 - although you already know what waits for you there ...

Unfortunately the party has had to remind us that agents of the traitor Goldstein have been saying that 2 + 3 = 5, when obviously they are 7.  Please correct this mistake with the following method ...

The code for this is here in Github.  The code for this has always been here in Github.

Switch statement

The switch statement is variant of if statement that works for integers.  If you have an integer intNum, you perform a switch on it, and use the case statement as below,

switch (langID)
  case 1:
  case 2:
  case 3:
  case 4:

I've always found switch statements odd syntactically.  It's like they belong to another language.

They don't have areas of statements bounded by {..} if the case matches.  You also have to use the break command to break out of the switch statement if you're done.

So for this example, if langID is,

  • 1 - prints "PrintA" (because case 1 has no break command and follows into the case 2 commands).
  • 2 - prints "PrintA", but no more because of the break command.
  • 3 - prints "PrintB" and "PrintC" because there's no break in case 3, it goes into the case 4 execution
  • 4 - prints "PrintC"
  • anything else, and it prints "PrintD"
Switch statements are tricky to learn at first - there's an example below, and also read here.  But they're useful because they do tidy up large numbers of actions to be simple to read.

Example 3 - Hola Mundo

This is an advanced form of Hello World.  I love how you can keep playing with the Hello World concept whilst exploring concepts.

This time we want a method which will print Hello World in a variety of languages depending on the integer number passed in.  Press,
  • 1 for Spanish
  • 2 for German
  • 3 for French
  • Any other number for English

Now the same function, but written with a switch statement ...

Note - because I purposely left the break command out of case 2, you get German and French printed out.  This code can be found here.

Expansion material
  • Read more on switch statements
  • Read more about relational operators here


  1. The switch statement also accepts Strings (if using Java 7 and above) so isn't limited to integers any more

    1. Nice. Thanks for that. This is where my old school programming let's me down. ☺

  2. perfect explanation about java programming .its very useful.thanks for your valuable training in chennai | java training in velachery

  3. It is really a great work and the way in which u r sharing the knowledge is excellentand i am waiting for your next post keep on updating these kinds of knowledgeable things...Really it was an awesome article.
    java training in chennai |
    java training institutes in chennai

  4. Nicely explained. Here you described the well written article from your in-depth knowledge. Truly impressive and nice information

    Java Training in Chennai Core Java Training in Chennai Core Java Training in Chennai

    Java Online Training Java Online Training JavaEE Training in Chennai Java EE Training in Chennai

  5. Gambling games that you can play at home.

    จีคลับ Web casino that will give investors the chance to meet the gambling luck. Played very well Gambling is live from the real casino. Bring it to everyone to play it realistically. There are many bets. Make huge profits. Where is the fun? New bets Have fun at any time like Gambling to help players find the casino game is the number one. Play Where to Place Wherever You Are It is ready to play the luck as anyone can play well.

    Gamblers who come to play casino online at our website. Investors are encouraged to invest in gambling. The gambler will also receive a lot of promotions that we have been held for investors to get here. Gamblers who are interested in subscribing to our website, then the gambler is able to invest in our online gambling services at this player will not be disappointed in this game. The player will be able to play easily with our online casino games with standard. The gambler will not be disappointed that we are open to the investors. บาคาร่า