• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

comparison operators

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whats the difference between = and =>
Can anybody help please?
e.g
int aNum = 3;



Don't they do the same thing?
Its just the behaviour of the operators that is puzzling me.
 
Saloon Keeper
Posts: 8220
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Martin Gerard wrote:Whats the difference between = and =>
Can anybody help please?
e.g
int aNum = 3;



Its just the behaviour of the operators that is puzzling me.


"<" is "Less Than"
"<=" is "Less Than or Equal"

So, if aNum is 5, then your first loop counts: 0, 1, 2, 3, 4. Your second loop counts: 0, 1, 2, 3, 4, 5.

Note that your first loop iterates 5 times through the loop but your second one iterates 6 times through the loop because it starts at zero.

The first loop is the preferred usage in Java unless there is a specific need to do it another way. Note that writing a loop any other way but the first example will slow down the comprehension of the human reader because they will have to take the time to understand why it was written differently.
 
Marshal
Posts: 72913
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Martin Gerard wrote:Whats the difference between = and => . . . .

Carey has answered the question you thought you asked. There is no => operator in Java®. The logical and inequaloty operators need care because many people seem to get them wrong.
 
Martin Gerard
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
aah a syntax error. oops
 
Campbell Ritchie
Marshal
Posts: 72913
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Syntax errors like that aren't serious; they won't get past the compiler. The following loops and selection statements contain errors, but not all of them, Some are more serious than others.All the errors should be obvious; I have not obfuscated anything.
 
Saloon Keeper
Posts: 4497
166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:(...)
The first loop is the preferred usage in Java unless there is a specific need to do it another way.


If I use the index as an index into some structure, I use 0 and <. But when used as a counter, I go from 1 to the end <=, since I like to count from 1 to say 10, and not from 0 to 9. But that is of course very personal.
 
Campbell Ritchie
Marshal
Posts: 72913
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is even more interesting when you have got used to 0‑based numbers and try writing loops on a backend using 1‑based array indices, etc.
 
Piet Souris
Saloon Keeper
Posts: 4497
166
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At HackerRank they like to tease one with 1-based arrays....  nightmare.
 
Ranch Foreman
Posts: 408
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:At HackerRank they like to tease one with 1-based arrays....  nightmare.



Or they are just trying to get more people to use FORTRAN or Julia?

Seriously, you do need to watch out for those questions.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic