• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using operators and decision constructs  RSS feed

 
Urs Waefler
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the code: It returns the higher value. To me it seems that there is an else missing. I do not understand why it does not always return y.
 
Carey Brown
Saloon Keeper
Posts: 3312
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IF x > y THEN return x
once you return then no further statements in the method are executed. On the other hand, if x is NOT > y then the return x is NOT execute and so processing continues on to the next statement which is return y.
 
João Victor Gomes
Ranch Hand
Posts: 113
11
Eclipse IDE Java Netbeans IDE Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The return statement exits the method. So, let's see what we got here:

Note: even though the if statement has just one line of code, I inserted the braces to make the code more easy to read.

The if statement just tests if the x value is greater than y. There is no test for equal values.
And remember that we can omit the braces when the if statement's block of code has just one line of code, like this:
 
Carey Brown
Saloon Keeper
Posts: 3312
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
João Victor Gomes wrote:The return statement exits the method. So, let's see what we got here:

Please wrap your long lines of comments. Excessively long lines cause other text to be hidden on the right.
 
João Victor Gomes
Ranch Hand
Posts: 113
11
Eclipse IDE Java Netbeans IDE Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:
João Victor Gomes wrote:The return statement exits the method. So, let's see what we got here:

Please wrap your long lines of comments. Excessively long lines cause other text to be hidden on the right.


I know that, sorry. For now, he could copy the code and paste in a file to see it better.
I'll try to improve it on the next time.
 
Mike London
Ranch Hand
Posts: 1505
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Urs Waefler wrote:This is the code: It returns the higher value. To me it seems that there is an else missing. I do not understand why it does not always return y.


One thing is I would recommend is to only have a single return exit point, not two. While it may not seem like a big deal in such a small piece of code, if you get into the habit you may have a long method with lots of those exit points making debugging very difficult.

The reason your code is always returning the larger of the two numbers is your algorithm.

Case 1:
=====
If you pass 1, and 2

1 is not greater than 2, so your code returns 2 from the bottom return statement.

Case 2:
=====
If you pass 2 and 1

2 is greater than 1 so you return 2 from the initial return statement.

Thus, the way you wrote your IF statement, you're guaranteed to always get the larger of the two numbers (like a Math.max()).

Creating a single return int variable, having a single return point, and debugging your code will show you exactly what's going on - though you can still debug (step through) your code as is and see what's happening.

----

HOPE THIS HELPS.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!