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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:23:19+00:00 2026-05-26T07:23:19+00:00

Can someone please explain what’s going on here, please? I’ve simplified as much as

  • 0

Can someone please explain what’s going on here, please?

I’ve simplified as much as I can.

( 1 || 2 == 1 )     is TRUE (as expected)
( 1 || 2 == 2 )     is TRUE (as expected)

I would also expect the following to both be true (but this may suggest a lack of understanding…)

( 1 == ( 1 || 2 ) ) is TRUE  
( 2 == ( 1 || 2 ) ) is FALSE <--- !!! I don't understand this..

Now it starts getting a bit odd…

( 2 == 1 || 2 )     is TRUE  
( 3 == 1 || 2 )     is TRUE  <--- !!! I don't understand this..

After some more playing about, I find the following:

( 1 == ( 1 || 2 ) ) is FALSE
( 2 == ( 1 || 2 ) ) is TRUE

( 1 == ( 1 && 2 ) ) is TRUE
( 2 == ( 1 && 2 ) ) is FALSE

You might have guess that I’m trying to do something along the lines of:

sub foo {
    # ... blah blah ...
    return 0 if $status == ( 2 || 4 || 7 || 9);
    # ... do other stuff ...
    return 1;
}

my $result = foo();
if ($result) { 
    # then do something based on a result
    } else {
    # log failure or whatever
}

I know I can use this idiom, but the extra “$status”(es) seem superfluous:

return 0 if ( $status == 1 || $status == 4 || $status == 7 ); # and so on...

I also know I could use a regex with alternation or to check if the value is in an array of the allowable values.

But I’d like to understand why this is not as I expect.

  • 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-26T07:23:20+00:00Added an answer on May 26, 2026 at 7:23 am
    $status == ( 2 || 4 || 7 || 9)
    

    will not do what you expect. (And if it did do what you expect, what do you think $status == (2 && 4 && 7 && 9) should mean?) That statement is equivalent to

    $status == 2
    

    || and && are binary operators that always return one of their scalar operands. It may help to think of them as equivalent to these binary functions:

    sub OR {
        my ($a,$b) = @_;
        if ($a) {
            return $a;
        } else {
            return $b;
        }
    }
    
    sub AND {
        my ($a,$b) = @_;
        if ($a) {
            return $b;
        } else {
            return $a;
        }
    }
    

    (I’m glossing over the short-circuiting feature of || and &&, but that’s not so important for this post).

    Noting that == has higher precedence than || and &&, we can rewrite all your expressions in terms of AND and OR:

    ( 1 || 2 == 1 )     -->   (OR(1,2==1))  --->  OR(1,0) --> 1  TRUE
    ( 1 || 2 == 2 )     -->   (OR(1,2==2))  --->  OR(1,1) --> 1  TRUE
    

    These two expressions are both true, but NOT for the reason you were expecting.

    ( 1 == ( 1 || 2 ) ) -->   (1 == OR(1,2))   --->   (1==1) TRUE
    ( 2 == ( 1 || 2 ) ) -->   (2 == OR(1,2))   --->   (2==1) FALSE
    
    
    ( 2 == 1 || 2 )     -->   OR(2==1,2)   --->  OR(0,2) -->  2  TRUE  
    ( 3 == 1 || 2 )     -->   OR(3==1,2)   --->  OR(0,3) -->  3  TRUE
    
    
    ( 1 == ( 1 || 2 ) ) -->   (1==OR(1,2)) --->  (1==1)  TRUE, not FALSE
    ( 2 == ( 1 || 2 ) ) -->   (2==OR(1,2)) --->  (2==1)  FALSE, not TRUE
    
    ( 1 == ( 1 && 2 ) ) -->   (1==AND(1,2)) ---> (1==1) TRUE
    ( 2 == ( 1 && 2 ) ) -->   (2==AND(1,2)) ---> (2==1) FALSE
    

    And your original idea of checking

    $status == ( 2 || 4 || 7 || 9)
    

    is equivalent to

    $status == OR(OR(OR(2,4),7),9)
    $status == OR(OR(2,7),9)
    $status == OR(2,9)
    $status == 2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone please explain what is going on here with the Django Tutorial Part
Can someone please explain how to obtain this result from the following array? Here
Can someone please explain what the & does in the following: class TEST {
Can someone please explain me what I am missing here. Based on my basic
Can someone please explain to me why this simple assignment doesn't work. Here is
Can someone please explain what the following javascript statement is doing? var default_hide =
can someone please explain to me the following JavaScript design pattern example and what
Can someone please explain the following code? Source: Arrays.class, public static <T> void sort(T[]
Can someone please explain why this JavaScript code outputs zero instead of one? Also,
Can someone please explain why the following code is not allowed: List<Number> l =

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.