# for-loop from an if-statement

8 views (last 30 days)
Spaceman on 21 Mar 2024
Commented: Spaceman on 30 Mar 2024
Given: Use previous code to make a for-loop
Find: Prompt the user to enter the number of grades (N) that they are trying to classify; use a for-loop to allow the user to enter and classify a total of N grades, (I will need a loop variable and will need to provide it with row vector of values, such that the loop will run N times); when run your code should ask the user (N times) to enter a percentage; after entering each grade, the letter grade should be displayed to the user. After getting this to run I will have to find a way to save each percentage in a vector and each letter grade returned in a string array, (strings function).
Issue: How do I turn my code into a for loop while keeping all architecture of my if-statement intact? I have a flowchart but I am under the impression I wont be able to use if, elseif, or else in the for-loop.
My solution: I have this code from the if-statement function I made:
grade=input('Enter a grade between 0 & 100: ');
disp('You got an A!!!')
disp('You got a B!')
disp('You got a C.')
disp('Got a D')
disp('You fail, F.')
else
disp('You entered an invalid percentage, try again')
end
##### 2 CommentsShow NoneHide None
Stephen23 on 22 Mar 2024
Edited: Stephen23 on 22 Mar 2024
There are many ways to approach this:
C = {'You fail, F.', 'Got a D', 'You got a C.', 'You got a B!', 'You got an A!!!'};
C{N}
ans = 'Got a D'
Spaceman on 30 Mar 2024
Genius. This is a beautiful way to do this as well. However, I have never dealt with the discretize function before.

Dinesh on 21 Mar 2024
Hello,
You can put all the if, else-if and else conditions within the for loop itself.
The following code runs for "N" times, once for each grade prompting the user to enter the grade in each iteration of the loop. The letter grades and percentages are all stored in vectors.
N = input('Enter the number of grades you are trying to classify: ');
grades = zeros(1, N);
letterGrades = strings(1, N);
for i = 1:N
grade = input('Enter a grade between 0 & 100: ');
if grade <= 100 && grade > 90
disp('You got an A!!!')
elseif grade < 90 && grade >= 80
disp('You got a B!')
elseif grade < 80 && grade >= 70
disp('You got a C.')
elseif grade < 70 && grade >= 60
disp('Got a D')
elseif grade < 60 && grade >= 0
disp('You fail, F.')
else
disp('You entered an invalid percentage, try again')
end
end
% Here, grades and letterGrades have the percentages and letter grades for
% all the entered N grades
If this is not what you are looking for, please let me know and I will look into this further.
Spaceman on 21 Mar 2024
Eureka! This is beautiful. Thank you so much. I have been up until 5am every night racking my brain with these practice problems. I am hoping eventually it will get easier and I will intuitively just know what to do based on what the prompt is asking. :3

### More Answers (1)

Hassaan on 21 Mar 2024
Edited: Hassaan on 21 Mar 2024
% Prompt the user to enter the number of grades
N = input('Enter the number of grades you want to classify: ');
% Initialize a vector to save the percentages
percentages = zeros(1, N);
% Initialize a string array to save the letter grades
letterGrades = strings(1, N);
% Use a for-loop to prompt for grades and classify them
for i = 1:N
grade = input('Enter a grade between <0 & 100>: ');
percentages(i) = grade; % Save the percentage
if grade <= 100 && grade > 90
disp('You got an A!!!');
elseif grade <= 90 && grade > 80
disp('You got a B!');
elseif grade <= 80 && grade > 70
disp('You got a C.');
elseif grade <= 70 && grade > 60
disp('Got a D');
elseif grade <= 60 && grade >= 0
disp('You fail, F.');
else
disp('You entered an invalid percentage, please try again');
percentages(i) = NaN; % Indicate invalid entry
% Optional: Decrement i to retry entering a valid grade
% i = i - 1; % Uncommenting this line may require changing loop structure
end
end
% Display the percentages and their corresponding letter grades
disp('Percentages:');
disp(percentages);
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Professional Interests
• Technical Services and Consulting
• Embedded Systems | Firmware Developement | Simulations
• Electrical and Electronics Engineering
Feel free to contact me.
Spaceman on 22 Mar 2024
Genius. This is such a beautiful answer as well. I liked the displaying of ALL the results at the end in a digestible manner. How would the loop need to be restructured to include the line of code:
i=i-1
such that the user would have to re-enter a correct input if they had accidently typed the wrong number and hit enter initially?

### Categories

Find more on Just for fun in Help Center and File Exchange

R2023b

### Community Treasure Hunt

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

Start Hunting!