MATLAB: Subtracting a value from a scalar until the scalar reaches a certain point, then starts again.

for loopif statementscalarsubtractwhile loop

I know this is relatively simple, but I've been struggling with this.
I have a problem where I need to tell an airplane pilot how many times he will fill up and how much fuel he will have left after x amount of legs. We need to assume that everytime the plane reaches 5 gallons of fuel, the plane is refueled to full capacity.
Each trip burns 8 gallons of fuel.
The fuel capacity is 42 gallons.
The test case is 12 legs and the pilot will be left with 10 gallons. I am almost positve I need a for loop with i = 1:number of legs
I've tried for loops, while loops, and if statements and all I get is that there are -54 gallons left or it just infinitely runs.
Any amount of help would be appreciated.
Thanks,
Jack

Best Answer

Try something like this:
legamount = 12;
refuel=0;
fuelremaingal = 42;
for i=1:legamount
if fuelremaingal - 8 > 5
fuelremaingal = fuelremaingal-8;
else
refuel=refuel+1;
fuelremaingal = 29+fuelremaingal;%just math
end
end
Related Question