• juliebean@lemm.ee
    link
    fedilink
    English
    arrow-up
    46
    ·
    2 years ago

    what would be the alternative? to always execute if the condition is true, but sometimes execute it even when false, for funsies?

    • General_Effort@lemmy.world
      link
      fedilink
      English
      arrow-up
      5
      ·
      2 years ago

      In normal parlance, “if and only if” rules out that something could also happen as a result of other circumstances. EG, if you fall out of a plane, you will lose your glasses. But there are other conditions that would lead to the same result.

      In code, the alternative would be to have a different if statement that executes identical code. Or *cough* you could use a jump instruction to execute literally the same code.

    • unmagical@lemmy.ml
      link
      fedilink
      English
      arrow-up
      1
      ·
      2 years ago

      Brb, making a truly “if” statement function in my products code base for funsies.

  • dohpaz42@lemmy.world
    link
    fedilink
    English
    arrow-up
    4
    arrow-down
    1
    ·
    2 years ago

    An if statement with one condition is an if and only if statement. The moment you add a second (or more) condition (regardless of && or ||), then it’s no longer if and only if.

  • Steve Dice@sh.itjust.works
    link
    fedilink
    English
    arrow-up
    8
    arrow-down
    5
    ·
    2 years ago

    No, they’re not.

    Let’s assume they are. Let funky function be defined as:

    int funky() {
        a=0
        b=1
        if ( a==1 ) {
            b=1
        }
        return(a)
    }
    

    Since a==1 if, and only if, b=1, in particular a==1 if b=1. We have b=1, therefore a==1. It follows funky will always return 1 but… it doesn’t. QED.