Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I don't know how to address the question properly to even google it so far. In a nested loop such as this

for (int i=0;i>0;i++)
{
   for(int k=0;k<0;k++)
   {
   }
}

What kind of applications would there be if we use k

I have this question because I wanted to make a loop which iterates like star printing with * char printing left triangle but it iterates on a 2 dimensional matrix as the cursor moves it iterates on the array items such as this

a[0][0]
a[1][0], a[1][1]
a[2][0], a[2][1], a[2][2]
a[3][0], a[3][1], a[3][2], a[3][3]

I want to figure out a for loop or something to be able to iterate the array such as this. What do you suggest?

share|improve this question
2  
That loop is unuseful, since when i = 0 the condition i > 0 will be false, so it never enters the body of the for. –  Christian Jan 18 '14 at 20:15
    
You need to fix your loop end conditions there. Your first loop will never start as it is written, and the inner loop will never finish. –  Teresa Carrigan Jan 18 '14 at 20:17
    
Thank you for quick reply. i know that loop is not useful i just wrote that to ask that dynamic variable instead of value. What would you suggest for what i aim to achieve? –  photonist Jan 18 '14 at 20:18

3 Answers 3

up vote 3 down vote accepted

You must change the first for condition, because when i = 0 the condition i > 0 is false, so it never enters the loop.

Note that when you go line be line, the k must iterate in this pattern: [0, 01, 012, 0123] while i in [0, 1, 2, 3]. In other words, k must iterate until it reaches the value of i, so the condition of the nested for must be k < i + 1.

for (int i = 0; i < 4; i++) {
    for (int k = 0; k < i + 1; k++) {
        // Here you should access to the array
        // array[i][k]
        System.out.print(i + " " + k + " - "); // [DEBUG]
    }
    System.out.println(); // [DEBUG]
}

Output: Just to see indexes

0 0 - 
1 0 - 1 1 - 
2 0 - 2 1 - 2 2 - 
3 0 - 3 1 - 3 2 - 3 3 - 
share|improve this answer
    
Why the downvote? Feedback please. –  Christian Jan 18 '14 at 20:23
    
thank you so much for your answer! Pretty explanatory!I understood what you are saying. So simple i am surprised how i couldn't see that. I have another question. Is there any application of using variable/dynamic condition value instead of static in any usage of for loop? –  photonist Jan 18 '14 at 20:32
    
What do you mean by variable/dynamic condition? Do you mean the condition of form i < some_variable? –  Christian Jan 18 '14 at 20:36
    
Oh i didnt downvote. I tried upvoting but it requires 15 reputations. Apologies i will upvote as soon as i get the points. I am new to the site. –  photonist Jan 18 '14 at 20:37
    
Yes, some increasing/decreasing variable. But i guess there isnt any reasonable usage area for that –  photonist Jan 18 '14 at 20:37

It looks like what you want is something like

for (int i = 0; i < SOME_LIMIT; ++i) {
   for (int k = 0; k <= i; ++k) {
      do_something_with (a[i][k]);
   }
}
share|improve this answer

It looks like you've already done the hard part -- designing the basic premise of the application and writing down on paper what you'd like to do.

Now all you do is take what you've written down and translate it in to code.

I'll give you one hint. On the inside loop, for(int k=0;k<0;k++), thibnk about how you would change this in order to achieve the behavior you're looking for.

Keep at it, you'll get there. The hard part is already done.

share|improve this answer
    
Thank you for your motivating answer! I am surprised how easy it is, i just wrote a nested for loop and mistakenly wrote the greater operator wrong. I just wanted to make a point to ask something else on that. Thank you for motivating also :) –  photonist Jan 18 '14 at 20:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.