How to define two types of variables in a for-loop

Hi!
Today I leared something like this

size(600, 600);
for (int i = 0, mi = 10, r = 200; i < mi; i++) line(cos(TWO_PI/mi*i)*r+width/2, sin(TWO_PI/mi*i)*r+height/2, cos(TWO_PI/mi*(i+1))*r+width/2, sin(TWO_PI/mi*(i+1))*r+height/2);

is possible. But how would I be able to use

for(int i = 0, r = 10; float c = 3; i < 10; i++) {} 

?

is it even possible?

I know I can just use

float c = 3;
for(int i = 0; i < 10; i++) { }

but I want to do it in a single line (while formatting the code with ctrl + t )

I know I can just do

for(float a = 0, i = 0; i < 10; i++) {
   array[int(a+i)];
}

but it is annoying

In my experience single line code is limited in what it can do. So likelyhood is this isnt possible. The same thing happens when you shorten conditional statements, it only picks up the first subsequent instruction.

but you can still do this:

if(a) println("a"); else if(b) println("b"); else if(c) println("c"); ...

the auto format makes it into several lines but still

Yeah but in your example each condition is followed by only one instruction.

Yeah. But it still is useful sometimes! And you can always use

for(int i = 0; i < 100; i++) for(int j = 0; j < 100; j++) rect(i*scl,j*scl,scl,scl);

I know ive thought the same thing, as the code just looks neater, alas its limited in what it can do.

ie this will only complete the first instruction.

if(true)doSomething(); doSomethingElse(); 

The doSomethingElse wont be called, only the first one. As essentially the brackets serve to close of the instruction and tell the program it needs to move on, however without brackets there is nothing to tell the program that the condition should still be valid so it stop after one instruction. Otherwise unintentional behaviours could creep in. At least i think thats why.

In such case a switch () {} block would be neater than multiple if () {}: :wink:

1 Like