HowToDoInJava

  • Java 8
  • Regex
  • Concurrency
  • Best Practices
  • Spring Boot
  • JUnit5
  • Interview Questions
  • Dark Mode

Java Control Flow Statements

By Lokesh Gupta | Filed Under: Java Basics

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

TwitterFacebookLinkedinRedditPocket

About Lokesh Gupta

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

Leave a Reply

This comment form is under antispam protection
This comment form is under antispam protection
  Subscribe  
Notify of

Search Tutorials

Java Tutorial

  • Java – Introduction
  • Java – JDK, JRE and JVM
  • Java – Naming Conventions
  • Java – ClassPath
  • Java – Variables
  • Java – Operators
  • Java – keywords
  • Java – Data Types
  • Java – Primitive Types
  • Java – Wrapper Classes
  • Java – Types of Statements
  • Java – Control Statements
  • Java – Labeled Statements
  • Java – String
  • Java – Create Class
  • Java – Immutable class
  • Java – main() Method
  • Java – Comments
  • Java – Pass-by-Value
  • Java – System Properties
  • Java – Static
  • Java – Static Import
  • Java – hashCode() and equals()
  • Java – this and super
  • Java – 32-bit vs. 64-bit
  • Java – java.exe vs javaw.exe
  • Java – Generate Bytecode
  • Java – Little-Endian vs Big-Endian
  • Java – Command Line Args
  • Java – Compare floats
  • Java – Recursion
  • Java – Pairs
  • Java – Tuples
  • Java – sun.misc.Unsafe
  • Java – UUID

Popular Tutorials

  • Java 8 Tutorial
  • Core Java Tutorial
  • Collections in Java
  • Java Concurrency
  • Spring Boot Tutorial
  • Spring AOP Tutorial
  • Spring MVC Tutorial
  • Spring Security Tutorial
  • Hibernate Tutorial
  • Python Tutorial
  • Jersey Tutorial
  • Maven Tutorial
  • Log4j Tutorial
  • Regex Tutorial

Meta Links

  • Advertise
  • Contact Us
  • Privacy policy
  • About Me

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 © 2016 · HowToDoInjava.com · All Rights Reserved. | Sitemap

wpDiscuz