1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

convert for loop into while and do..while

Discussion in 'C' started by philay, Dec 12, 2007.

  1. philay

    philay Banned

    Joined:
    Dec 12, 2007
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    #include<stdio.h>
    int main ()
    {
    int i,j;
    
    for(i=1;i<=5;i++)  {
      for(j=1;j<=i;j++)
        printf("%i",i);
      printf("\n");
    }
    
    return 0;
    
    }
    
    The output is
    1
    22
    333
    4444
    55555

    this is the "for" method, how do i convert it into while and do..while format? i just still noob in programming , any expert help me out pls? thx
     
    Last edited by a moderator: Dec 12, 2007
  2. Salem

    Salem New Member

    Joined:
    Nov 15, 2007
    Messages:
    133
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Please don't PM me for 1:1 support.
    for ( a ; b ; c ) d;

    Is approximately
    a;
    while ( b ) { d; c; }
     
  3. philay

    philay Banned

    Joined:
    Dec 12, 2007
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    ya, but if got 2 loops? for(i=0;i<=8;i++) the first loop then inside another for (j=0;j<=i;j++)?
     
  4. Salem

    Salem New Member

    Joined:
    Nov 15, 2007
    Messages:
    133
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Please don't PM me for 1:1 support.
    So two for loops become two while loops - what's the problem?
     
  5. philay

    philay Banned

    Joined:
    Dec 12, 2007
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    int i=1,j=1;
    
    while(i<=5)  {
        printf("%i",i);
        i++;
        while(j<=i)  {
           printf("\n");
           j++;
    }
    
    but if i write like this, i compile cant get the correct output same as the for loop
    1
    22
    333
    4444
    55555
     
  6. philay

    philay Banned

    Joined:
    Dec 12, 2007
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    int i=1,j=1;
    
    do {
       printf("%i",i);
       i++;
    do {
       printf("\n");
       j++;
    }while(j<=i);
    }while(i<=5);
    
     
  7. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,293
    Likes Received:
    365
    Trophy Points:
    83
    For while loop you should increment the counter as the last statement and then it will be equivalent.

    Do while loops are a bit different and cannot be equal to for loop but in your case applying the increment as the last statement of the loop would do.
     
  8. philay

    philay Banned

    Joined:
    Dec 12, 2007
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    i don't really get what u really mean.. how about u post the code for me? then i will know better
     
  9. Salem

    Salem New Member

    Joined:
    Nov 15, 2007
    Messages:
    133
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Please don't PM me for 1:1 support.
    Refer to my other post
    Code:
    for([COLOR=Red]i=1[/COLOR];[COLOR=Blue]i<=5[/COLOR];[COLOR=Magenta]i++[/COLOR])  {
    [COLOR=Lime]  for(j=1;j<=i;j++)
        printf("%i",i);
      printf("\n");[/COLOR]
    }
    
    Becomes
    Code:
    [COLOR=Red]i=1[/COLOR];
    while ( [COLOR=Blue]i<=5[/COLOR] ) {
    [COLOR=Lime]  for(j=1;j<=i;j++)
        printf("%i",i);
      printf("\n");[/COLOR]
      [COLOR=Magenta]i++[/COLOR];
    }
    
    Now apply the same transformation to the inner loop, and you're done.
     
  10. philay

    philay Banned

    Joined:
    Dec 12, 2007
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    kk, thx so much for the help
     
  11. asadullah.ansari

    asadullah.ansari TechCake

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA
    One thing remember before putting any problem just try to solve atleast one time.
     
  12. asadullah.ansari

    asadullah.ansari TechCake

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA
    If any one putted any silly problem by mistake then that post should be deleted because when we are searching such silly problem making the Image of this forum lower and user time is waisted.
     

Share This Page