How to create a loop until the condition is met?

1 Ansicht (letzte 30 Tage)
Glyzel Ann Madlangbayan
Glyzel Ann Madlangbayan am 21 Jul. 2021
Bearbeitet: Jonas am 21 Jul. 2021
I would like to create a loop wherein whenever the user input a date and the slot is already full, the loop would go back to the "choose date" again and again until the user find a date that is not full and still available.
%choose date
disp ('Choose on the following dates (MMDDYY)')
disp ('Aug0221 Aug0921 Aug1621 Aug2321 Aug3021')
date = input('Enter date of first dose (AugDDYY):', 's');
if date == 'Aug0221'
disp ('Date of first dose: Aug0221')
fprintf(1, '\n');
%assign a slot
slots_a1(index_a1)=1;
sum_a1=sum_a1+1;
if sum_a1 <= 2
%print reservation
disp ('COVID-19 Vaccine Reservation')
fprintf ('Name: %s\n', name);
fprintf ('Contact Number: %.f\n', contactno);
fprintf ('Address: %s\n', address);
disp('Vaccination Site: Aguinaldo Elementary School');
fprintf('Date of Vaccination: %s\n', date');
fprintf('Preferred Vaccine: %s\n', vaccine');
disp(['Slot number: ', num2str(index_a1)]);
%represent slot chart
slot_charta1=slots_a1
index_a1=index_a1+1;
else
fprintf('<strong>Sorry, your preferred date of vaccination is already full. Kindly choose another date that is available for you.</strong>\n');
fprintf(1, '\n');
end
elseif date == 'Aug0921'
disp ('Date of first dose: Aug0921')
fprintf(1, '\n');
%assign a slot
slots_a2(index_a2)=1;
sum_a2=sum_a2+1;
if sum_a2 <= 2
%print reservation
disp ('COVID-19 Vaccine Reservation')
fprintf ('Name: %s\n', name);
fprintf ('Contact Number: %.f\n', contactno);
fprintf ('Address: %s\n', address);
disp('Vaccination Site: Aguinaldo Elementary School');
fprintf('Date of Vaccination: %s\n', date');
fprintf('Preferred Vaccine: %s\n', vaccine');
disp(['Slot number: ', num2str(index_a2)]);
%represent slot chart
slot_charta2=slots_a2
index_a2=index_a2+1;
else
fprintf('<strong>Sorry, your preferred date of vaccination is already full. Kindly choose another date that is available for you.</strong>\n');
fprintf(1, '\n');
end
elseif date == 'Aug1621'
disp ('Date of first dose: Aug1621')
fprintf(1, '\n');
%assign a slot
slots_a3(index_a3)=1;
sum_a3=sum_a3+1;
if sum_a3 <= 2
%print reservation
disp ('COVID-19 Vaccine Reservation')
fprintf ('Name: %s\n', name);
fprintf ('Contact Number: %.f\n', contactno);
fprintf ('Address: %s\n', address);
disp('Vaccination Site: Aguinaldo Elementary School');
fprintf('Date of Vaccination: %s\n', date');
fprintf('Preferred Vaccine: %s\n', vaccine');
disp(['Slot number: ', num2str(index_a3)]);
%represent slot chart
slot_charta3=slots_a3
index_a3=index_a3+1;
else
fprintf('<strong>Sorry, your preferred date of vaccination is already full. Kindly choose another date that is available for you.</strong>\n');
fprintf(1, '\n');
end
elseif date == 'Aug2321'
disp ('Date of first dose: Aug2321')
fprintf(1, '\n');
%assign a slot
slots_a4(index_a4)=1;
sum_a4=sum_a4+1;
if sum_a4 <= 2
%print reservation
disp ('COVID-19 Vaccine Reservation')
fprintf ('Name: %s\n', name);
fprintf ('Contact Number: %.f\n', contactno);
fprintf ('Address: %s\n', address);
disp('Vaccination Site: Aguinaldo Elementary School');
fprintf('Date of Vaccination: %s\n', date');
fprintf('Preferred Vaccine: %s\n', vaccine');
disp(['Slot number: ', num2str(index_a4)]);
%represent slot chart
slot_charta4=slots_a4
index_a4=index_a4+1;
else
fprintf('<strong>Sorry, your preferred date of vaccination is already full. Kindly choose another date that is available for you.</strong>\n');
fprintf(1, '\n');
end
elseif date == 'Aug3021'
disp ('Date of first dose: Aug3021')
fprintf(1, '\n');
%assign a slot
slots_a5(index_a5)=1;
sum_a5=sum_a5+1;
if sum_a5 <= 2
%print reservation
disp ('COVID-19 Vaccine Reservation')
fprintf ('Name: %s\n', name);
fprintf ('Contact Number: %.f\n', contactno);
fprintf ('Address: %s\n', address);
disp('Vaccination Site: Aguinaldo Elementary School');
fprintf('Date of Vaccination: %s\n', date');
fprintf('Preferred Vaccine: %s\n', vaccine');
disp(['Slot number: ', num2str(index_a5)]);
%represent slot chart
slot_charta5=slots_a5
index_a5=index_a5+1;
else
fprintf('<strong>Sorry, your preferred date of vaccination is already full. Kindly choose another date that is available for you.</strong>\n');
fprintf(1, '\n');
end
end

Antworten (1)

Jonas
Jonas am 21 Jul. 2021
Bearbeitet: Jonas am 21 Jul. 2021
add the follwing at the beginning
infLoop=1;
while infLoop
infLoop=0;
% your code
and that to the end of your code
fprintf('<strong>Sorry, your preferred date of vaccination is already full. Kindly choose another date that is available for you.</strong>\n');
fprintf(1, '\n');
infLoop=1; % add this
end
end
end % add this

Kategorien

Mehr zu Epidemiology finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by