Operatori logici

I valori booleani hanno operatori propri che permettono di ottenere un altro valore booleano a partire da operandi dello stesso tipo.

fill

Gli operandi (A o B) possono essere costituiti anche da espressioni che restituiscono un valore booleano.

// height=160
let Aand;
let andB;
let AandB;
let Aor;
let orB;
let AorB;
let Anot;
let notA;

function setup() {
//    createCanvas(windowWidth,160);
    noCanvas();
//    background(255);

    // AND
    
    Aand = createSpan("false");
    Aand.mouseReleased(boolClicked);
    Aand.style("cursor","pointer")
    Aand.style("color","#a32900")

    andB = createSpan("false");
    andB.mouseReleased(boolClicked);
    andB.style("cursor","pointer")
    andB.style("color","#a32900")

    AandB = createSpan("false");

    // AND
    
    Aor = createSpan("false");
    Aor.mouseReleased(boolClicked);
    Aor.style("cursor","pointer")
    Aor.style("color","#a32900")

    orB = createSpan("false");
    orB.mouseReleased(boolClicked);
    orB.style("cursor","pointer")
    orB.style("color","#a32900")

    AorB = createSpan("false");

    // NOT

    Anot = createSpan("false");
    Anot.mouseReleased(boolClicked);
    Anot.style("cursor","pointer")
    Anot.style("color","#a32900")

    notA = createSpan("true");


    let textDiv = createDiv('');
    textDiv.style("cursor","default");
    textDiv.position(0,0);
    textDiv.style("width", "100%");
    textDiv.style("text-align", "center");
    textDiv.style("font-family", "Roboto Mono" );
    textDiv.style("font-size","24px");
    //textDiv.style("font-weight","bold");

    textDiv.child(Aand);
    textDiv.child(createSpan(" && "));
    textDiv.child(andB);
    textDiv.child(createSpan(" ⟶ "));
    textDiv.child(AandB);

    textDiv.child(createSpan("<br><br>"));

    textDiv.child(Aor);
    textDiv.child(createSpan(" || "));
    textDiv.child(orB);
    textDiv.child(createSpan(" ⟶ "));
    textDiv.child(AorB);

    textDiv.child(createSpan("<br><br>"));

    textDiv.child(createSpan("! "));
    textDiv.child(Anot);
    textDiv.child(createSpan(" ⟶ "));
    textDiv.child(notA);
}

function boolClicked(e) {
  // print("boolClicked")
  //print(e)
  let b = this.html() == "true";
  this.html(!b);

  AandB.html((Aand.html() == "true") && (andB.html() == "true"))
  AorB.html((Aor.html() == "true") || (orB.html() == "true"))
  notA.html(!(Anot.html() == "true"))
}

Provare a cambiare i valori booleani con un clic su quelli evidenziati.