Like Tree1Likes
  • 1 Post By laserlight

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

This is a discussion on One program does nested loops while the other doesn't. Why? within the C Programming forums, part of the General Programming Boards category; So I'm a noob in C programming. Just started looking at loops. I will post the program that's in my ...

  1. #1
    Y2R
    Y2R is offline
    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
    21,589
    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)
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Y2R
    Y2R is offline
    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
    Y2R
    Y2R is offline
    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
    21,589
    There's no need to close it
    Y2R likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 05:21 AM
  2. nested for loops/
    By o0o in forum C++ Programming
    Replies: 2
    Last Post: 12-28-2003, 09: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, 09:25 AM
  5. nested for loops
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 11-17-2001, 10:44 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21