This week's book giveaway is in the Testing forum.
We're giving away four copies of Data Structures the Fun Way: An Amusing Adventure with Coffee-Filled Examples and have Jeremy Kubica on-line!
See this thread for details.
  • 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

what does inclusive range means in Java?

 
Ranch Hand
Posts: 929
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,

I refer to this question in Hackerrank :

https://www.hackerrank.com/challenges/java-if-else/problem?isFullScreen=true

there is this part that said



so, the solution :



the part on why A < 5 and not A <= 5 stumbled me...why < 5 and not <=5 ? I am confused what a range is

And I google alot but can't find any topic on it.

Hope someone can let me know why not <=5 i mean =5 is inclusive of 5 right ?
 
Saloon Keeper
Posts: 14497
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The terms "inclusive" and "exclusive" are qualifiers that apply to the endpoints of a range. "Inclusive" means that the number that specifies an endpoint of the range is included in that range. "Exclusive" means that a number is not included.

The "inclusive range of 2 to 5" means that the following integers are in the range: 2, 3, 4, 5. In mathematical notation, we write such a range as [2, 5] (using square brackets).

If the bounds of the range were exclusive, only the integers 3 and 4 would be in range. In mathematical notation, such a range would be written (2, 5) (using parentheses).
'
Note that the bounds of a range can have differing qualifiers. "The range of 2 (inclusive) to 5 (exclusive)" means the integers 2, 3 and 4 are in range. This is written [2, 5).

In Java, it is convention that the left bound of a range is inclusive and that the right bound is exclusive, so "from 2 to 5" usually means that the 2 is included and the  5 is excluded. For this reason, the assignment explicitly specifies that the 5 is included as well.
 
Marshal
Posts: 76852
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Most ranges in computer sciences are defined on the inclusive‑exclusive basis. Look at this method. Look at the names of the method parameters, which aree particularly helpful in this case. And now look at this method, which is subtly different.
On the inclusive‑exclusive basis, 0...10 includes 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9.
On the inclusive basis, 0...10 includes 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, and 10.
 
Campbell Ritchie
Marshal
Posts: 76852
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What will happen if you enter 6? Look at your line 2.
 
Campbell Ritchie
Marshal
Posts: 76852
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the question you quoted was poorly worded, and I also think it is possible to write a much simpler solution that will fulfill all its requirements. I think the question is less about writing if‑elses than about Boolean algebra.
Challenge: find the shortest and simplest solution.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic