Saturday 30 July 2022

Break, Continue, and Return Statements in Java

Jumping within or out of a loop / coming out from an infinite loop is the main requirements in an iterative program. Returning results to the calling method is done using return statement. Here in this post I am going to discuss about the three most useful java statements  Break, Continue, and Return.

1. The Break statement 

The break statement can be used in 3 ways:

  1. break is used inside a loop to come out of it.
  2. break is used inside the switch block to come out of the switch block.
  3. break can be used in nested blocks to go to the end of a block.

The following example shows you how the break statement is used 

class BreakDemo 
{
	public static void main(String[] args) 
	{
		boolean x=true;
		block1:
		{
			block2:
			{
				block3:
				{
					System.out.println(" Inside block3");
					if(x) break block3;
				}
				System.out.println(" Inside block2");
			}
			System.out.println(" Inside block1");
		}
		System.out.println("In the main method");
	}
}

Output:-

In the above program block1,block2,block3 are names of the blocks starting with a { and ending with a }. First, block3 will be displayed as the control of execution enters that block. Then it executes 

if(x) break block3;

This represents going to the end of the block3. After the closing }, we got block2, which is displayed followed by block1 and finally the main method block will be executed.

This type of break is similar to goto statements in C/C++, but goto statement is not available in java.

2.Continue statement in Java

Continue is used inside a loop to repeat the next iteration of the loop. When continue is executed, subsequent statements in the loop are executed and control of execution goes back to the next repetition of the loop.

Syntax:

continue;

Let us consider the following program for detailed understanding of continue

class ContiuneDemo 
{
	public static void main(String[] args) 
	{
		for(int i=10;i>=1;i--)
		{
	
		System.out.println(i);
		}
	}
}

Output

In the above program, the value of 'i' starts from 10 and decrement and print upto 1. Now, let us introduce continue in the above program and see what it will result 

class ContiuneDemo 
{
	public static void main(String[] args) 
	{
		for(int i=10;i>=1;i--)
		{
			if(i>5)
				continue;
		System.out.println(i);
		}
	}
}

Output:


Here, we are redirecting the flow of execution back to the next iteration of the loop when i>5; so, where 'i' value prints from 5 to 1 because when 'i' value are from 10 to 6, the if condition becomes false that is why the values from 10 to 6 are not printed.

3. Return statement

Like other programming languages, java methods also return values to the calling function. In java, when program execution starts the JVM first calls main() method( in case of no static method). If you want to call any user defined method that can be done from inside the main only.

 The return statement terminates the execution of the method. you can also use system.exit(0) to do the same.

More and detailed information about the use of return will be discussed at the time discussing the methods in java.

 

0 comments :

Post a Comment

Note: only a member of this blog may post a comment.

Machine Learning

More

Advertisement

Java Tutorial

More

UGC NET CS TUTORIAL

MFCS
COA
PL-CG
DBMS
OPERATING SYSTEM
SOFTWARE ENG
DSA
TOC-CD
ARTIFICIAL INT

C Programming

More

Python Tutorial

More

Data Structures

More

computer Organization

More
Top