473,320 Members | 1,965 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

return

12
Hello guys!
this is the best forum for Java, and you guys are the best!

now >>> the question...
i mdae a method, and a for loop inside the method:

Expand|Select|Wrap|Line Numbers
  1. public String go(String width)
  2. {
  3.  for(i=0; i< 300; i++)
  4.   {
  5.      if(blablabla)
  6.       {return "a";}
  7.   }
  8. }
this is the example of the code, when i try to compile this application i have error: "missing return statement"....
so i guess i have to write the return statement after the for loop, but than it wouldn't be the same (and it would not consider my IF condition) ! please help me!
Nov 25 '07 #1
4 1260
JosAH
11,448 Expert 8TB
Hello guys!
this is the best forum for Java, and you guys are the best!

now >>> the question...
i mdae a method, and a for loop inside the method:

Expand|Select|Wrap|Line Numbers
  1. public String go(String width)
  2. {
  3.  for(i=0; i< 300; i++)
  4.   {
  5.      if(blablabla)
  6.       {return "a";}
  7.   }
  8. }
this is the example of the code, when i try to compile this application i have error: "missing return statement"....
so i guess i have to write the return statement after the for loop, but than it wouldn't be the same (and it would not consider my IF condition) ! please help me!
Suppose the expression 'blablabla' is never true: the loop completes and then
nothing will be returned. Maybe *you* know that 'blablabla' will always be true
somewhere in that loop but the compiler isn't that smart, i.e. it can't figure that
out without actually executing the code it is compiling.

Sometimes these things happen and you have to add another return "string"
statement at the end of the method as well, although *you* might know that the
code will never be reached.

If, on the other hand 'blablabla' turns out to be always false you also must put
another return "string" statement after the loop.

On the other hand: some folks like the 'one return statement' policy and they'd
break out of the loop after setting a flag or similar if 'blablabla' happens to be true.
At the end of the method they'd test that flag or similar condition and decide
what to return. I find that approach a bit 'mechanic'.

kind regards,

Jos
Nov 25 '07 #2
mike131
12
Suppose the expression 'blablabla' is never true: the loop completes and then
nothing will be returned. Maybe *you* know that 'blablabla' will always be true
somewhere in that loop but the compiler isn't that smart, i.e. it can't figure that
out without actually executing the code it is compiling.

Sometimes these things happen and you have to add another return "string"
statement at the end of the method as well, although *you* might know that the
code will never be reached.

If, on the other hand 'blablabla' turns out to be always false you also must put
another return "string" statement after the loop.

On the other hand: some folks like the 'one return statement' policy and they'd
break out of the loop after setting a flag or similar if 'blablabla' happens to be true.
At the end of the method they'd test that flag or similar condition and decide
what to return. I find that approach a bit 'mechanic'.

kind regards,

Jos
ohoo thank you very much...
by the way what is the meaning of *you*? LOL
Nov 25 '07 #3
JosAH
11,448 Expert 8TB
ohoo thank you very much...
by the way what is the meaning of *you*? LOL
I just wanted to emphasize the word 'you' as in *you* know it but the *compiler* doesn't.

kind regards,

Jos
Nov 25 '07 #4
You can return "null" after the loop, incase the if condition in your loop is not executed.....
Nov 26 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Phil Powell | last post by:
My first time working with a PHP class, and after 6 hours of working out the kinks I am unable to return a value from the class, so now I appeal to the general audience what on earth did I do wrong...
20
by: Jakob Bieling | last post by:
Hi! I am using VC++ 7.1 and have a question about return value optimization. Consider the following code: #include <list> #include <string> struct test {
25
by: cppaddict | last post by:
I'd like to know what goes on under the hood when methods return objects. Eg, I have a simple Point class with two members _x and _y. It's constructor, copy constructor, assignment operator and...
2
by: PengYu.UT | last post by:
I have the following sample program, which can convert function object with 1 argument into function object with 2 arguments. It can also do + between function object of the same type. The last...
2
by: Rhino | last post by:
I am trying to verify that I correctly understand something I saw in the DB2 Information Center. I am running DB2 Personal Edition V8.2.1 on Windows. I came across the following in the Info...
15
by: Greenhorn | last post by:
Hi, when a function doesn't specify a return type ,value what value is returned. In the below programme, the function sample()is returning the value passed to 'k'. sample(int); main() { int...
10
by: Mark Jerde | last post by:
I'm trying to learn the very basics of using an unmanaged C++ DLL from C#. This morning I thought I was getting somewhere, successfully getting back the correct answers to a C++ " int SumArray(int...
12
by: Michael Maes | last post by:
Hello, I have a BaseClass and many Classes which all inherit (directly) from the BaseClass. One of the functions in the BaseClass is to (de)serialize the (inherited) Class to/from disk. ...
3
by: kikazaru | last post by:
Is it possible to return covariant types for virtual methods inherited from a base class using virtual inheritance? I've constructed an example below, which has the following structure: Shape...
6
KoreyAusTex
by: KoreyAusTex | last post by:
If anyone can help me figure out the what the missing return statements are, I think it might be the fact that I need to add a return false in the getValue()? import java.util.*; public class...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.