Thread: change var inside loop and run loop again

  1. #1
    Registered User
    Join Date
    Jul 2011

    change var inside loop and run loop again

    #include <stdio.h>
    int main()
    double a=1;
    double b=0.01;
    double c;
    c = a * b;
    while ( a <= 10000)
         printf("%f\t%f\n", a, c);
    a = a++;
    c = a*b;
    return 0;
    my question is how do I replace the value of b and run the loop over again untill b = 100

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Put into words what it is you actually are trying to do. You can't code for what you want if you don't know what it is you want to do. How are a and b related? Why are you using a to control the loop if you really care about what b is doing?

    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2006
    By having a second, outer loop.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    Registered User
    Join Date
    Jul 2011
    b is 1% and a is 1 dollar
    i used this loop to get a to go up to 10,000 dollars and return the % amount for each dollar

    i don't know how to change the value of b and run the loop again

    if I change the value of b to .02 the loop will return the % of the given dollar value

    sorry I should of put comments in code

  5. #5
    Join Date
    Aug 2010
    Ontario Canada
    Here's a hint... If the loop were in a function, you could feed it seed values and run it as often as you like.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > a = a++;
    This is wrong -> see Question 3.3

    > b is 1% and a is 1 dollar
    Then you'll want to use integers, not floats. floats are approximate numbers with all sorts of rounding errors. If you want to keep things nice and accurate, do something like

    int dollars = 100;
    int cents = 1;


    dollars += 100; // a new dollar
    cents++; // a new cent

    Notice how I've uses meaningful variable names, not meaningless a,b,c
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 06-23-2009, 03:51 AM
  2. For Loop inside While Loop
    By mesmer in forum C Programming
    Replies: 1
    Last Post: 11-29-2008, 12:29 PM
  3. for loop ignoring scanf inside loop
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-17-2007, 01:46 AM
  4. loop and compiling inside a code
    By MtJ in forum C Programming
    Replies: 3
    Last Post: 03-05-2006, 12:50 PM
  5. for loop inside of a for loop
    By pippen in forum C++ Programming
    Replies: 5
    Last Post: 03-28-2003, 04:11 PM