Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6094793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:43:16+00:00 2026-05-23T12:43:16+00:00

Code Example: public class TestReturn { public void printNum(int[] ab){ int i = 0;

  • 0

Code Example:

public class TestReturn {
    public void printNum(int[] ab){

        int i = 0;
        for( i=0; i<ab.length; i++){
        if(ab[i] < 10){
             System.out.println("less than 10");
             return;
        }
        else{
             System.out.println("more than or equal to 10");
             return;
        }
        }
    }
    public static void main(String args[])
    {
        TestReturn a = new TestReturn();
        int[] ab = {67, 56, 34, 89, 2, 23, 92, 33, 9, 74};
        a.printNum(ab);

    }

}

In the above code return has been used twice. While running the code you can see that according to the input the code runs only once . Now if the return statement in the else block is commented out the loop runs for 5 times till it reaches the value 2 and then it stops printing.

This can be achieved through break statement also. Are there any more advantages of this return statement?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T12:43:17+00:00Added an answer on May 23, 2026 at 12:43 pm

    The only advantage of the return statement is that it exits the method. So if you had any further code after the loop and used break, that would be executed.

    With return, it is not executed.

    This is generally how I use return – if I know the method has finished executing for the purposes of what you need it for, I put a return statement in.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If i have the following code example: public class ClassBase { public int ID
here is my example code: Public Class Parent Private _TestProperty As String Private WithEvents
take a look at this example code: public class Comment { private Comment() {
I have this code for example public class BaseClass {} public class DerivedClass1 :
I was browsing over the following code example: public class GenericTest { public static
Given the following code from a Microsoft example: public class EngineMeasurementCollection : Collection<EngineMeasurement> {
Consider following example : public class SomeBusinessLayerService : DataService<MyEntityContainer> { [WebInvoke] void DoSomething(string someParam)
I have piece of code: class example { public function say($x) { if ($x
Is the following code resistant to both Serialization and Reflection Attacks? public class Example{
This is an example code: public class MyParent : INotifyPropertyChanged { List<MyChild> MyChildren; public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.