Java Control Flow Statements

Control Flow Statements in Java

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 in Java

  • if
  • if – else
  • switch

Java if Statement

Syntax :
			    if( <condition> )
			    {
				    Statement 1;
				    Statement 2;
				    Statement n;
			    }
			    

Java if Statement Example

/*
    Java 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("Java if Statement Example ");

		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("------- ANOTHER WAY -------");

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

	}
}
				    

Java if Statement Example 2

/*
    Java if Statement Example 2
    Save with file name IfStatement.java
*/

public class IfStatement
{
	public static void main(String args[])
	{
		int a = 30, b = 40;

		System.out.println("Java if Statement Example ");

		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("------- ANOTHER WAY -------");

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

	}
}
				    

Java if else Statement

Syntax :
			    if(<condition>)
			    {
				    Statement 1;
				    Statement 2;
				    Statement n;
			    }
			    else
			    {
				    Statement 1;
				    Statement 2;
				    Statement n;
			    }
			    

Java if else Statement Example

/*
    Java 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("Java if Else Statement Example ");

		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("------- ANOTHER WAY -------");

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

	}
}
				    

Java switch Statement

Syntax:
			    switch(<value>)
			    {
				    Case 1:
				    {
				    }
				    Case 2:
				    {
				    }
				    Case n:
				    {
				    }
				    Default Case:
				    {
				    }
			    }
			    

Java switch Statement Example

/*
    Java 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("Java int Switch Statement Example ");

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

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

	}
}
				    

Java switch Statement Example

/*
    Java 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("Java char Switch Statement Example ");

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

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

	}
}