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 6757681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:40:59+00:00 2026-05-26T13:40:59+00:00

I have a program that I am trying to understand but there is an

  • 0

I have a program that I am trying to understand but there is an abbreviated code or codes that i don’t understand.
the code is

    double xDistance = x1 - x2 >= 0 ? x1 - x2 : x2 - x1;
    double yDistance = y1 - y2 >= 0 ? y1 - y2 : y2 - y1;

i read in programming books that ?: is an abbreviated form of if…then but where do i put these words in because everytime i insert or replace some code I get a syntax error.

Also are there more than one way to write the following line of code

(xDistance <= (w1 + w2) / 2 && yDistance <= (h1 + h2) / 2)

preferably replacing the && operator

thanks in advance for any assistance given.

  • 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-26T13:41:00+00:00Added an answer on May 26, 2026 at 1:41 pm

    The conditional operator ? : is shorthand for if-else and works as so:

    (condition) ? (return this if condition true) : (return this if condition false)
    

    Therefore

    double xDistance = x1 - x2 >= 0 ? x1 - x2 : x2 - x1;
    

    equates to:

    if (x1 - x2 >= 0)
       xDistance = x1 - x2;
    else
       xDistance = x2 - x1;
    

    The second line:

    double yDistance = y1 - y2 >= 0 ? y1 - y2 : y2 - y1;
    

    is the same thing, equates to:

    if (y1 - y2 >= 0)
       yDistance = y1 - y2;
    else
       yDistance = y2 - y1;
    

    Which is storing the absolute values (ie. distance rather than displacement) in xDistance and yDistance. Can also be replaced with Math.abs(x1 - x2), etc.

    And why do you want to replace the && operator in the following?

    (xDistance <= (w1 + w2) / 2 && yDistance <= (h1 + h2) / 2)
    

    If you really had to you could have (assuming this statement belongs in an if statement):

    if (xDistance <= (w1 + w2) / 2)
       if (yDistance <= (h1 + h2) / 2)
          // do something
    

    EDIT: As mentioned by David in the comments, && is simply a logical AND. ie. for X && Y, the expression is TRUE iff both X is TRUE and Y is TRUE.

    Because of this you can take advantage of short-circuiting, where if the first condition (X) is FALSE, then there is no point in the program evaluating the second (Y) since the expression can never be TRUE.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a c program that is trying to read in a file. Using
I have a program that multiplies 3 numbers and i am trying to understand.
I have a calendar program that I am trying to convert to iPad (using
I have a small program that I'm trying to create to get ip addresses
I have a C++ program that writes to stdout that I am trying to
I have a program that reads input from a file. I am trying to
I am trying to polish up a program a program that I have converted
I have been trying, in vain, to make a program that reads text out
I'm trying to multithread something and i have my program set up such that
I have program that requires Python 3, but I develop Django and it uses

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.