#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0

    Loop an If condition until .. ?


    Is it possible to have a code that would do my math until the results are between 50 and 200 ?
    My equation can go very high and I have to work with either /2 or *2 until I get between those 2 targets. As high as 11 times doing /2 (hence the 11 if)

    Could you help me please ? :)

    Code:
      switch (choix)
      {  
       case 1:        
        printf("f: "); 
        scanf("%lf", &B);
        C = A/(1/B);
        if(C<50) C=2*C;
        if(C>200) C=C/2;   
        if(C<50) C=2*C;
        if(C>200) C=C/2;
        if(C<50) C=2*C;
        if(C>200) C=C/2;
        if(C<50) C=2*C;
        if(C>200) C=C/2;   
        if(C<50) C=2*C;
        if(C>200) C=C/2;
        if(C<50) C=2*C;
        if(C>200) C=C/2;
        if(C<50) C=2*C;
        if(C>200) C=C/2;
        if(C<50) C=2*C;
        if(C>200) C=C/2;
        if(C<50) C=2*C;
        if(C>200) C=C/2;
        if(C<50) C=2*C;
        if(C>200) C=C/2;
        if(C<50) C=2*C;
        if(C>200) C=C/2;
        printf("t = %.10f \n\n",C);
        break;
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,365
    Rep Power
    1870
    Like
    Code:
    while ( C<50 || C>200 ) {
        if(C<50) C=2*C;
        if(C>200) C=C/2;   
    }
    Or even
    Code:
    while ( C < 50 ) C = C * 2;
    while ( C > 200 ) C = C / 2;
    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

IMN logo majestic logo threadwatch logo seochat tools logo