Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 845909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:31:49+00:00 2026-05-15T06:31:49+00:00

i would like to write a simple line of code, without resorting to if

  • 0

i would like to write a simple line of code, without resorting to if statements, that would evaluate whether a number is within a certain range. i can evaluate from 0 – Max by using the modulus.

30 % 90 = 30  //great

however, if the test number is greater than the maximum, using modulus will simply start it at 0 for the remaining, where as i would like to limit it to the maximum if it’s past the maximum

94 % 90 = 4  //i would like answer to be 90

it becomes even more complicated, to me anyway, if i introduce a minimum for the range. for example:

minimum = 10
maximum = 90

therefore, any number i evaluate should be either within range, or the minimum value if it’s below range and the maximum value if it’s above range

-76 should be 10
2 should be 10
30 should be 30
89 should be 89
98 should be 90
23553 should be 90

is it possible to evaluate this with one line of code without using if statements?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T06:31:49+00:00Added an answer on May 15, 2026 at 6:31 am

    Probably the simplest way is to use whatever max and min are available in your language like this:

    max(10, min(number, 90))
    

    In some languages, e.g. Java, JavaScript, and C# (and probably others) max and min are static methods of the Math class.

    I’ve used a clip function to make it easier (this is in JavaScript):

    function clip(min, number, max) {
        return Math.max(min, Math.min(number, max));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to write a function that creates a plot of a set
I would like to write an if statement that would continue to repeat a
I would like to write a query that returns a single date value, that
I would like to write a cross-platform function in C++ that contains system calls.
I would like to write an if statement that will do something base on
I would like to write some Objective-C code for distribution. What is the best
The novice understanding of Node is that if I re-write synchronous, or in-line code,
I would like to write an android app that basically layers an overlay on
I would like to write an abstract base class class func_double_double_t : public unary_function<double,
I would like to write a League Fixture generator in python, but I can't.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.