Java Branching Statements

Java Control Flow Statements

Branching Statements in Java

In this tutorial you can learn about Java Branching Statements like break Statement, continue Statement and return Statement. This tutorial is also example for branching statements in java.

  • break
  • continue
  • return

Java break Statement Example

/*  Java break Statement Example  */
/*  Save with file name BreakStatement.java  */

public class BreakStatement
{
	public static void main(String args[])
	{
		System.out.println("Java break Statement Example ");

		int count = 10;

		while(true) //INFINIT LOOP
		{
			if(count == 0)
				break;

			System.out.println(count);

			count--;
		}
	}
}
				    

Java continue Statement Example

/*  Java continue Statement Example  */
/*  Save with file name ContinueStatement.java  */

public class ContinueStatement
{
	public static void main(String args[])
	{
		System.out.println("Java continue Statement Example ");

		int count = 1;

		while(count<=100)
		{
			if(count <= 10)
			{
				System.out.println(count);

				count++;

				continue;
			}
			else
			{
				break;
			}
		}
	}
}
				    

Java return Statement Example

/*  Java return Statement Example  */
/*  Save with file name ReturnStatement.java  */

public class ReturnStatement
{
	public static void main(String args[])
	{
		System.out.println("Java return Statement Example ");

		int max = getMaximumNumber(10, 20);

		if(max==0)
			System.out.println("Equal Numbers");
		else
			System.out.println("Max Number is : " + max);
	}

	//METHOD

	public static int getMaximumNumber(int x, int y)
	{
		if(x > y)
			return x;

		else if(x < y)
			return y;

		else
			return 0;
	}
}