Jump to content




Recent Status Updates

  • Photo
      18 Aug
    KodeKool

    When faced with a wall of errors and no hope to fix them, remember the following "Programs always do what you tell them to, and seldom what you want them to, but eventually you'll run out of things that can go wrong and it'll just work. and that's the secret to good programming."

    Show comments (2)
View All Updates

Developed by Kemal Taskin
Photo
- - - - -

Nested For Loop [EXAMPLES!]

nested loop for loop loop

  • Please log in to reply
116 replies to this topic

#109 lespauled

lespauled

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1,174 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash

Posted 03 July 2014 - 08:57 AM

Loops are very simple, once you get the pattern.  Just look at the pattern for the items and the answer will become crystal clear.  If you learn to find the pattern on your own, you will have a valuable skill that will never leave you.  If someone else tells you how to do it, you'll miss out on developing this essential skill, not only in programming, but in life.

 

Look at the first line and determine the pattern.  That's your first pattern.  Then look at the following lines and determine how they relate to the first line (and also each other).  That's you're second pattern.

 

Then you simply write a couple of loops that fulfill those patterns.

 

Your loops will be "for" loops.

 

HINT:  the 3rd condition of the for statement can be any value, even negatives.


  • 0
My Blog: http://forum.codecal...699-blog-77241/
"Women and Music: I'm always amazed by other people's choices." - David Lee Roth

#110 XB23

XB23

    CC Newcomer

  • Member
  • PipPip
  • 21 posts

Posted 03 July 2014 - 09:18 AM

Loops are very simple, once you get the pattern.  Just look at the pattern for the items and the answer will become crystal clear.  If you learn to find the pattern on your own, you will have a valuable skill that will never leave you.  If someone else tells you how to do it, you'll miss out on developing this essential skill, not only in programming, but in life.
 
Look at the first line and determine the pattern.  That's your first pattern.  Then look at the following lines and determine how they relate to the first line (and also each other).  That's you're second pattern.
 
Then you simply write a couple of loops that fulfill those patterns.
 
Your loops will be "for" loops.
 
HINT:  the 3rd condition of the for statement can be any value, even negatives.


It's my first time programming, so it's a challenge.


Can you teach me, step my step, how you did that?

My effort was quite poor:
 

for (int i = 0; i < 6; i++){
  
  for (int j = 0; j < 6-i; j++){
    print("-");
  }
  
  
  for (int j = 0; j < (2*i)+1;j++){
    
    print("*");
  }
  
    for (int j = 0; j < 6-i; j++){
    print("-");
  }
  
  println();
}

int num = 13;

for (int i = 0; i < 7; i++){
  
   for (int j = 1; j < i+1; j++){
    
    print("-");
  }
  
  for (int j = 0; j < num; j++){
    
    print("*");
   
    
  }
  
   for (int j = 1; j < i+1; j++){
    
    print("-");
  }
  
  println();
   num = num - 2;
}

println();
Anyone know how the following is done:

2 4 6 8
4 6 8
6 8 
8


for (int i = 0; i < 4; i++){
  
  for (int j = 0 ; j < 4-i; j++){
    
    print(2*i+2*j+2 + " ");
  }
  println();
}

Edited by XB23, 03 July 2014 - 09:12 AM.

  • 0

#111 lespauled

lespauled

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1,174 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash

Posted 03 July 2014 - 10:12 AM

As I stated in my last email, you can use any initializer  as well and any increment.

 

So, this will give you your first line:

 

for( int i = 2; i < 10; i = i + 2)

 

From there, you have to figure out the pattern for the rest


  • 0
My Blog: http://forum.codecal...699-blog-77241/
"Women and Music: I'm always amazed by other people's choices." - David Lee Roth

#112 XB23

XB23

    CC Newcomer

  • Member
  • PipPip
  • 21 posts

Posted 03 July 2014 - 10:41 AM

I've looked at the 9 pages of this thread, and didn't find within it, how to approach a problem like this. The pattern I see is it goes 2 up each time the outer & inner loop iterates. But I couldn't translate that into a for loop.


  • 0

#113 Ritwik I the programmer

Ritwik I the programmer

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 241 posts
  • Programming Language:Java
  • Learning:Java, C++, Python

Posted 04 July 2014 - 08:26 PM

There are infinite possible pattern variations that can be set as assignments, so it is not very surprising that you can't find the exact solutions here. As I see, you are currently worrying about 3 patterns

 

1.

1
2, 1
1, 2, 3
4, 3, 2, 1
1, 2, 3, 4, 5

2.

-----*-----
----***----
---*****---
--*******--
-*********-
***********
-*********-
--*******--
---*****---
----***----
-----*-----

3.

2, 4, 6, 8
4, 6, 8
6, 8
8

First, 2 things that are not about the patterns:

1.To avoid the comma on the last item, check to see if this is the last iteration. For eg.

for(int i = 0; i < 10; i++)

In the above code, the loop body is executed for the last time when i = 9. So:

for(int i = 0; i < 10; i++)
{
    //Code for printing pattern
    if(i != 9)
        System.out.print(",")
}

2.Try using tabs instead of space when printing patterns. It simplifies the spacing concerns when there are 2-3 digit numbers in the pattern.

 

