# C-loops explain plz

Discussion in 'C Programming' started by Tweaxor, Oct 17, 2003.

1. ### TweaxorGuest

Hey, forgive me of my ignorance!

Can someone tell me why this won't work. I have this coded but it
won't compile is this not permitted in C.

kg = kilograms and d = distance // this is not code but a example of
what
I was trying to do. I at the library now and I don't have the code
with me now
but this is what I was trying to do.

if kg => 6kg
if d => 200
count = 1.50
else for (d; d > 200; d--)
count + 1.50

Well that basically how I coded it. I was trying to see if the
distance is 200 or less if so add 1.50 to count. But if it's more
than 200 add 1.50 to count for every 200 in distance. Using a for loop
that how I thought it should be written. The Sams teach yourself
don't mention when one should use different looping statements.
Please explain to me why this would work. I got it to work using only
if statements thou. Explain plz. Thanks in advance

Tweaxor, Oct 17, 2003

2. ### Kris WempaGuest

This is not a valid C program. Is this what you mean ?

if (kg >= 6) {
if (d >= 200) {
count = 1.50;
} else {
for ( d ; d > 200 ; d--)
count += 1.50;
}
}

Also, the first 'd' in the for loop is not needed so it can be re-written
as:

for ( ; d > 200 ; d-- )

This is a valid C program. Explain what you are trying to do a little
better.

Kris Wempa, Oct 17, 2003

3. ### Irrwahn GrausewitzGuest

Uck! Let's see what you really want to do:
if ( d <= 200 )
if ( d <= 200 )
count += 1.5;
if ( d <= 200 )
count += 1.5;
else
if ( d <= 200 )
count += 1.5;
else
count += 1.5 * (d / 200);

Note: we are assuming that d is positive and of integral type!
OK.

if ( d <= 200 )
count += 1.5;
else
for( ; d > 200; d -= 200 )
count += 1.5;

I'm not sure if it is what you asked for, if not please refine your
request.

HTH
Regards

Irrwahn Grausewitz, Oct 17, 2003
4. ### TweaxorGuest

This is want I was trying. I was wanting to increment total cost.
cost1 = 1.10 for every 500 in d add 1.10 to it.
printf("\nEnter weight(in Kg): ");
scanf("%i",&wt);
if (wt < 0 || wt > 20) {
scanf("%i",&wt);
}
printf("\nEnter distanc(in mi.): ");
scanf("%i",&d);
if (d < 0 || d >3000) {
scanf("%i",&d);
}
if (wt <= 2 && d <= 500) {
printf("\tTotal cost: %1.2f",cost1);
}
if (wt >= 3 && wt <= 6 && d <= 500) {
printf("\tTotal cost: %1.2f",cost2);
}
if (wt >= 6 && wt <= 10 && d <= 500) {
printf("\tTotal cost: %1.2f",cost3);
}
if (wt >= 10 && wt <= 20 && d <= 500) {
printf("\tTotal cost: %1.2f",cost4);
}

return 0;
}

Tweaxor, Oct 18, 2003
5. ### Chiron PaixosGuest

Well, some questions/remarks:
1) Don't you understand your assignment?
2) When is it due?
3) Try to explain in plain language what you try to do in this program
4) Post your actual code and not some snippet that's incomplete:
- cost1, cost2 , cost3, cost4 are neither defined nor initialized
- there is no "main" statement
5) Be as precise as possible as nobody in this NG likes guessing
- in your example code aren't any loops at all, so what's

Let me guess - your assignment reads more or less like this:
"Write a calculator for a parcel service. The rates are calcuted from
a weight depend base price and the distance of transports."
(Then there is a neat table giving the 4 weight ranges and the price
including the first 500 miles.)
For longer distances an additional charge of 1.10 per 500 miles has to
be added the the base price." (some examples of correct freight rates
follow)

Good guess, isn't it?

Chiron Paixos, Oct 18, 2003