Java Control Flow Statements

It is sometimes necessary to perform repeated actions or skip some statements in a program. For these actions certain control statements are available. These statements control the flow of execution of the programs.

Decision Making Statements

  • if
  • if – else
  • switch

if Statement

Syntax : if( <condition> ) { Statement 1; Statement 2; Statement n; }
if Statement Example
/*
    if Statement Example
    Save with file name IfStatement.java
*/

public class IfStatement
{
	public static void main(String args[])
	{
		int a = 30, b = 40;
		System.out.println();
		System.out.println("if Statement Example ");
		System.out.println("====================");
		System.out.println();

		if(a > b)
			System.out.println("a is greater than b");
		System.out.println("Greater Value");	//OUT PUT STATEMENT

		if(a < b)
			System.out.println("a is less than b");	//OUT PUT STATEMENT
		System.out.println("Lesser Value");	//OUT PUT STATEMENT
		System.out.println();

		System.out.println("------- ANOTHER WAY -------");
		System.out.println();

		// ANOTHER WAY OF IF STATEMENT
		// IF THE CONDITION HAS MORE THAN ONE STATEMENT
		// THEN YOU SHOULD USE THOSE STATEMENTS WITH IN BRACES
		if(a > b)
		{
			System.out.println("a is greater than b");
			System.out.println("Greater Value");
		}
		if(a < b)
		{
			System.out.println("a is less than b");	//OUT PUT STATEMENT
			System.out.println("Lesser Value");	//OUT PUT STATEMENT
		}

	}
}
                        
if Statement Example2
/*
    if Statement Example
    Save with file name IfStatement.java
*/

public class IfStatement
{
	public static void main(String args[])
	{
		int a = 30, b = 40;
		System.out.println();
		System.out.println("if Statement Example ");
		System.out.println("====================");
		System.out.println();

		if(a > b)
			System.out.println("a is greater than b");
		System.out.println("Greater Value");	//OUT PUT STATEMENT

		if(a < b)
			System.out.println("a is less than b");	//OUT PUT STATEMENT
		System.out.println("Lesser Value");	//OUT PUT STATEMENT
		System.out.println();

		System.out.println("------- ANOTHER WAY -------");
		System.out.println();

		// ANOTHER WAY OF IF STATEMENT
		// IF THE CONDITION HAS MORE THAN ONE STATEMENT
		// THEN YOU SHOULD USE THOSE STATEMENTS WITH IN BRACES
		if(a > b)
		{
			System.out.println("a is greater than b");
			System.out.println("Greater Value");
		}
		if(a < b)
		{
			System.out.println("a is less than b");	//OUT PUT STATEMENT
			System.out.println("Lesser Value");	//OUT PUT STATEMENT
		}

	}
}
                        

if else Statement

Syntax : if(<condition>) { Statement 1; Statement 2; Statement n; } else { Statement 1; Statement 2; Statement n; }
if else Statement Example
/*
    if else Statement Example
    Save with file name IfElseStatement.java
*/

public class IfElseStatement
{
	public static void main(String args[])
	{
		int a = 30, b = 40;
		System.out.println();
		System.out.println("if Else Statement Example ");
		System.out.println("====================");
		System.out.println();

		if(a > b)
			System.out.println("a is greater than b");
		else
			System.out.println("a is less than b");	//OUT PUT STATEMENT
		System.out.println();

		System.out.println("------- ANOTHER WAY -------");
		System.out.println();

		// ANOTHER WAY OF IF STATEMENT
		// IF THE CONDITION HAS MORE THAN ONE STATEMENT
		// THEN YOU SHOULD USE THOSE STATEMENTS WITH IN BRACES
		if(a > b)
		{
			System.out.println("a is greater than b");
			System.out.println("Greater Value");
		}
		else
		{
			System.out.println("a is less than b");	//OUT PUT STATEMENT
			System.out.println("Lesser Value");	//OUT PUT STATEMENT
		}

	}
}
                        

switch Statement

Syntax: switch(<value>) { Case 1; Case 2; Case n; Default Case; }
switch Statement Example
/*
    switch Statement Example
    Save with file name IntSwitchStatement.java
*/

public class IntSwitchStatement
{
	public static void main(String args[])
	{
		int a = 2, b = 40;
		System.out.println();
		System.out.println("int Switch Statement Example ");
		System.out.println("============================");
		System.out.println();

		// int value parameter single statement in case
		switch(a)
		{
			case 1:
				System.out.println("a value is One");
			case 2:
				System.out.println("a value is Two");
			default:
				System.out.println("a value is Default Value");
		}

		System.out.println("------------------------");
		// int value parameter Multiple statements in case
		switch(a)
		{
			case 1:
			{
				System.out.println("Case 1 executed");
				System.out.println("a value is One");
				break;
			}
			case 2:
			{
				System.out.println("Case 2 executed");
				System.out.println("a value is Two");
				break;
			}
			default:
			{
				System.out.println("Default Case executed");
				System.out.println("a value is Default Value");
				break; //NOT REQUIRE BECAUSE LAST STATEMENT
			}
		}

	}
}
                        
Relational Operators
/*
    switch Statement Example
    Save with file name CharSwitchStatement.java
*/

public class CharSwitchStatement
{
	public static void main(String args[])
	{
		char c = 'b'; //char DATA TYPE DECLARATION AND ASSIGN A VALUE
		System.out.println();
		System.out.println("char Switch Statement Example ");
		System.out.println("=============================");
		System.out.println();

		// char value parameter single statement in case
		switch(c)
		{
			case 'a':
				System.out.println("case a");
			case 'b':
				System.out.println("case b");
			case 'c':
				System.out.println("case c");

			default:
				System.out.println("default case");
		}

		System.out.println("------------");
		// char value parameter Multiple statements in case
		switch(c)
		{
			case 'a':
			{
				System.out.println("Case a");
				break;
			}
			case 'b':
			{
				System.out.println("Case b");
				break;
			}
			case 'c':
			{
				System.out.println("Case c");
				System.out.println("a value is Two");
				break;
			}
			default:
			{
				System.out.println("Default Case");
				break; //NOT REQUIRE BECAUSE LAST STATEMENT
			}
		}

	}
}