Like Tree1Likes
  • 1 Post By quzah

need help converting while loops to for loops in my code

This is a discussion on need help converting while loops to for loops in my code within the C Programming forums, part of the General Programming Boards category; Hello everyone, I'm new to learning C language and was wondering if anyone with more experiance could help me out. ...

  1. #1
    Amb
    Amb is offline
    Registered User
    Join Date
    Jun 2011
    Posts
    1

    need help converting while loops to for loops in my code

    Hello everyone, I'm new to learning C language and was wondering if anyone with more experiance could help me out.
    I only have about 20 hours at most of self learned C programming under my belt so bare with my stuff here.
    Once I realized I knew enough to toss together a simple program that actully does somthing other then say "blah blah blah" i made a pound/kilogram converter, anyway the point of my post is I used while loop statments in my code and shortly after started learning about for loop statments, I thought I understood how to change my while loops to for loops but apparently I don't, when I change it to use for loops enstead of while loops when i run my program and select 2 it goes to convert pounds to kilograms although it should be converting kilograms to pounds like it does when I use the while loops.
    Anyway heres my code and how I thought I was supposed to change it and my question is at the bottom

    Code:
    #include <stdio.h>
    
    int main()
    {
        float a, b;
        a = 0;
        b = 0;
        printf("\t\tPound to Kilogram converter by Dustin Silver\n");
        printf("\nType 1 to convert pounds to kilograms\nType 2 to convert kilograms to pounds\nthen press enter:");
        scanf("%f", &b);
        while (b < 3)
        {
            if (b > 1)
            {
                while (b > 1)
                {
                    printf("\nEnter amount of kilograms to be converted to pounds then press enter:\n");
                    scanf("%f", &a);
                    printf("%f kilogram(s) Kgs = %f pound(s) Lbs\n", a, (a * 2.20462));
                    printf("\nCreated & proudly produced by Dustin Silver ;)\n");
                }
            }
            printf("\nEnter amount of pounds to be converted to kilograms then press enter:\n");
            scanf("%f", &a);
            printf("%f pound(s) Lbs = %f kilogram(s) Kgs\n", a, (a / 2.20462));
            printf("\nCreated & proudly produced by Dustin Silver ;)\n");
        }
        return 0;
    }

    Here is how I thought it was supposed to be changed. (if I wanted for loops enstead of while loops)

    Code:
    #include <stdio.h>
    
    int main()
    {
        float a, b;
        a = 0;
        printf("\t\tPound to Kilogram converter by Dustin Silver\n");
        printf("\nType 1 to convert pounds to kilograms\nType 2 to convert kilograms to pounds\nthen press enter:");
        scanf("%f", &b);
        for (b=0; b<3; b+0)
            if (b > 1)
            {
                for (b=0; b>1; b+0)
                    printf("\nEnter amount of kilograms to be converted to pounds then press enter:\n");
                    scanf("%f", &a);
                    printf("%f kilogram(s) Kgs = %f pound(s) Lbs\n", a, (a * 2.20462));
                    printf("\nCreated & proudly produced by Dustin Silver ;)\n");
            }
            printf("\nEnter amount of pounds to be converted to kilograms then press enter:\n");
            scanf("%f", &a);
            printf("%f pound(s) Lbs = %f kilogram(s) Kgs\n", a, (a / 2.20462));
            printf("\nCreated & proudly produced by Dustin Silver ;)\n");
        return 0;
    }
    OR

    Code:
    #include <stdio.h>
    
    int main()
    {
        float a, b;
        a = 0;
        printf("\t\tPound to Kilogram converter by Dustin Silver\n");
        printf("\nType 1 to convert pounds to kilograms\nType 2 to convert kilograms to pounds\nthen press enter:");
        scanf("%f", &b);
        for (b=0; b<3; b+0)
        {
            if (b > 1)
            {
                for (b=0; b>1; b+0)
                {
                    printf("\nEnter amount of kilograms to be converted to pounds then press enter:\n");
                    scanf("%f", &a);
                    printf("%f kilogram(s) Kgs = %f pound(s) Lbs\n", a, (a * 2.20462));
                    printf("\nCreated & proudly produced by Dustin Silver ;)\n");
                }
            }
            printf("\nEnter amount of pounds to be converted to kilograms then press enter:\n");
            scanf("%f", &a);
            printf("%f pound(s) Lbs = %f kilogram(s) Kgs\n", a, (a / 2.20462));
            printf("\nCreated & proudly produced by Dustin Silver ;)\n");
        }
        return 0;
    }
    but neither of these two codes work like the first one as explained earlier

    Can someone please help me convert the while loop staments in the top piece of code to for loop statments, any help would be very very appreciated.

    Also I have another small piece of code I'm trying to convert from for loops to while loops to help me better understand the concept of for looping but after I make the changes the results are not what I'm expecting, if anyone could help me convert this piece of code the same way as I need the other one converted (but opposite) it would be very very appreciated.

    Code:
    #include <stdio.h>
    
    int main()
    {
        int a[5];
        int i;
    
        for (i=0; i<5; i++)
            a[i] = i;
        for (i=0; i<5; i++)
        printf("a[%d] = %d\n", i, a[i]);
        return 0;
    }
    thanks,
    Dustin
    Last edited by Amb; 06-01-2011 at 01:37 AM.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    While loops are repeated as long as the condition of entry is true ...
    For loops iterate through a set of values while the repeat condition is true.
    They have very different applications and often you can't replace one with another.

    In your first piece of code the exit condition is entering a number 3 or greater.
    You can't really replace that with a for loop since you aren't iterating through any given number of values. A better replacement in your first piece of code would be to replace the while conditions with if statements...

    Code:
    #include <stdio.h>
    
    int main( void )
    {
        float a, b;
        a = 0;
        b = 0;
        printf("\t\tPound to Kilogram converter by Dustin Silver\n");
        printf("\nType 1 to convert pounds to kilograms\nType 2 to convert kilograms to pounds\nthen press enter:");
        scanf("%f", &b);
        if (b == 1)
         {
            printf("\nEnter amount of kilograms to be converted to pounds then press enter:\n");
            scanf("%f", &a);
            printf("%f kilogram(s) Kgs = %f pound(s) Lbs\n", a, (a * 2.20462));
            printf("\nCreated & proudly produced by Dustin Silver ;)\n");
          }
        else if(b == 2)
         {
            printf("\nEnter amount of pounds to be converted to kilograms then press enter:\n");
            scanf("%f", &a);
            printf("%f pound(s) Lbs = %f kilogram(s) Kgs\n", a, (a / 2.20462));
            printf("\nCreated & proudly produced by Dustin Silver ;)\n");
        }
      else
        Printf ("Oops... you must enter 1 or 2\n" ); 
        return 0;
    }




    In your last piece of code you had the idea... it should have printed
    a[0]=0, a[1]=1, a[2]=2, a[3]=3, a[4]=4 ... which would be the correct answer.

    At least now you should have a picture of the different uses for these kinds of loops...
    Last edited by CommonTater; 06-01-2011 at 02:35 AM.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,290
    Quote Originally Posted by CommonTater View Post
    For loops iterate through a set of values while the repeat condition is true.
    for loops are often used to iterate (for example over a range) but there is nothing limiting them to that.
    Quote Originally Posted by CommonTater View Post
    They have very different applications and often you can't replace one with another.
    That's not true. Yes, the different loop constructs are often applied differently, but any loop construct can be replaced with another (including a simple loop involving a goto).

    Any while loop of the form
    Code:
        pre_condition();
        while (test_condition())
        {
             do_something();
             change_condition();
        }
    is exactly equivalent to
    Code:
        for (pre_condition(); test_condition() ; change_condition())
        {
            do_something();
        }
    Right 98% of the time, and don't care about the other 3%.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can use any loop in place of any other, but in my opinion, you should use whatever loop makes your code the most readable.
    Code:
    for( x = 0; x < 10; x++ )
    {
        ...do something 10 times...
    }
    That's clear and readable, and I would probably use that more than I would to write it this way:
    Code:
    x = 0;
    while( x < 10 )
    {
        ...do something...
        x++;
    }
    Or even:
    Code:
    x = 0;
    do
    {
        ...
        x++;
    }
    while( x < 10 );
    Some of it is just cleaner.

    Now what if I was doing a menu?
    Code:
    do
    {
        showmenu();
        c = getchoice();
    } while( ! validchoice( c ) );
    Could I do that with a for loop? Sure.
    Code:
    for( ; ; ! validchoice( c ) )
    {
        showmenu();
        c = getchoice();
    }
    Does it look better? Not to me. Also, there are some very subtle differences here.

    For one, a do-while will always execute at least once. So you need to consider that when you make loops. Do you always need it to work at least once? Then do-while is probably what you want. Do you need to test and maybe loop? Then either a while or a for is probably best. Can you use a do-while for something you need to check to see if you need to loop before actually looping? Sure, it's just more involved, and looks a bit messy:
    Code:
    if( condition ) do
    {
        ...
    }
    while( condition );
    Use whatever looks clean and fits more easily with the task at hand.

    Oh, and all of these could be written with goto as well.


    Quzah.
    Salem likes this.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loops that iterate/alter code in a binary fashion?
    By 1:1 in forum C++ Programming
    Replies: 4
    Last Post: 01-16-2011, 05:32 PM
  2. can i ask what is wrong with this code? [LOOPS only]
    By Huskar in forum C Programming
    Replies: 6
    Last Post: 03-18-2009, 02:05 PM
  3. Help with a code using File I/O and Loops
    By bns1201 in forum C Programming
    Replies: 27
    Last Post: 10-17-2008, 09:19 PM
  4. while loops...a problem with my simple code?
    By niceguy in forum C Programming
    Replies: 13
    Last Post: 02-20-2008, 01:59 PM
  5. Loops OR Functions!! HELP WITH THIS CODE!!!!!!!!!!
    By WIshIwasGooD in forum C++ Programming
    Replies: 4
    Last Post: 10-24-2001, 12:49 PM

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