OCA Java SE 8 Mock Exam Review - Java Statement Review








if and if-else constructs

The if statement executes a set of statements in your code based on the result of a condition, which should evaluate to a boolean or Boolean value.

An if construct may or may not define its else part.

The else part of an if construct can't exist without the definition of its then part.

You can execute a single statement or a block of statements for corresponding true and false conditions.

A pair of braces marks a block of statements: {}.

If an if statement doesn't use {} to define a block of code, only the first line of code is part of the if construct.

An assignment of a boolean variable can be passed as an argument to the if construct.





switch statements

A switch statement is used to compare the value of a variable with multiple predefined values.

A switch statement accepts arguments of type char, byte, short, int, and String.

The case value should be a compile-time constant, assignable to the argument passed to the switch statement.

The case value can't be the literal value null.

The case value can define expressions that use literal values, for example 7+2.

One code block can be defined to execute for multiple case values.

A break statement is used to exit a switch construct once a matching case is found.

If there is no break statement, control will fall through all the remaining case values in a switch statement until the first break statement is found.





for loops

A for loop defines three types of statements separated by semicolons (;): initialization statements, termination condition, and update clause.

The definition of any of the three for statements-initialization statements, termination condition, and update clause-is optional.

The initialization block executes only once.

A for loop can declare and initialize multiple variables in its initialization block, but the variables that it declares should be of the same type.

The termination condition is evaluated once, for each iterations, before the statements defined within the body of the loop are executed.

The for loop terminates when the termination condition evaluates to false.

The update block is used to increment or decrement the value of the variables defined in the initialization block.

Enhanced for loops

The enhanced for loop is also called the for-each loop.

The enhanced for loop has simple syntax to iterate through a collection of values-an array, List, or other classes from Java's Collection framework.

The enhanced for loop can't be used to initialize an array and modify its elements.

The enhanced for loop can't delete the elements of a collection.

The enhanced for loop can't be used to iterate over multiple collections or arrays in the same loop.

while and do-while loops

A while loop keeps executing a set of statements until the condition that it uses evaluates to false.

A while loop checks the condition before it starts the execution of the statement.

A do-while loop keeps executing a set of statements until the condition that it uses evaluates to false.

A do-while loop checks the condition after it completes the execution of all the statements in its loop body.

Both do-while and while loops can define either a single line of code or a code block to execute.

The block of code is defined by using curly braces, {}.

Both the do-while and while loops execute a set of statements until the termination condition evaluates to false.

The do-while loop executes the code at least once, even if the condition evaluates to false.

break and continue

The break statement can exit the for, for-each, do, and do-while loops and the switch construct.

The continue statement skips the remaining steps in the current iteration and starts with the next loop iteration.

The continue statement works with the for, for-each, do, and do-while loops and the switch construct.

When using the break statement with nested loops, it exits the inner loop.

When using the continue statement with nested loops, it exits the current iteration of the inner loop.

Labeled statements

You can add labels to a code block defined using braces, {}.

You can't add labels to declarations of variables.

You can use a labeled break statement to exit an outer loop.

You can use a labeled continue statement to skip the iteration of the outer loop.