Thread: One program does nested loops while the other doesn't. Why?

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    12

    Unhappy One program does nested loops while the other doesn't. Why?

    So I'm a noob in C programming. Just started looking at loops. I will post the program that's in my lecture first and then one I made and they both have next for loops. But the second one (mine) isn't functioning like the first. Why is it? Sorry if it's really obvious.

    Code:
    #include <stdio.h>
    
    
    main(void) 
    
    
     {
         int i, j, k, m=0;
    
    
         for (i=1; i<=5; i+=2)  
             { 
             for (j=1; j<=4; j++)  
                 {
                 k = i+j;
                 printf("i=%3d, j=%3d, k=%3d\n", i, j, k);
                 } 
             
             } 
     }

    And my program is:

    Code:
    #include <stdio.h>
    main ()
    {
    int count=3,times=1;
    for (times;times<=5;times+=1){
    for (count;count<=99;count+=3)
    	{
    	printf ("%d ",count);
    	}
    }
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Declare main as returning an int. You should indent your code properly, e.g.,
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int i, j, k, m = 0;
    
        for (i = 1; i <= 5; i += 2)
        {
            for (j = 1; j <= 4; j++)
            {
                k = i + j;
                printf("i=%3d, j=%3d, k=%3d\n", i, j, k);
            }
        }
        return 0;
    }
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int count = 3, times = 1;
        for (times; times <= 5; times += 1)
        {
            for (count; count <= 99; count += 3)
            {
                printf("%d ", count);
            }
        }
        return 0;
    }
    Anyway, in what way do you mean "the second one (mine) isn't functioning like the first"? Clearly, they are different.

    One thing to note is that just writing:
    times;[/code]
    evaluates the variable and does nothing. Normally, we would either leave it out because the variable already has the desired value:
    Code:
    for (; times <= 5; times += 1)
    or we will set the desired value:
    Code:
    for (times = 1; times <= 5; times += 1)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    12
    Quote Originally Posted by laserlight View Post
    Declare main as returning an int. You should indent your code properly, e.g.,
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int i, j, k, m = 0;
    
        for (i = 1; i <= 5; i += 2)
        {
            for (j = 1; j <= 4; j++)
            {
                k = i + j;
                printf("i=%3d, j=%3d, k=%3d\n", i, j, k);
            }
        }
        return 0;
    }
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int count = 3, times = 1;
        for (times; times <= 5; times += 1)
        {
            for (count; count <= 99; count += 3)
            {
                printf("%d ", count);
            }
        }
        return 0;
    }
    Anyway, in what way do you mean "the second one (mine) isn't functioning like the first"? Clearly, they are different.

    One thing to note is that just writing:
    times;[/code]
    evaluates the variable and does nothing. Normally, we would either leave it out because the variable already has the desired value:
    Code:
    for (; times <= 5; times += 1)
    or we will set the desired value:
    Code:
    for (times = 1; times <= 5; times += 1)
    Hey, sorry. I got it. I should close this thread now. Hm. I know they're different, but the basic function I'm looking for is a loop within a loop. I got it to work by having
    Code:
    for (count=3;count<=99;count+=3)
    I removed the initial value when declaring count. That way it loops again with count again equals to 3. Thanks!

  4. #4
    Registered User
    Join Date
    Feb 2012
    Posts
    12
    How do I close this thread?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    There's no need to close it
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested for loops
    By akub3 in forum C Programming
    Replies: 2
    Last Post: 04-01-2004, 06:21 AM
  2. nested for loops/
    By o0o in forum C++ Programming
    Replies: 2
    Last Post: 12-28-2003, 10:19 AM
  3. nested loops
    By briand. in forum C Programming
    Replies: 6
    Last Post: 10-01-2002, 05:15 PM
  4. Nested for loops
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 02-12-2002, 10:25 AM
  5. nested for loops
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 11-17-2001, 11:44 AM

Tags for this Thread