I would not recommend the use of a switch statement inside the inner loop although it does make sense to keep the string comparisons outside the loop. The drawGrid method below shows how to do this elegantly and efficiently.
// lt -> left top
// rt -> right top
// ld -> left down
// rd -> right down
// Any other value will be treaed as lt
void drawGrid(String str) {
int r1 = 0;
int c1 = 0;
boolean flipY = str.equals("ld") || str.equals("rd");
boolean flipX = str.equals("rt") || str.equals("rd");
for (int r = 0; r < total; r++) {
r1 = flipY ? total - r - 1 : r;
for (int c = 0; c < total; c++) {
c1 = flipX ? total-c-1 : c;
int v = 1 + c1 + r1 * total;
float x = (c + 0.5) * itemSize;
float y = (r + 0.5) * itemSize;
fill(-1);
square(x, y, itemSize * 0.8);
fill(0);
text(v, x, y);
}
}
}
Note I use the ternary operator statements inside the loop like this
c1 = flipX ? total-c-1 : c;
// variable = boolean-condition ? value-if-true : value-if-false;
the ternary statement returns a value based on whether a condition is true so if flipX is true then c1=total-c-1 but if false then c1=c