Now about the patterns:

 

1.If a pattern seems too complex on the whole, break it down into parts. For example, the diamond pattern. Break it down into the upper and lower triangle. Then, thing how to print the dashes and stars with separate inner loops. Count the number of dashes and stars on each line and see what separate patterns they follow. Then bring it together.

 

2.It seems you are having trouble thinking about loops that decrement loop variables. For example, see this one

4, 3, 2, 1
3, 2, 1
2, 1,
1

Here's the code for this:

for(int i = 4; i >= 1; i--)
{
    for(int j = i; j >= 1; j--)
    {
        System.out.print(j);
        if(j != 1)
            System.out.print(",");
    }
    System.out.println();
}

Now try thinking about how you would do the 3rd problem.

 

3.Now, again, try breaking down the 1st problem. As you see, the odd and even lines follow different patterns. What patterns? How can you implement them separately using inner loops?

 

Hint:You need separate inner loops for the odd and even lines. Obviously, one of them should be executed only for odd line numbers and the other for even ones. So, you need to put them inside if-else blocks.


Edited by Ritwik I the programmer, 04 July 2014 - 08:28 PM.

  • 0

I can believe, but why should I?


#114 XB23

XB23

    CC Newcomer

  • Member
  • PipPip
  • 21 posts

Posted 05 July 2014 - 07:35 AM

My problem is, I'm not sure what is the best way to approach a problem. I assume I'm doing it the long/hard/ugly way. I'm completely new to this & programming as a whole, and basically followed the method posted by: wim DC (on page 3 of this thread ("Solution for every loop your teacher is bothering you with"). And then went and completed every example/loop posted on all 9 pages.

 

Here is my code:

 

1:

1
2, 1
1, 2, 3
4, 3, 2, 1
1, 2, 3, 4, 5
for (int i = 1; i <= 5 ; i++) {


  for (int j = 1; j < i+1; j++) {




    if (i % 2 != 0) {


      print(j);
    }


    else {


      print((i-j)+1);
    }


    if (i == j) {


      print("");
    }


    else {


      print(",");
    }
  }


  println();
}

2: 

-----*-----
----***----
---*****---
--*******--
-*********-
***********
-*********-
--*******--
---*****---
----***----
-----*-----
int num = 1;

for (int i = 6; i >= 1; i--) {

  for (int j = i; j >= 2; j--) {

    print("-");
  }
  for (int j = 1; j <= num; j++) {

    print("*");
  }

  for (int j = i; j >= 2; j--) {

    print("-");
  }

  num+= 2;
  println();
}


int nums = 9;


for (int i = 1; i <= 5; i++) {

  for (int j = 1; j < i+1; j++) {

    print("-");
  }

  for (int j = 1; j <= nums; j++) {

    print("*");
  }

  for (int j = 1; j < i+1; j++) {

    print("-");
  }

  nums-= 2;
  println();
}

3:

2, 4, 6, 8
4, 6, 8
6, 8
8
for (int i = 0; i < 4; i++) {

  for (int j = 1; j < 5-i; j++) {

    print((i*2)+(j*2));

    if ((i+j) != 4) {

      print(",");
    }
  }

  println();
}

There is obviously a better/quicker way than the way I'm doing it. I basically want to know how to do it the easy/easier way. Is there any tutorials that teach you how to approach the task (patterns/things to look for, break it down, and solve it)? As I spend a lot longer on each task, then it should require.


Edited by XB23, 05 July 2014 - 07:37 AM.

  • 0

#115 XB23

XB23

    CC Newcomer

  • Member
  • PipPip
  • 21 posts

Posted 05 July 2014 - 12:14 PM

What about a problem like this:

1 2 3 4 5

10 9 8 7 6 

11 12 13 14 15

20 19 18 17 16 

21 22 23 24 25

I can see the patterns, but don't know how to translate that into code.


  • 0

#116 XB23

XB23

    CC Newcomer

  • Member
  • PipPip
  • 21 posts

Posted 05 July 2014 - 12:29 PM

I know it's been mentioned that someone should post their code, and show their efforts, instead of expecting answers, so here was my attempt:

int num = 1;

for (int i = 1; i <=5; i++) {

  for (int j = 1; j <=5; j++) {


    if (i % 2 == 0) {

      num--;
      print(num + " ");
    }

    else {


      print(num + " ");
      num++;
    }
  }

  num = num + 5;
  println();
}


  • 0

#117 Ritwik I the programmer

Ritwik I the programmer

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 241 posts
  • Programming Language:Java
  • Learning:Java, C++, Python

Posted 05 July 2014 - 07:27 PM

In fact, although there is a pattern, the line starting with 0 is deleted to avoid -ve nos. So, the general pattern would have been,

0  -1 -2 -3 -4
1   2  3  4  5
10  9  8  7  6
11 12 13 14 15
20 19 18 17 16
21 22 23 24 25

So, you start with 0, increment your variable i by 10 in each iteration, and print lines starting with i and i + 1, except when loop variable is 0. I think you can figure out how to do the individual lines and translate this into code.


  • 0

I can believe, but why should I?






Also tagged with one or more of these keywords: nested loop, for loop, loop