Thread: Turning a nested for loop into a nested while loop fails - why?

  1. #1
    Registered User
    Join Date
    Dec 2018
    Posts
    36

    Turning a nested for loop into a nested while loop fails - why?

    Hi!

    As an exercise I would like to turn following nested for loop into a nested while loop:

    Turning a nested for loop into a nested while loop fails - why?-nested_forloop-png

    But somehow it does not work out the way I want it to and obviously after all iterations are completed within the inner while loop,
    my program does not find its way back into the outer while loop to start over again:

    Turning a nested for loop into a nested while loop fails - why?-nested_whileloop-png

    Code:
    #include <stdio.h>
    
     int main() {
         int i = 1, j = 1;
         while(i <= 5) {
            while(j <= 5) {
                printf("%4d", i * j);
                j++;
            }
            printf("\n");
            i++;
         }
    
    
         return 0;
     }
    I cannot spot my mistake and would appreciate any help.^^

    regards,
    Placebo

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Your j = 1 is in the wrong spot. Remember j is used in the inner loop.
    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
    Dec 2018
    Posts
    36
    Hey,

    thanks a lot, your hint helped me out:


    Turning a nested for loop into a nested while loop fails - why?-nested_whileloop-png

    The problem is though that I do not understand why we must assign the value directly before the inner loop begins, i.e. directly before we will need the variable?
    Can't the program remember the value of the variable j, when I would have assigned it at the very beginning of the program and then start using the remembered value whenever it is needed?
    Is this a particularity with "loop-blocks" or with any seperated blocks or scopes, in general?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It is doing that: it "remembers" the value of j, and that's the value of j that was incremented again and again in the inner loop. That value of j is no longer 1, which is why you have to "reset" it before the inner loop.
    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

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Remember that
    Code:
    for ( a ; b ; c ) {
        d;
    }
    Is approximately equal to
    Code:
    a;
    while ( b ) {
        d;
        c;
    }
    The approximation breaks down when trying to use the 'continue' keyword.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Dec 2018
    Posts
    36
    Quote Originally Posted by laserlight View Post
    It is doing that: it "remembers" the value of j, and that's the value of j that was incremented again and again in the inner loop. That value of j is no longer 1, which is why you have to "reset" it before the inner loop.
    Ah, now I indeed got it - thanks a lot!

    Also, thanks Salem for your hint.^^
    Last edited by Placebo; 04-07-2019 at 09:34 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested while loop
    By _jamie in forum C Programming
    Replies: 2
    Last Post: 03-01-2019, 12:08 PM
  2. Help with nested for loop .
    By perfectprashant in forum C Programming
    Replies: 5
    Last Post: 12-26-2012, 05:40 PM
  3. Nested while loop inside for loop
    By Sonny in forum C Programming
    Replies: 71
    Last Post: 07-31-2011, 08:38 PM
  4. Nested for loop
    By gjack72 in forum C++ Programming
    Replies: 4
    Last Post: 10-26-2005, 12:44 PM
  5. Nested Loop
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 05-07-2002, 12:40 AM

Tags for this Thread