If-lause (TAI-operaattori)



Tämä simulaatio havainnollistaa if-lausetta ja TAI-operaattoria. Kopioi alla oleva koodi ja aja ohjelma. Älä tee koodiin muutoksia.

// Ohjelman tehnyt e-Oppi Oy
// 30.1.2018
int a = 470; // Laskurin alkuarvo 
int b = 240; // Laskurin alkuarvo 
int c = 695; // Laskurin alkuarvo 
int d = 600; // Laskurin alkuarvo 
int e = 170; // Laskurin alkuarvo 
void setup () {
     size(1000,700); // ikkunan koko
     stroke(0); // Musta viivan väri
     textSize(30); // Tekstin koko
}

void draw() {
     background(255); // Tyhjennä tausta
     int a2 = round(map(a,0,950,-10,10));
     int b2 = round(map(b,0,950, -10,10));
     int c2 = round(map(c,0,950,0,5));
     String [] c2a = {">",">=","==","<=","<","!="};
     int d2 = round(map(d,0,950, -10,10));
     int e2 = round(map(e,0,950,0,5));
     String [] e2a = {">",">=","==","<=","<","!="};
     strokeWeight(1);
     fill(255);
     rect(500,200,500,500);
     stroke(200);
     for (int t=1; t <= 10; t++) {
        line(500+t*50,200,500+t*50,700);
        line(500,200+t*50,1000,200+t*50);
     }
     fill(0);
     textSize(30);
     stroke(0);
     
     if ((c2a[c2] == ">") && (e2a[e2] == "<")) {
        if ((a2 > b2) || (a2 < d2)){
           text("Ehto on tosi",500+50,200+100);
        } else {
           text("Ehto on epätosi",500+50,200+200);
        }
     }
     if ((c2a[c2] == ">") && (e2a[e2] == "<=")) {
        if ((a2 > b2) || (a2 <= d2)){
           text("Ehto on tosi",500+50,200+100);
        } else {
           text("Ehto on epätosi",500+50,200+200);
        }
     }
     if ((c2a[c2] == ">") && (e2a[e2] == ">")) {
        if ((a2 > b2) || (a2 > d2)){
           text("Ehto on tosi",500+50,200+100);
        } else {
           text("Ehto on epätosi",500+50,200+200);
        }
     }
     if ((c2a[c2] == ">") && (e2a[e2] == ">=")) {
        if ((a2 > b2) || (a2 >= d2)){
           text("Ehto on tosi",500+50,200+100);
        } else {
           text("Ehto on epätosi",500+50,200+200);
        }
     }
      if ((c2a[c2] == ">") && (e2a[e2] == "==")) {
        if ((a2 > b2) || (a2 == d2)){
           text("Ehto on tosi",500+50,200+100);
        } else {
           text("Ehto on epätosi",500+50,200+200);
        }
     }
      if ((c2a[c2] == ">") && (e2a[e2] == "!=")) {
        if ((a2 > b2) || (a2 != d2)){
           text("Ehto on tosi",500+50,200+100);
        } else {
           text("Ehto on epätosi",500+50,200+200);
        }
     }
     
      if ((c2a[c2] == ">=") && (e2a[e2] == "<")) {
        if ((a2 >= b2) || (a2 < d2)){
           text("Ehto on tosi",500+50,200+100);
        } else {
           text("Ehto on epätosi",500+50,200+200);
        }
     }
     if ((c2a[c2] == ">=") && (e2a[e2] == "<=")) {
        if ((a2 >= b2) || (a2 <= d2)){
           text("Ehto on tosi",500+50,200+100);
        } else {
           text("Ehto on epätosi",500+50,200+200);
        }
     }
     if ((c2a[c2] == ">=") && (e2a[e2] == ">")) {
        if ((a2 >= b2) || (a2 > d2)){
           text("Ehto on tosi",500+50,200+100);
        } else {
           text("Ehto on epätosi",500+50,200+200);
        }
     }
     if ((c2a[c2] == ">=") && (e2a[e2] == ">=")) {
        if ((a2 >= b2) || (a2 >= d2)){
           text("Ehto on tosi",500+50,200+100);
        } else {
           text("Ehto on epätosi",500+50,200+200);
        }
     }
      if ((c2a[c2] == ">=") && (e2a[e2] == "==")) {
        if ((a2 >= b2) || (a2 == d2)){
           text("Ehto on tosi",500+50,200+100);
        } else {
           text("Ehto on epätosi",500+50,200+200);
        }
     }
      if ((c2a[c2] == ">=") && (e2a[e2] == "!=")) {
        if ((a2 >= b2) || (a2 != d2)){
           text("Ehto on tosi",500+50,200+100);
        } else {
           text("Ehto on epätosi",500+50,200+200);
        }
     }
      
      if ((c2a[c2] == "<") && (e2a[e2] == "<")) {
        if ((a2 < b2) || (a2 < d2)){
           text("Ehto on tosi",500+50,200+100);
        } else {
           text("Ehto on epätosi",500+50,200+200);
        }
     }
     if ((c2a[c2] == "<") && (e2a[e2] == "<=")) {
        if ((a2 < b2) || (a2 <= d2)){
           text("Ehto on tosi",500+50,200+100);
        } else {
           text("Ehto on epätosi",500+50,200+200);
        }
     }
     if ((c2a[c2] == "<") && (e2a[e2] == ">")) {
        if ((a2 < b2) || (a2 > d2)){
           text("Ehto on tosi",500+50,200+100);
        } else {
           text("Ehto on epätosi",500+50,200+200);
        }
     }
     if ((c2a[c2] == "<") && (e2a[e2] == ">=")) {
        if ((a2 < b2) || (a2 >= d2)){
           text("Ehto on tosi",500+50,200+100);
        } else {
           text("Ehto on epätosi",500+50,200+200);
        }
     }
      if ((c2a[c2] == "<") && (e2a[e2] == "==")) {
        if ((a2 < b2) || (a2 == d2)){
           text("Ehto on tosi",500+50,200+100);
        } else {
           text("Ehto on epätosi",500+50,200+200);
        }
     }
      if ((c2a[c2] == "<") && (e2a[e2] == "!=")) {
        if ((a2 < b2) || (a2 != d2)){
           text("Ehto on tosi",500+50,200+100);
        } else {
           text("Ehto on epätosi",500+50,200+200);
        }
     }
     
      
      if ((c2a[c2] == "<=") && (e2a[e2] == "<")) {
        if ((a2 <= b2) || (a2 < d2)){
           text("Ehto on tosi",500+50,200+100);
        } else {
           text("Ehto on epätosi",500+50,200+200);
        }
     }
     if ((c2a[c2] == "<=") && (e2a[e2] == "<=")) {
        if ((a2 <= b2) || (a2 <= d2)){
           text("Ehto on tosi",500+50,200+100);
        } else {
           text("Ehto on epätosi",500+50,200+200);
        }
     }
     if ((c2a[c2] == "<=") && (e2a[e2] == ">")) {
        if ((a2 <= b2) || (a2 > d2)){
           text("Ehto on tosi",500+50,200+100);
        } else {
           text("Ehto on epätosi",500+50,200+200);
        }
     }
     if ((c2a[c2] == "<=") && (e2a[e2] == ">=")) {
        if ((a2 <= b2) || (a2 >= d2)){
           text("Ehto on tosi",500+50,200+100);
        } else {
           text("Ehto on epätosi",500+50,200+200);
        }
     }
      if ((c2a[c2] == "<=") && (e2a[e2] == "==")) {
        if ((a2 <= b2) || (a2 == d2)){
           text("Ehto on tosi",500+50,200+100);
        } else {
           text("Ehto on epätosi",500+50,200+200);
        }
     }
      if ((c2a[c2] == "<=") && (e2a[e2] == "!=")) {
        if ((a2 <= b2) || (a2 != d2)){
           text("Ehto on tosi",500+50,200+100);
        } else {
           text("Ehto on epätosi",500+50,200+200);
        }
     }
     
      
      if ((c2a[c2] == "==") && (e2a[e2] == "<")) {
        if ((a2 == b2) || (a2 < d2)){
           text("Ehto on tosi",500+50,200+100);
        } else {
           text("Ehto on epätosi",500+50,200+200);
        }
     }
     if ((c2a[c2] == "==") && (e2a[e2] == "<=")) {
        if ((a2 == b2) || (a2 <= d2)){
           text("Ehto on tosi",500+50,200+100);
        } else {
           text("Ehto on epätosi",500+50,200+200);
        }
     }
     if ((c2a[c2] == "==") && (e2a[e2] == ">")) {
        if ((a2 == b2) || (a2 > d2)){
           text("Ehto on tosi",500+50,200+100);
        } else {
           text("Ehto on epätosi",500+50,200+200);
        }
     }
     if ((c2a[c2] == "==") && (e2a[e2] == ">=")) {
        if ((a2 == b2) || (a2 >= d2)){
           text("Ehto on tosi",500+50,200+100);
        } else {
           text("Ehto on epätosi",500+50,200+200);
        }
     }
      if ((c2a[c2] == "==") && (e2a[e2] == "==")) {
        if ((a2 == b2) || (a2 == d2)){
           text("Ehto on tosi",500+50,200+100);
        } else {
           text("Ehto on epätosi",500+50,200+200);
        }
     }
      if ((c2a[c2] == "==") && (e2a[e2] == "!=")) {
        if ((a2 == b2) || (a2 != d2)){
           text("Ehto on tosi",500+50,200+100);
        } else {
           text("Ehto on epätosi",500+50,200+200);
        }
     }
     
      
      if ((c2a[c2] == "!=") && (e2a[e2] == "<")) {
        if ((a2 != b2) || (a2 < d2)){
           text("Ehto on tosi",500+50,200+100);
        } else {
           text("Ehto on epätosi",500+50,200+200);
        }
     }
     if ((c2a[c2] == "!=") && (e2a[e2] == "<=")) {
        if ((a2 != b2) || (a2 <= d2)){
           text("Ehto on tosi",500+50,200+100);
        } else {
           text("Ehto on epätosi",500+50,200+200);
        }
     }
     if ((c2a[c2] == "!=") && (e2a[e2] == ">")) {
        if ((a2 != b2) || (a2 > d2)){
           text("Ehto on tosi",500+50,200+100);
        } else {
           text("Ehto on epätosi",500+50,200+200);
        }
     }
     if ((c2a[c2] == "!=") && (e2a[e2] == ">=")) {
        if ((a2 != b2) || (a2 >= d2)){
           text("Ehto on tosi",500+50,200+100);
        } else {
           text("Ehto on epätosi",500+50,200+200);
        }
     }
      if ((c2a[c2] != "<") && (e2a[e2] == "==")) {
        if ((a2 != b2) || (a2 == d2)){
           text("Ehto on tosi",500+50,200+100);
        } else {
           text("Ehto on epätosi",500+50,200+200);
        }
     }
      if ((c2a[c2] != "<") && (e2a[e2] == "!=")) {
        if ((a2 != b2) || (a2 != d2)){
           text("Ehto on tosi",500+50,200+100);
        } else {
           text("Ehto on epätosi",500+50,200+200);
        }
     }
     fill(240);
     noStroke();
     rect(0,200,500,500);
     rect(0,0,1000,200);
  
     stroke(0);  
     strokeWeight(1);
  
     fill(255,255,0); // Keltainen täyttöväri
     rect(0,0,999,30); // Piirrä keltainen suorakulmio
     rect(0,30,999,30); // Piirrä keltainen suorakulmio
     rect(0,60,999,30); // Piirrä keltainen suorakulmio
     rect(0,90,999,30); // Piirrä keltainen suorakulmio
     rect(0,120,999,30); // Piirrä keltainen suorakulmio
     
     fill(255,0,0); // Punainen täyttöväri
     rect(a,0,50,30); // Piirrä punainen neliö
     rect(b,30,50,30); // Piirrä punainen neliö
     rect(c,60,50,30); // Piirrä punainen neliö
     rect(d,90,50,30); // Piirrä punainen neliö
     rect(e,120,50,30); // Piirrä punainen neliö
     int x1 = mouseX; // Selvitä hiiren vaakakoordinaatti
     int y1 = mouseY;
     if (mousePressed == true) {
      if ((y1 > 0) && (y1 < 30)) {
          if ((x1>a) && (a < 950)) { // Jos hiiri on oikealla puolen
                a++; // niin kasvata laskuria
          }
          if ((x1<a) && (a > 0)) { // Jos hiiri on vasemmalla puolen
                a--; // niin pienennä laskuria
          }
      }
      if ((y1 > 30) && (y1 < 60)) {
          if ((x1>b) && (b < 950)) { // Jos hiiri on oikealla puolen
                b++; // niin kasvata laskuria
          }
          if ((x1<b) && (b > 0)) { // Jos hiiri on vasemmalla puolen
                b--; // niin pienennä laskuria
          }
      }
      if ((y1 > 60) && (y1 < 90)) {
          if ((x1>c) && (c < 950)) { // Jos hiiri on oikealla puolen
                c++; // niin kasvata laskuria
          }
          if ((x1<c) && (c > 0)){ // Jos hiiri on vasemmalla puolen
                c--; // niin pienennä laskuria
          }
      }
      if ((y1 > 90) && (y1 < 120)) {
          if ((x1>d) && (d < 950)) { // Jos hiiri on oikealla puolen
                d++; // niin kasvata laskuria
          }
          if ((x1<d) && (d > 0)) { // Jos hiiri on vasemmalla puolen
                d--; // niin pienennä laskuria
          }
      }
      if ((y1 > 120) && (y1 < 150)) {
          if ((x1>e) && (e < 950)) { // Jos hiiri on oikealla puolen
                e++; // niin kasvata laskuria
          }
          if ((x1<e) && (e > 0)){ // Jos hiiri on vasemmalla puolen
                e--; // niin pienennä laskuria
          }
      }
     }
     fill(0); // Musta tekstin väri
     textSize(20); // Tekstin koko
     text(a2,a+5,25);
     text(b2,b+5,55);
     text(c2a[c2],c+10,85);
     text(d2,d+5,115);
     text(e2a[e2],e+10,145);
      
     textSize(25); // Tekstin koko
     text("void setup () {",10,240);
     text("size(500,500); ",50,270);
     text("textSize(30);",50,300);
     text("background(255);",50,330);
     text("fill(0);",50,360);
     text("}",10,390);
     text("void draw () {",10,420);
     text("int x = "+a2+";",50,450);
     text("if ((x "+c2a[c2]+" "+b2+") || (x "+e2a[e2]+" "+d2+")) {",50,480);
     text("text(\"Ehto on tosi\",50,100);",90,510);
     text("} else {",50,540);
     text("text(\"Ehto on epätosi\",50,200);",90,570);
     text("}",50,600);
     text("}",10,630);
     strokeWeight(5);
     line(100,170,940,170);
     triangle(930,157,930,183,948,170);
     for (int r=0; r <= 20; r++) {
       line(100+r*40,165,100+r*40,175);
     }
     for (int r=0; r <= 9; r++) {
       textSize(20);
       text(-10+r,83+r*40,195);
     }
     for (int r=0; r <= 10; r++) {
       textSize(20);
       text(r,493+r*40,195);
     }
     stroke(255,0,0);
     if (c2a[c2] == ">") {
               noFill();
               ellipse(100+40*(10+b2),170,20,20);
               line(110+40*(10+b2),170,900,170);
     }
     if (c2a[c2] == ">=") {
               fill(255,0,255);
               ellipse(100+40*(10+b2),170,20,20);
               line(110+40*(10+b2),170,900,170);
     }
     if (c2a[c2] == "<") {
               noFill();
               ellipse(100+40*(10+b2),170,20,20);
               line(100,170,90+40*(10+b2),170);
     }
     if (c2a[c2] == "<=") {
               fill(255,0,255);
               ellipse(100+40*(10+b2),170,20,20);
               line(100,170,90+40*(10+b2),170);
     }
     if (c2a[c2] == "==") {
               fill(255,0,255);
               ellipse(100+40*(10+b2),170,20,20);
     }
     if (c2a[c2] == "!=") {
               noFill();
               ellipse(100+40*(10+b2),170,20,20);
               line(900,170,110+40*(10+b2),170);
               line(100,170,90+40*(10+b2),170);
     }
     
     if (e2a[e2] == ">") {
               noFill();
               ellipse(100+40*(10+d2),170,20,20);
               line(110+40*(10+d2),170,900,170);
     }
     if (e2a[e2] == ">=") {
               fill(255,0,255);
               ellipse(100+40*(10+d2),170,20,20);
               line(110+40*(10+d2),170,900,170);
     }
     if (e2a[e2] == "<") {
               noFill();
               ellipse(100+40*(10+d2),170,20,20);
               line(100,170,90+40*(10+d2),170);
     }
     if (e2a[e2] == "<=") {
               fill(255,0,255);
               ellipse(100+40*(10+d2),170,20,20);
               line(100,170,90+40*(10+d2),170);
     }
     if (e2a[e2] == "==") {
               fill(255,0,255);
               ellipse(100+40*(10+d2),170,20,20);
     }
     if (e2a[e2] == "!=") {
               noFill();
               ellipse(100+40*(10+d2),170,20,20);
               line(100,170,90+40*(10+d2),170);
               line(900,170,110+40*(10+d2),170);
     }
      stroke(0,0,255);
      line(100+40*(10+a2),155,100+40*(10+a2),175);
      stroke(0);
}