• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

Using operators and decision constructs

 
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.
 
Saloon Keeper
Posts: 6467
61
Eclipse IDE Firefox Browser MySQL Database VI Editor Java 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.
 
Ranch Hand
Posts: 117
11
Hibernate Netbeans IDE Eclipse IDE Postgres Database Tomcat Server Java
  • 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: 6467
61
Eclipse IDE Firefox Browser MySQL Database VI Editor Java 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: 117
11
Hibernate Netbeans IDE Eclipse IDE Postgres Database Tomcat Server Java
  • 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.
 
Bartender
Posts: 1700
17
  • 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.
 
I'm still in control here. LOOK at this tiny ad!
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!