How to create a loop?

2 views (last 30 days)
John Carlo
John Carlo on 12 Jan 2023
Edited: VBBV on 12 Jan 2023
How can I loop this code such that after getting the answer the code should re run by itself?
clear all
clc
format longG;
format compact;
txt = input ('Choose a Production Period: 1) Annual 2) Monthly 3) Weekly 4) Daily');
if txt == 1
prompt = "Input Annual Production Target:";
ProdTarget = input(prompt)
disp("The production target (MT) per shift should be")
ProdTarget_PerShift = ProdTarget/10/4/7/2
elseif txt == 2
prompt = "Input Monthly Production Target:";
ProdTarget = input(prompt)
disp("The production target (MT) per shift should be")
ProdTarget_PerShift = ProdTarget/4/7/2
elseif txt == 3
prompt = "Input Weekly Production Target:";
ProdTarget = input(prompt)
disp("The production target (MT) per shift should be")
ProdTarget_PerShift = ProdTarget/7/2
elseif txt == 4
prompt = "Input Daily Production Target:";
ProdTarget = input(prompt)
disp("The production target (MT) per shift should be")
ProdTarget_PerShift = ProdTarget/2
else
disp("INVALID")
end

Answers (1)

VBBV
VBBV on 12 Jan 2023
Edited: VBBV on 12 Jan 2023
clear all
clc
format longG;
format compact;
txt = input ('Choose a Production Period: 1) Annual 2) Monthly 3) Weekly 4) Daily');
if txt == 1
prompt = "Input Annual Production Target:";
ProdTarget = input(prompt)
disp("The production target (MT) per shift should be")
ProdTarget_PerShift = ProdTarget/10/4/7/2
while ProdTarget_PerShift > Value1 & ProdTarget_PerShift < Value2 % Value1 and Value2 are limiting values for ...
% variable %
ProdTarget_PerShift = ProdTarget/factor; % factor is division factor for given condition
end
elseif txt == 2
prompt = "Input Monthly Production Target:";
ProdTarget = input(prompt)
disp("The production target (MT) per shift should be")
ProdTarget_PerShift = ProdTarget/4/7/2
% similarly for this input
while ProdTarget_PerShift > Value1 & ProdTarget_PerShift < Value2 % Value1 and Value2 are limiting values for ...
% variable %
ProdTarget_PerShift = ProdTarget/factor; % factor is division factor
end
elseif txt == 3
prompt = "Input Weekly Production Target:";
ProdTarget = input(prompt)
disp("The production target (MT) per shift should be")
ProdTarget_PerShift = ProdTarget/7/2
while ProdTarget_PerShift > Value1 & ProdTarget_PerShift < Value2 % Value1 and Value2 are limiting values for ...
% variable %
ProdTarget_PerShift = ProdTarget/factor; % factor is division factor
end
elseif txt == 4
prompt = "Input Daily Production Target:";
ProdTarget = input(prompt)
disp("The production target (MT) per shift should be")
ProdTarget_PerShift = ProdTarget/2
while ProdTarget_PerShift > Value1 & ProdTarget_PerShift < Value2 % Value1 and Value2 are limiting values for ...
% variable %
ProdTarget_PerShift = ProdTarget/factor; % factor is division factor
end
else
disp("INVALID")
end
  3 Comments
VBBV
VBBV on 12 Jan 2023
Edited: VBBV on 12 Jan 2023
while 1 % this is another way
txt = input ('Choose a Production Period: 1) Annual 2) Monthly 3) Weekly 4) Daily');
if txt == 1
prompt = "Input Annual Production Target:";
ProdTarget = input(prompt)
disp("The production target (MT) per shift should be")
ProdTarget_PerShift = ProdTarget/10/4/7/2
if ProdTarget_PerShift > Value1 & ProdTarget_PerShift < Value2 % Value1 and Value2 are limiting values for ...
% variable %
ProdTarget_PerShift = ProdTarget/factor; % factor is division factor for given condition
else
break
end
elseif txt == 2
prompt = "Input Monthly Production Target:";
ProdTarget = input(prompt)
disp("The production target (MT) per shift should be")
ProdTarget_PerShift = ProdTarget/4/7/2
% similarly for this input
if ProdTarget_PerShift > Value1 & ProdTarget_PerShift < Value2 % Value1 and Value2 are limiting values for ...
% variable %
ProdTarget_PerShift = ProdTarget/factor; % factor is division factor for given condition
else
break
end
elseif txt == 3
prompt = "Input Weekly Production Target:";
ProdTarget = input(prompt)
disp("The production target (MT) per shift should be")
ProdTarget_PerShift = ProdTarget/7/2
if ProdTarget_PerShift > Value1 & ProdTarget_PerShift < Value2 % Value1 and Value2 are limiting values for ...
% variable %
ProdTarget_PerShift = ProdTarget/factor; % factor is division factor for given condition
else
break
end
elseif txt == 4
prompt = "Input Daily Production Target:";
ProdTarget = input(prompt)
disp("The production target (MT) per shift should be")
ProdTarget_PerShift = ProdTarget/2
if ProdTarget_PerShift > Value1 & ProdTarget_PerShift < Value2 % Value1 and Value2 are limiting values for ...
% variable %
ProdTarget_PerShift = ProdTarget/factor; % factor is division factor for given condition
else
break
end
else
disp("INVALID")
end
end %
VBBV
VBBV on 12 Jan 2023
There are several ways in which you can re-run the code, e.g shown above is another method

Sign in to comment.

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!