Menu

1 / 2

#35 Known is a drop! break; return; continue; yield; throw;

DEV Community·Deepikandas·6 months ago
#RHgmNvyA
#beginners#java#programming#tutorial#return
Reading 0:00
15s threshold

1. break

Used to immediately stop a loop or switch.

for ( int i = 1 ; i <= 5 ; i ++) { if ( i == 3 ) { break ; } System . out . println ( i ); } Output: 1 2

When i == 3 , loop stops completely .

Enter fullscreen mode

Exit fullscreen mode

2. continue

Skips current iteration and moves to next iteration.

for ( int i = 1 ; i <= 5 ; i ++) { if ( i == 3 ) { continue ; } System . out . println ( i ); }

Output:

1 2 4 5

3 is skipped , but loop continues .

Enter fullscreen mode

Exit fullscreen mode

3. return

Ends the method execution.

public int add ( int a , int b ) { return a + b ; }

Enter fullscreen mode

Exit fullscreen mode

After return, nothing else in the method executes.

return;

Used in void methods.

A return statement in Java can return:

1. Primitive values

return 10;

return 3.14;

return true;

return 'A';

2. Objects

return "Hello";

return new Student();

Can return:

String

Arrays

Collections

Custom objects

Any class object

3. Arrays

return new int[]{1,2,3};

4.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.