Java Flow Control Statements

326 views
optmiize_your_resume_1

Java application code is normally executed sequentially from top to bottom in the order that the code appears. To apply business logic, we may need to execute code on conditional basis. Control flow statements helps in this conditional execution of code blocks.

All control flow statements are associated with a business condition – when true, the code block executes; when false it is skipped.

Java supports following control statements.

1. If-else Statement

If-else statement tells the program to execute a certain section of code only if a particular test evaluates to true otherwise else block is executed.

We can have nested if-else blocks.

public class JavaExample { public static void main(String[] args) { boolean condition = true; if(condition) { System.out.println("Condition is true"); } else { System.out.println("Condition is false"); } } }
Code language: JavaScript (javascript)

Program output.

Condition is true

2. Switch Statement

As if-else statement tells your program to execute a certain section of code only if a particular test evaluates to true or false, the switch statement can have multiple execution paths.

Java Flow Control Statements

A switch works with the byte, short, char, and int primitive data types. It also works with enumerated types, the String class, and a few special classes that wrap certain primitive types: Character, Byte, Short, and Integer. (enums were added in java 5, and String class was added in java 7).

public class JavaExample { public static void main(String[] args) { String value = "B"; switch (value) { case "A": System.out.println("Value is A"); break; case "B": System.out.println("Value is B"); break; default: System.out.println("Value is neither A nor B"); break; } } }
Code language: JavaScript (javascript)

Program output.

Value is B

3. While Loop

The while statement or loop continually executes a block of statements while a particular condition is true. The while statement continues testing the expression and executing its block until the expression evaluates to false.

public class JavaExample { public static void main(String[] args) { int count = 1; while (count < 5) { System.out.println("Count is: " + count); count++; } } }
Code language: JavaScript (javascript)

Program output.

1
2
3
4
5 

4. Do-while Loop

The difference between do-while and while is that do-while evaluates its expression at the bottom of the loop instead of the top. Therefore, the statements within the do block are always executed at least once.

Note that the do-while statement ends with a semicolon. The condition-expression must be a boolean expression.

int i = 1; int sum = 0; do { sum = sum + i; i++; } while (i <= 10); System.out.println(sum);
Code language: JavaScript (javascript)

Program output.

Java Flow Control Statements
  55 

5. For Loop

The for statement iterates over a range of values. It repeatedly loops over values until a particular condition is satisfied.

for(int num = 1; num <= 5; num++) { System.out.println(num); }

Program output.

12345

6. Enhanced For-each Loop

Java 5 introduced an foreach loop, which is called a enhanced for-each loop. It is used for iterating over elements of arrays and collections.

int[] numList = {10, 20, 30, 40}; for(int num : numList) { System.out.println(num); }

Program output.

10
20
30
40

7. Labeled Statement

Whenever during a program execution, a labeled break statement is encountered that control immediately goes out of enclosing labeled block. Similarly, labeled continue will bring control back to start. Just like in normal break and continue statements, with additional names given to blocks.

public class JavaExample { public static void main(String[] args) { loop: for(int i=0; i < 6; i++) { if(i % 2 == 0) { System.out.println("In if block :: " + i); continue loop; } else { System.out.println("In else block :: " + i); } } } }
Code language: JavaScript (javascript)

Program output.

In if block :: 0
In else block :: 1
In if block :: 2
In else block :: 3
In if block :: 4
In else block :: 5

Happy Learning !!

Java Flow Control Statements
20%off for Ink 280 281

You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More