MATLAB Answers

another way to break without using the command 'break'

59 views (last 30 days)
Hi all,
In this code i want to know another way to break the code withoug using the 'break command': This break should happen when the remaining liquid in a silo is less than the volume of a container
function [FullContainers,RemainingLiquid] = myFunction(VolumeOfSilo)
% myFunction generates the number of containers that are filled by the
% liquid in a silo of a given volume and the Liters remaining in the silo
% after all the containers have been filled
% >> syntax >> [NoContainer,RemainingLiquid] = myFunction(volumeOfSilo)
c = 0 ; % initially we dont have any containers
vol = zeros() ; % preallocate for speed
while c >= 0
c = c + 1; % keep bringing containers
vol(c) = 280 + 150*rand ; % determine the volume of the container
if VolumeOfSilo >= vol(c)
RemainingLiquid = VolumeOfSilo - vol(c) ; % remaining liters after pouring
VolumeOfSilo = RemainingLiquid; % update the liters remaining in silo
FullContainers = c ;
elseif VolumeOfSilo < vol(c) %
FullContainers = 0 ;
RemainingLiquid = VolumeOfSilo ;
end
if VolumeOfSilo < vol(c)
break
end
end
end

  0 Comments

Sign in to comment.

Accepted Answer

Allen
Allen on 25 Jan 2020
To stop the while loop you need to make sure that none of the conditions are met, else it will keep running. By changing || to && you are saying that both conditions need to be met in order to continue looping.
term_while = true; % flag variable
c = 0 ; % initially we dont have any containers
vol = zeros() ; % preallocate for speed
% while c >= 0 || term_while
while c >= 0 && term_while
...
if VolumeOfSilo < vol(c) % help me to avoid using break
% break
term_while = false; % set flag variable to false so the loop stops
end
end
Alternatively, you can use your orginal code but replace break with c = -1. This will prevent the condition for continuing the while-loop from being met.
c = 0 ; % initially we dont have any containers
vol = zeros() ; % preallocate for speed
while c >= 0
c = c + 1; % keep bringing containers
...
if VolumeOfSilo < vol(c)
c = -1;
end
end
However, looking over the intention of your function, you might consider using the following instead.
function [FullContainers,RemainingLiquid] = myFunction(VolumeOfSilo)
% myFunction generates the number of containers that are filled by the
% liquid in a silo of a given volume and the Liters remaining in the silo
% after all the containers have been filled
% >> syntax >> [NoContainer,RemainingLiquid] = myFunction(volumeOfSilo)
i = 0; % loop counter
vol = 0; % preallocate for speed
% While-loop creates a random number of volumes until the total volume is
% greater than the Volume of the Silo. Also assigned a max number of
% iterations to allow the while-loop to run as a safety measure.
while sum(vol)<=VolumeOfSilo && i<1e5
i = i+1; % increment loop counter
vol(i) = 280+150*rand ; % determine the volume of the container
end
% Counter the number of elements in the vol variable and determine the
% amount of remaining fluid. Since we set the while-loop to end after the
% silo volume is exceeded, we need to ignore the last container volume
% in the remaining steps.
FullContainers = numel(vol)-1;
RemainingLiquid = VolumeOfSilo-sum(vol(1:end-1));
end

More Answers (1)

Bhaskar R
Bhaskar R on 25 Jan 2020
You can set a flag variable to terminate while loop without using break keyword
term_while = true; % flag variable
c = 0 ; % initially we dont have any containers
vol = zeros() ; % preallocate for speed
while c >= 0 || term_while
c = c + 1; % keep bringing containers
vol(c) = 280 + 150*rand ; % determine the volume of the container
if VolumeOfSilo >= vol(c)
RemainingLiquid = VolumeOfSilo - vol(c) ; % remaining liters after pouring
VolumeOfSilo = RemainingLiquid; % update the liters remaining in silo
FullContainers = c ;
end
if VolumeOfSilo < vol(c) % help me to avoid using break
% break
term_while = false; % set flag variable to false so the loop stops
end
end
end

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by