Loop through a sequence

How do I loop through this sequence separated by commas so I can determine the minimum value?

x = (min(splitFloat[0],splitFloat[2],splitFloat[4]);

Thank you.

What do you mean by this sequence?

float[] splitFloat = {0.1, 0.2, 0.3, 0.4, 0.5};
float x = min(splitFloat[0],splitFloat[2],splitFloat[4]);
println(x);
1 Like

I guess now I know what you are asking. It is how the min() function is done internally, isn’t it.

float[] splitFloat = {0.3, 0.2, 0.5, 0.4, 0.1};
float min = splitFloat[0];

void setup() {
  for (int i = 1; i < splitFloat.length; i++) {
    if (splitFloat[i] < min) {
      min = splitFloat[i];
    }
  }
  println(min);
}
2 Likes

Oops, It’s P5.js

var splitFloat = [0.3, 0.2, 0.5, 0.4, 0.1];

let min = splitFloat[0];

function setup() {
  for (let i = 1; i < splitFloat.length; i++) {
    if (splitFloat[i] < min) {
      min = splitFloat[i];
    }
  }
  print(min);
}
1 Like

Thank you very much Mr. Noel, I’ve applied the code you gave me and it worked perfectly. You are such a gem, you open me up to so many possibilities. I truly appreciate, God bless you.

1 Like

Hi Noel, how are you?..is it possible to use ‘‘if statements’’ to draw different instance of ellipses assuming you have to loop through these numbers ; 4,9,7,15,11,23
e.g
if (x1>x2) {
ellipse(((i*50,(i+1)50 2, 2);
}
else if (x1==x2) {
ellipse(((i
50,(i+1)50 4, 4);
}
else(x1<x2) {
ellipse(((i
50,(i+1)*50 7, 7);
}

Please see how you can make this work, I’ve tried and it’s not working for me.
Thank you very much.

Hi @Chigoz
These numbers are related to which variable? (x1, x2 or i)?

ok, it should be i, i+1

You can create different data sets that reflects the different conditions

I think two numbers are ok i,e condition (1) 9 ,7 , condition(2) 6,6 and condition(3) 7,9

I can’t see the rest of the code, so I used some random values. It’s weird of course, but it shows how to loop through the numbers.

var number = [4,9,7,15,11,23];

let x1, x2, i = 0;

function setup() {
  createCanvas(500, 500);
}

function draw() {
  x1 = random(width);
  x2 = random(width);
  if (x1 > x2) {
    fill(255, 0, 0);
    ellipse(number[i]*x1, (number[i]+1)*x2, 30, 30);
  } else if (x1 == x2) {
    fill(0, 0, 255);
    ellipse(number[i]*x1, (number[i]+1)*x2, 10, 10);
  }
   else if(x1 < x2) {
     fill(0, 255, 0);
    ellipse(number[i]*x1, (number[i]+1)*x2, 20, 20);
  }
  i++;
  if(i > number.length-1) i = 0;
}

Thanks, let me apply them.

Hello Noel, how are you?, this is what I’ve done so far on my project. I’ll still add to it as I get more ideas. Tell me what you think. Thank you very much for your help.