1

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?

2
  • Absolutely. Yes, it runs 7 times. Commented Sep 6, 2015 at 9:41
  • Consider accepting my answer. And I provided an explanation why your code seems to be running 7 times. The logic in your edit is mine which made more sense to you. Do you still feel the need of other's opinions on my logic while you understood it without any doubt. Commented Sep 6, 2015 at 10:58

5 Answers 5

3

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).

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1

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.

Comments

0

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>

4 Comments

can you check my answer on this because you answered more or less exactly what I wrote.
@Mr.Robot You are correct, the code I wrote is exactly like yours. Apparently OP didn't read your answer in full and edited the question with similar construct, but didn't get it right. My answer is a response to the edited question, where I explicitly point out the parts OP got wrong. --- Sorry that it appears to supersede your answer, that was not the intention, and I don't see why you still have downvotes, unless the downers didn't get notified (yet) about the updated answer.
No I didn't meant to impose anything on you. I tried to notify the downvoters several times, but didn't got any response.
@Mr.Robot Yeah, I saw that, which is why I've begun calling them downers instead of downvoters, especially for the ones who don't leave a comment explaining why they find the answer "not useful".
0

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();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.