HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / Java Basics / Java Flow Control Statements

Java Flow Control Statements

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");
		}
	}
}

Program output.

Condition is true

Read More : Java if-else statement

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.

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;
		}
	}
}

Program output.

Value is B

Read More : Java switch statement

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++;
        }
    }
}

Program output.

1
2
3
4
5

Read More : Java while loop statement

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);

Program output.

55

Read More : Java do-while loop statement

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.

1
2
3
4
5

Read More : Java for-loop statement

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

Read More : Java for-each statement

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);
			}
		}
	}
}

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

Read More : Java label statement

Happy Learning !!

Reference: Java Docs

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Comments are closed on this article!

Search Tutorials

Java Flow Control

  • Java Flow Control Statements
  • Java if-else Statement
  • Java Switch Statement
  • Java for Loop
  • Java for-each Loop
  • Java while Loop
  • Java do-while Loop
  • Java break Statement
  • Java continue Statement
  • Java Labeled Statement

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces