Thread: converting for loops to while loops

  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
    28,413
    What have you tried?
    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
    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
    28,413
    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.
    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. 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