Nesting "Else If"

Hi there -

I’d like to have a variable number of “else if” statements. I wrote the following code, but it gets a bunch of errors (which I copied and pasted below the code.
Does anyone know how to do this? Thanks!

void setup(){

int sized = 10;
int randomNum = int(random(sized));

if (randomNum==0) {println("zero");};
for (int x = 1;  x<(sized-1); x++){
  else if (randomNum==x) {println(x);}
};
}
Syntax Error - Incomplete statement or extra code near 'extraneous input 'else' expecting {'color', HexColorLiteral, CHAR_LITERAL, 'abstract', 'assert', 'boolean', 'break', 'byte', 'char', 'class', 'continue', 'do', 'double', 'final', 'float', 'for', 'if', 'int', 'interface', 'long', 'new', 'private', 'protected', 'public', 'return', 'short', 'static', 'strictfp', 'super', 'switch', 'synchronized', 'this', 'throw', 'try', 'var', 'void', 'while', DECIMAL_LITERAL, HEX_LITERAL, OCT_LITERAL, BINARY_LITERAL, FLOAT_LITERAL, HEX_FLOAT_LITERAL, BOOL_LITERAL, STRING_LITERAL, 'null', '(', '{', '}', ';', '<', '!', '~', '++', '--', '+', '-', '@', IDENTIFIER}'?

What!? Why !? :slight_smile:
What you are trying to do ?

void setup() {
int sized = 10;
int randomNum = int(random(sized));

  for (int x = 0;  x<sized; x++) {
    if (randomNum == 0) {
       println("zero");
    }
    else if (randomNum==1)  {
      println("one");
    }
   // And so on ...
    else {
      println(x);
    }
  }
}

Or

void setup() {
int sized = 10;
int randomNum = int(random(sized));

  for (int x = 0;  x<sized; x++) {
    switch(x) {
     case 0: 
         println("zero");
     break;
     case 1:
         println("one");
     break;
     // And so on ....
     default:
        println(x);
    }
  }
}

Cheers
— mnse

2 Likes

Hi there, @mnse - it looks like I explained this incorrectly?

Let’s say I have five types of triangles - and a random number will decide which triangle is displayed on the screen. I’d like a way to easily scale this so that I could just as easily have 30 triangles, but I don’t want to write 28 additional “Else If” statements…or 30 “case” statements.

Is there a way to generate multiple statements with a single line?

Thanks!

Do you have a class to represent each 1 of those triangles already?
You could use a HashMap to associate a number to a specific Triangle instance.

import java.util.Map;
final Map<Integer, Triangle> triangles = new HashMap<Integer, Triangle>();

This is very interesting…

I do have a single class to represent the “platonic” triangle… and I was going to send it different value sets to distort its shape.

With the HashMap, it looks like I’m creating a kind of dictionary - the entries have a name, represented in string form - and an integer “value”.

I can then call the entry, by string name, to get or set the value.

But can I use a large array for the value?

We can use any non-primitive datatype (preferably immutable) as a Map container’s key, not just String.

Sure you can! But why not passing that array to your Triangle’s constructor instead?

Hi @panko,

For me the question would be…
Do you really need your triangles predefined (ie. in a Map), or is there an algorithm which you can use to scale you triangles the way you want ?
Made a small example which you can maybe pick up a few usefull things …

Cheers
— mnse

/*Triangle class*/
class Triangle {
  PVector a,b,c;
  
  public Triangle() {
    reset();
  }
  
  // define triangle relative to center origin
  public void reset() {
    a = new PVector(0.0,-0.5);
    b = new PVector(-0.5,0.5);
    c = new PVector(0.5,0.5);
  }
  
  // scale constant
  public void scale(float s) {    
    a.mult(s);
    b.mult(s);
    c.mult(s);
  }

  // scale individually 
  public void scale(int which, float sx, float sy) {
    switch(which) {
      case 0:
        a.x *= sx;
        a.y *= sy;
      break;
      case 1:
        b.x *= sx;
        b.y *= sy;
      break;
      case 2:
        c.x *= sx;
        c.y *= sy;
      break;
      default:
    }
  }
  
  // show the triange + triangle in triangle
  public void show() {
    pushStyle();
    noStroke();
    
    fill(0,0,255);
    beginShape(TRIANGLE);
    vertex(a.x,a.y);
    vertex(b.x,b.y);
    vertex(c.x,c.y);
    endShape(CLOSE);

    fill(255,0,255);
    beginShape(TRIANGLE);    
    // not efficient but do what it should do :)
    vertex(PVector.add(c,b).mult(0.5).x,PVector.add(c,b).mult(0.5).y); 
    vertex(PVector.add(b,a).mult(0.5).x,PVector.add(b,a).mult(0.5).y);
    vertex(PVector.add(c,a).mult(0.5).x,PVector.add(c,a).mult(0.5).y);
    endShape(CLOSE);
    
    popStyle();
  }
}

/*Main Sketch*/
Triangle myTriangle;

void setup() {
  size(600, 600, P2D);
  myTriangle = new Triangle(); 
  frameRate(3);
}

void draw() {
  background(0);
  translate(width/2,height/2); // show it at center origin
  myTriangle.reset(); // reset the triangle to default
  if (frameCount < 15) {
    myTriangle.scale(random(100,600)); // scale whole triangle 
  }
  else {
    myTriangle.scale(0,random(100,600),random(100,600)); // move Point a of triangle by scaling individually 
    myTriangle.scale(1,random(100,600),random(100,600)); // move Point b of triangle by scaling individually
    myTriangle.scale(2,random(100,600),random(100,600)); // move Point c of triangle by scaling individually
  }
  myTriangle.show();
}

tri_example

2 Likes