"j" in the code

Hello,

I am pretty new at writing Arduino code. I have seen in some codes I have downloaded a "j" in it and am wondering what it is and what it does. For example it may be "int j; or

void setup(){
  for (int jj; jj<sizeof(leds)/sizeof(int);jj++){
    pinMode(leds[jj],OUTPUT);
    delay(10);

So I was wondering is this a value or a command or what.

Thanks

Hello

jj is a variable from type INT.

have a look at C++ for loop which describes a simple declaration

1 Like

it should read
for (int jj = 0; ...

otherwise you don't have a known starting value and it can be anything that is on the stack when you use the for loop.

And the double letter versions ii and jj and so forth make it a little easier to find instances amongst all the 'i's floating around in the code.

But used as a loop variable like this, any reference to that actual variable will, it is hoped, be found very nearby as it is only valid within the body of the for statement.

a7

2 Likes

True, it's the proper thing to do (and makes the code more readable), however afaik most if not all modern C++ compilers by default initialise a variable to 0 and that code will run as expected starting the count from 0.

Thanks for the info, very helpful.

Are they, j and jj, used instead of a word like fred or dingledorf because it's less to type, less is better?

can you try this code

void getStuffOnStack(int x, int y, int z) {
  Serial.print(x); Serial.write(' ');
  Serial.print(y); Serial.write(' ');
  Serial.println(z);
}

void setup() {
  Serial.begin(115200);
  for (int i = 0; i < 10; i++) {
    getStuffOnStack(random(-10, 10), random(-100, 100), random(-1000, 1000));
    Serial.println("now an uninitiaized j");
    for (int j; j < 3; j++) {
      Serial.print("j=");  Serial.println(j);
    }
    Serial.println("---------");
  }

  {
    volatile int local[100] = {0}; // attempt to clear the stack
  }

    Serial.println("now an uninitiaized j");
    for (int j; j < 3; j++) {
      Serial.print("j=");  Serial.println(j);
    }
}

void loop() {}

typed here, can't test for the moment

I suspect i was short for index, and j was the logical next letter after i, when we needed a second indexing variables.

Besides, in some neolithic languages, i was the start of integer variables, with other letters connoting different variable types. But, that might be misremembering Cobol, some form of Basic, and other early teachings. Not sure. Z8 basic, maybe?

Could well be. I'm pretty sure it was the practice in my Fortran punch card days. Not that I kept any of that.

I get something like:

3 -51 -193
now an uninitiaized j
---------
2 30 658
now an uninitiaized j
---------
-7 -22 544
now an uninitiaized j
---------
-5 -60 709
now an uninitiaized j
---------
-3 -58 -508
now an uninitiaized j
---------
-1 27 -497
now an uninitiaized j
---------
-7 -88 -160
now an uninitiaized j
---------
7 9 169
now an uninitiaized j
---------
9 33 560
now an uninitiaized j
---------
5 -84 -722
now an uninitiaized j
---------
now an uninitiaized j after the stack was cleared
j=0
j=1
j=2

... and learned a thing. The uninitialized j lines followed by the line of dashed lines indicate that ``j < 3`' was false.

1 Like

In old Fortran variables starting with i to n were default integer variable, the other letters were floating point. Not sure how it is now - it has been decades since I've written Fortran.

3 Likes

It's mostly from ForTran. variables did not need to be pre-defined the way they are in C/C++, and variables that began with I through N would be Integers (The first two letters of "INteger", get it?), and everything else was floats. It wasn't uncommon to see nested loops with "i", "ii", "iii"...
There was even an IMPLICIT statement that allowed you to specify the type of otherwise undeclared variables based on the first letter. IMPLICIT COMPLEX Z would mean that any variable name starting with "Z" would be of type COMPLEX

(I'm not sure if this had roots in any previous language. Fortran was pretty early!)

("i" for "Index" is sort of sensible, and "n" for a "natural number" is ok. IMO using "j" or "jj" is to be avoided; a sign that you're an "old fart" thathused to write Fortran code.)

2 Likes

In my FORTRAN punch card days, the convention was that variable names starting with I-N were implicit _IN_tegers, while the rest of A-H,O-Z names were implicit reals. Which more careful programmers could avoid with IMPLICIT NONE.

1 Like

I seem to remember from Fortran 4 i j k l m and n were by default integer variables
you did not even have to define a variable type just use the identifier which could lead to all sorts of problems - type a variable identifer wrong and there was no compiler warning/error message

2 Likes

I did and this what it printed,

3 -51 -193
now an uninitiaized j
---------
2 aized j
j=0
j=1
j=2
9 33 560
now an uninitiaized j
---------
5 -84 -722
now an uninitiaized j
---------
now an uninitiaized j
j=0
j=1
j=2
3 -51 -193
now an uninitiaized j
---------
2 30 658
now an uninitiaized j
---------
-7 -22 544
now an uninitiaized j
---------
-5 -60 709
now an uninitiaized j
---------
-3 -58 -508
now an uninitiaized j
---------
-1 27 -497
now an uninitiaized j
---------
-7 -88 -160
now an uninitiaized j
---------
7 9 169
now an uninitiaized j
---------
9 33 560
now an uninitiaized j
---------
5 -84 -722
now an uninitiaized j
---------
now an uninitiaized j

So it's clear that in the first case j was getting a value from the stack that was not 0 and in the second case we entered the for loop...

➜ so this is probably not as clear as it may seem

always initialise your variables, period — should be the advice.

2 Likes

Absolutely true.
Interesting test results! I've seen my share of weird effects (usually due to having an array reading out of bounds) but didn't see this one yet.

1 Like

the magic of "random" stuff on the stack :slight_smile:

Like x, y, z are coordinates, i, j, k are counters in nested for loops. Just a convention.
Like driving on the right side of the road (where in some countries the wrong side is right side) ( of course).