Home > Net >  Use loop to print values as requested
Use loop to print values as requested

Time:04-27

print out below table in console using a nested loop

Hi question how do I print out above in console from processing.

    int result;
    for (int i=100 ; i < = 10; i   = 10) { 
       result = 100; 
       for (int k=2; k <= 10; k  ) { 
          result-=10 ; 
       } print(result);
        println();
    }
  • My thought is it looks like a 10*10 table thus both i and k is < = 10.
  • And other thought is when i is odd i starts from 100 and -=10 (i > = 10) when i is even(line 2,4,6...) i start from 10 and =10 ( i<=100) Then I got the printed table.

But I am stuck how to achieve this in code if my logic is correct as mentioned above.

CodePudding user response:

You can do it with 2 nested loops:

for (int i=0; i<10;   i) { 
     for (int j=0; j<10;   j) {
        print(str(i%2 == 0 ? (10-j)*10 : (j 1)*10)   " ");
     }
     println();
}

Or one single loop:

for (int i=0; i<100;   i) { 
     int j = i;
     print(str((i/10)%2 == 0 ? (10-j)*10 : (j 1)*10)   " ");
     if (i % 10 == 9) {
       println();
     }
}

Output:

100 90 80 70 60 50 40 30 20 10 
10 20 30 40 50 60 70 80 90 100 
100 90 80 70 60 50 40 30 20 10 
10 20 30 40 50 60 70 80 90 100 
100 90 80 70 60 50 40 30 20 10 
10 20 30 40 50 60 70 80 90 100 
100 90 80 70 60 50 40 30 20 10 
10 20 30 40 50 60 70 80 90 100 
100 90 80 70 60 50 40 30 20 10 
10 20 30 40 50 60 70 80 90 100

CodePudding user response:

This is a simple example showing that logic in javascript.

It doesn't use the double loop nor smart increments inside the for condition statement, but it just increments an index from 1 to 10 using a condition with the ternary operator (over a flag that gets flipped at each iteration) to output one fashion or the other.

It's not the most elegant solution and it uses multiplications. I'm sure you'll find smartest ways to achieve the same.

let alt = true;
let counter = 0;

//infinite loop
while(true){
  counter  ;
  let row = "";
  for(let i=1; i<=10; i  ){
    row  = (alt) ? (10*i)   ' ' : 110-(10*i)   ' ';
  }
  //output current row
  console.log(row);
  //switch alternate
  alt = !alt;
  //exit at iteration 10
  if(counter == 10) break;
}

CodePudding user response:

Here's yet another variation:

void setup() {
  print(reversingNumberLoop(10, 100, 10, 9));
}

String reversingNumberLoop(int from, int to, int increment, int rows){
  
  String result = "";
  
  String backToFront = "";
  String frontToBack = "";
  
  // loop once writing in both directions
  for(int i = from; i <= to; i = increment){
    frontToBack  = i   " ";
    backToFront  = ((to - i)   from)   " ";
  }
  
  // add line endings
  frontToBack  = "\n";
  backToFront  = "\n";
  // repeat for the number of rows
  for(int i = 0 ; i < rows; i  ){
    result  = i % 2 == 1 ? frontToBack : backToFront;
  }
  
  return result;
}

It's not doing anything too clever: still using % to alternate and just pre-allocates the repeating lines first. This would have two looks in succession, not nested.

  • Related