Like Tree2Likes
  • 2 Post By laserlight

converting for loops to while loops

This is a discussion on converting for loops to while loops within the C Programming forums, part of the General Programming Boards category; I am needing to convert the for loop to the while loop but I am unsure exactly how Code: #include ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Location
    Fresno, California, United States
    Posts
    6

    converting for loops to while loops

    I am needing to convert the for loop to the while loop but I am unsure exactly how

    Code:
    #include <stdio.h>
    
    
    int
    main(void)
    {
          int i, j;   /* loop control variables */
    
    
          printf("           I    J\n");        /* prints column labels     */
    
    
          for  (i = 1;  i < 4;  ++i) {       /* heading of outer for loop     */
          printf("Outer %6d\n", i);
          for  (j = 0;  j < i;  ++j) {      /* heading of inner loop     */
              printf("  Inner%9d\n", j);
          }   /* end of inner loop */
          }   /*  end of outer loop */
    
    
          return (0);
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,440
    What have you tried?
    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
    Registered User
    Join Date
    Feb 2012
    Location
    Fresno, California, United States
    Posts
    6
    I currently have it to this but not sure of its accuracy

    Code:
    #include <stdio.h>
    
    
    int
    main(void)
    {
      int i, j; /*loop control variables */
      printf("          I   J\n"); /* prints column labels */
      i = 1;
      while ( i < 4 ) {
    	printf("Outer %6d\n", i);
      i = i + 1;
      j = 0;
      while ( j < i ) {
        printf("Inner %9d\n", j);
      j = j + 1;
      }
    }
    return(0);
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,440
    Compare the output. Are they the same?

    By the way, you need to indent your code properly. This is especially important when nested loops are involved.
    iMalc and inu11byte like 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. Replies: 3
    Last Post: 06-01-2011, 04:19 PM
  2. loops, menu loops
    By gloworm in forum C Programming
    Replies: 17
    Last Post: 04-12-2010, 07:59 PM
  3. loops
    By crvenkapa in forum C++ Programming
    Replies: 8
    Last Post: 03-24-2007, 01:24 PM
  4. 2 loops at once?
    By DeepFyre in forum C++ Programming
    Replies: 28
    Last Post: 09-08-2004, 09:58 PM
  5. loops
    By Shadow in forum C Programming
    Replies: 2
    Last Post: 05-16-2002, 01:31 PM

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