How to create a program that repeats itself?

I want this program to repeat itself after giving an output (ask input again and calculate again). I don't know how to do that can someone help me?
t=36; %total number of lectures
b=0;
while b==0
p=input('Enter Number of Lectures Attended \n');
if p>36
fprintf('Error, note that the total number of lectures are 36. \n')
else
b=1;
end
end
a=(p/t)*100; % this calculates the percentage attendance
if a>=75
fprintf('The student does not have short attendace. \n')
else
fprintf('The student has short attendace. \n')
end

 Accepted Answer

Try this:
% Demo by Image Analyst.
clc; % Clear the command window.
fprintf('Beginning to run %s.m ...\n', mfilename);
close all; % Close all figures (except those of imtool.)
clearvars;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 16;
buttonText = '';
totalNumberOfLectures=36; % Total (max) number of lectures
while ~contains(buttonText, 'Quit', 'IgnoreCase', true)
numLecturesAttended = -1; % Initialize
while numLecturesAttended < 0 || numLecturesAttended > totalNumberOfLectures
numLecturesAttended=input('Enter Number of Lectures Attended : ');
if numLecturesAttended < 0 || numLecturesAttended > totalNumberOfLectures
fprintf('Error. Note that the total number of lectures is %d.\nEnter a number between 0 and %d (inclusive).\n', ...
totalNumberOfLectures, totalNumberOfLectures)
end
end
percentAttended = (numLecturesAttended/totalNumberOfLectures) * 100; % This calculates the percentage attendance
if percentAttended >= 75
fprintf('The student does not have short attendance. \n')
else
% Did not attend at least 75% of their classes.
fprintf('The student has short attendance. \n')
end
% See if they want to do another one.
promptMessage = sprintf('Do you want to ask again,\nor Quit processing?');
titleBarCaption = 'Continue?';
buttonText = questdlg(promptMessage, titleBarCaption, 'Yes - Ask Again', 'No - Quit', 'Yes - Ask Again');
if contains(buttonText, 'Quit', 'IgnoreCase', true)
break;
end
end
fprintf('Done running %s.m\n', mfilename);

More Answers (0)

Categories

Find more on Scripts in Help Center and File Exchange

Products

Release

R2020a

Community Treasure Hunt

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

Start Hunting!