Statements, loops and conditions | Preparing for Java Oracle Associate Certification (OCA) / Java basic technical tests

Loops and conditions are the core behaviors of programming languages. We’ll have a look at the particularities of them in Java and the tricks that you could encounter at tests or exams.

This series of articles are meant for people who already know the basics of Java, that is grammar and vocabulary, and knowledge of OOP. The topics covered correspond to OCA Programmer I level and relate to Java 8 version. If you need to learn the basics, I can recommend the JetBrains Academy Java developer track.

Switch statement

switch/case statement supports the following types:

  • int
  • byte
  • short
  • char
  • String
  • Enum
  • (wrappers only in the Switch)

and thus doesn’t support boolean (that would be a plain if/else statement), long, floating point types and reference types. All those would prevent code from compiling.

All case statements must share the same type as the switch (covariance rules apply).

The switch variable must be big enough to hold all the case constants (checked at compile time). And yes, that’s important, case statements must use compile-time constants.

The case statements must use different values from each other (also checked at compile time).

One usual trick in tests is not to put a break; statement on each case line, so remember that the swith/case will execute the first case statement that matches and will continue until it meets a break; or a return;.

While and Do… while

They are pretty simple. Just pay attention to exit conditions (are they ever met?) as well as exit statements (described in the end of the article). Also whenever a boolean expression, always pay attention to the sign being used (== or =) and associated tricks I explained in previous article about the Java operators.

Don’t forget the ; after a do-while!

while (boolean expression) {
  // this is reached 0-N times
  // if boolean expression returns false the block is not entered
}

do {
  // this is always reached at least once before checking the boolean expression
} while (boolean expression);

Pay attention to this. Why is it so? Try to answer before reading next paragraph.

while (false) {
  // won't compile
}

do {
  // will compile!
} while (false);

The while loop is only entered if the boolean condition returns true, which will never happen if the boolean expression is “false”. The do-while loop is always entered at least once before the boolean expression is checked. Even though it’s dirty to write that, it’s legit. Don’t forget that you may exit the loop without making use of the boolean expression in the while clause (explained in the end of the article).

For loop (“for i”)

for ( 1 ; 2 ; 3) {
  // ...
}
  • 1 – initialization(s): usually we would write int i = 0 (we initialize an iteration variable). Please note multiple initialization (N variables) is legit.
  • 2 – boolean expression: there can be maximum 1 expression.
  • 3 – update statement: what to do with the initialization variable(s).

The cycle goes like so:

(1) Variables are initialized, (2) boolean expression is read, if it’s evaluated to false we exit the loop, otherwise the body is executed, THEN (3) the update statement is executed, (2) boolean expression is read, and so on. Init statement is executed only once, then it’s a loop between (2) and (3).

Important to know is that neither of the 3 statement is mandatory! for( ; ; ) is totally legit. However, for ( … ; false ; …) will not compile for the same reason as the while (false) statement.

Foreach (for Item item: items)

Foreach loop can be used on an Iterable object (an array or a Collection usually).

for (DataType item: Collection) {
  // ...
}

The variable used to iterate cannot be a variable that was declared before (that’s different from the classic for-loop where initialization can use a variable already defined). Type must match with the type of the Collection (think about covariance though). Also that variable can be final and that’s the only modifier allowed.

If condition

if (boolean expression {
  //...
}

if (boolean expression {
  //...
} else {
  //...
}

if (boolean expression {
  //...
} else if (boolean expression) {
  //...
} else {
  //...
}

Know that, for some reason, if (false) is totally legit (although the block will never be entered).

Braces / blocks

Concerning all but switch/case statement. Good practice is to use braces {} to hold the code block that’ll be executed. However, they are not mandatory if the code to execute is a one-liner (well, a single statement to be more precise, because putting several statements on a line won’t do the trick).

That’s a recurring trick in tests, pay extra attention for example to such code:

if (boolean expression)
  doSomething();
  doAnotherThing();
else
  doElse();

It would be all good without the else statement. But as no braces are used, the if statement ends after doSomething() is executed, so the else statement is an orphan (which is not legit). Be careful that in if/else statements, braces and no braces styles can be used together. Also, at the certification, they love not to indent the code so it won’t help you.

The exit statements

Loops and conditions can be exited manually with:

  • return; = exit the method; it can either return a value or nothing
  • break; = terminates the nearest inner loop and continues with next statement; can be used with while, do-while, for, switch (and inside an if statement only if inside one of those)
  • continues; = terminates the current execution of the running loop and goes back to the boolean evaluation; can be used with while, do-while, for (and if inside one of those) but not is switch/case!

A trick in tests is to put some code after those to check if you can spot unreachable code (which triggers a compiling error).


This topic may seem easy but a lot of tricks can be applied so make sure to train so you can spot them quickly and don’t waste time solving long code snippets for nothing.

Next post will be about String and String Builder. Don’t forget to subscribe so you won’t miss any new post. Also check previous posts of the Java certification series.

Leave a Reply

Your email address will not be published. Required fields are marked *

Don’t miss out!

Receive every new article in your mailbox automatically.


Skip to content