Alfred O.
Alfred O.

Reputation: 39

Java For Loop Patterns

Was doing some Java practices and one particular for loop pattern confused me. I was working towards a goal to print this pattern,

123456
12345
1234
123
12
1

And the solution given was

    for(int k = 8; k > 1; k--) {
        for(int l = 1; l < k - 1; l++){
            System.out.print(l);
        }
        System.out.println();
    }

I played with the values but I didn't understand the value of k = 8. wouldn't that mean the loop runs 7 times when k > 1 is true?

edit I played around with the code and found out a lesser, more simplified code that made more sense to me,

    for(int k = 6; k >= 0; k--) {
        for(int l = 1; l < k; l++){
            System.out.print(l);
        }
        System.out.println();
    }

It too gave me the same outcome. Is this way of logic more confusing to people or is it easier to understand?

Upvotes: 1

Views: 779

Answers (5)

Jeet
Jeet

Reputation: 1

Here, l is initialised with 1 and runs till it is less than k-1. When k is 8 the loop will run till l is less than 8-1, i.e, 7 and not till it is equal to 7.Therfore, the inner loop will run 6 times. If you wish, then you can consider the following code segment:

for(int k=6;k>=1;k--)
{
  for(int l=1;l<=k;l++)
   System.out.print(l);
System.out.println();
}

Upvotes: 0

Andreas
Andreas

Reputation: 159086

When running your edit code, you get (<nl> represent a newline):

12345<nl>
1234<nl>
123<nl>
12<nl>
1<nl>
<nl>
<nl>

As you can see, your edit runs 7 times too, with k values 6,5,4,3,2,1,0, and you don't get the number 6 at the end of the first line.

Change to k > 0, and to l <= k:

for(int k = 6; k > 0; k--) {
    for(int l = 1; l <= k; l++){
        System.out.print(l);
    }
    System.out.println();
}

Output

123456<nl>
12345<nl>
1234<nl>
123<nl>
12<nl>
1<nl>

Upvotes: 0

rajan.agarwal
rajan.agarwal

Reputation: 11

I played with the values but I didn't understand the value of k = 8. wouldn't that mean the loop runs 7 times when k > 1 is true?
for(int k = 8; k > 1; k--) { for(int l = 1; l < k - 1; l++){ System.out.print(l); } System.out.println(); }

Hi, Here, as you can see that the outer loop will run till the value of k is 2, the first value of k is 8, so it will run for values - 8,7,6,5,4,3,2. However the integer values are printed in the inner loop, where, the value of l goes from 1 to less than k-1, hence in the first iteration it goes from 1 to 6. The outer loop will run 7 times but value of l is printed only 6 times as l is always one less than k.

Upvotes: 1

syntagma
syntagma

Reputation: 24294

I played with the values but I didn't understand the value of k = 8. wouldn't that mean the loop runs 7 times when k > 1 is true?

I means that the loop will run as long as k > 1 is true but k is also decremented by 1, therefore the loops runs 7 times but in the last run it will only print a newline only (which you have not included in your output but it was there, believe me).

Upvotes: 3

Esty
Esty

Reputation: 1912

Yes. Loop For k will run 7 times. But at the last time when k = 2 then inner loop for l = 1 and l < k - 1 means 1 < 1 will not execute.

Upvotes: 1

Related Questions