Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
October 31st, 2012, 01:36 AM
 H4rduino
Registered User

Join Date: Oct 2012
Posts: 3
Time spent in forums: 29 m 45 sec
Reputation 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
October 31st, 2012, 02:09 AM
 salem
Contributed User

Join Date: Jun 2005
Posts: 4,266
Time spent in forums: 2 Months 4 Weeks 1 Day 18 h 35 m 56 sec
Reputation Power: 1827
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

 Viewing: Dev Shed Forums > Programming Languages > C Programming > Loop an If condition until .. ?