• 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
  • Liutauras Vilda
  • Paul Clapham
  • paul wheaton
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Rob Spoor
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:

what does inclusive range means in Java?

 
tangara goh
Ranch Hand
Posts: 1013
  • 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 ?
 
Stephan van Hulst
Saloon Keeper
Posts: 15253
349
  • 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.
 
Campbell Ritchie
Marshal
Posts: 78659
374
  • 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: 78659
374
  • 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: 78659
374
  • 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.
 
Mo-om! You're embarassing me! Can you just read a tiny ad like a normal person?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic