Java For Each Loop

For-Each Loop

There is also a “for-each” loop, which is used exclusively to loop through elements in an array (or other data sets):

Syntax

for (type variableName : arrayName) {
// code block to be executed
}

The following example outputs all elements in the cars array, using a “for-each” loop: Continue reading Java For Each Loop

Java Nested Loops

Nested Loops

It is also possible to place a loop inside another loop. This is called a nested loop.

The “inner loop” will be executed one time for each iteration of the “outer loop”:

Example

// Outer loop
for (int i = 1; i <= 2; i++) {
  System.out.println("Outer: " + i); // Executes 2 times
  
  // Inner loop
  for (int j = 1; j <= 3; j++) {
    System.out.println(" Inner: " + j); // Executes 6 times (2 * 3)
  }
} 

Continue reading Java Nested Loops

Java For Loop

Java For Loop

When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop:

Syntax

for (statement 1; statement 2; statement 3) {
  // code block to be executed
}

Statement 1 is executed (one time) before the execution of the code block.

Statement 2 defines the condition for executing the code block.

Statement 3 is executed (every time) after the code block has been executed.

The example below will print the numbers 0 to 4: Continue reading Java For Loop

Java While Loop Examples

Real-Life Examples

To demonstrate a practical example of the while loop, we have created a simple “countdown” program:

Example

int countdown = 3;

while (countdown > 0) {
  System.out.println(countdown);
  countdown--;
}

System.out.println("Happy New Year!!");

 

To demonstrate a practical example of the while loop combined with an if else statement, let’s say we play a game of Yatzy: Continue reading Java While Loop Examples

Java Do While Loop

The Do/While Loop

The do/while loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true.

Syntax

do {
  // code block to be executed
}
while (

condition

);

The example below uses a do/while loop. The loop will always be executed at least once, even if the condition is false, because the code block is executed before the condition is tested: Continue reading Java Do While Loop

Java While Loop

Loops

Loops can execute a block of code as long as a specified condition is reached.

Loops are handy because they save time, reduce errors, and they make code more readable.


Continue reading Java While Loop

Java Switch

Java Switch Statements

Instead of writing many if..else statements, you can use the switch statement.

The switch statement selects one of many code blocks to be executed:

Syntax

switch(

expression

) {
  case x:
    // code block
    break;
  case y:
    // code block
    break;
  default:
    // code block
}

This is how it works:

  • The switch expression is evaluated once.
  • The value of the expression is compared with the values of each case.
  • If there is a match, the associated block of code is executed.
  • The break and default keywords are optional, and will be described later in this chapter

The example below uses the weekday number to calculate the weekday name: Continue reading Java Switch

Java If … Else Examples

Real-Life Examples

This example shows how you can use if..else to “open a door” if the user enters the correct code:

Example

int doorCode = 1337;

if (doorCode == 1337) {
  System.out.println("Correct code. The door is now open.");
} else {
  System.out.println("Wrong code. The door remains closed.");
}

 

This example shows how you can use if..else to find out if a number is positive or negative: Continue reading Java If … Else Examples

Java Short Hand If…Else (Ternary Operator)

Short Hand if…else

There is also a short-hand if else, which is known as the ternary operator because it consists of three operands.

It can be used to replace multiple lines of code with a single line, and is most often used to replace simple if else statements: Continue reading Java Short Hand If…Else (Ternary Operator)

Java Else If

The else if Statement

Use the else if statement to specify a new condition if the first condition is false.

Syntax

if (

condition1

) {
  // block of code to be executed if condition1 is true
} else if (

condition2

) {
  // block of code to be executed if the condition1 is false and condition2 is true
} else {
  // block of code to be executed if the condition1 is false and condition2 is false
}

Example

int time = 22;
if (time < 10) {
  System.out.println("Good morning.");
} else if (time < 18) {
  System.out.println("Good day.");
} else {
  System.out.println("Good evening.");
}
// Outputs "Good evening."

 

Continue reading Java Else If