Clear Filters
Clear Filters

Part of my program wont work and i just cant figure it out why.

1 view (last 30 days)
The question is "The program asks for an input of a score(0-100). If the score is greater than or equal to 60, display “Congratulations! You passed!” Otherwise, display “Sorry, you failed.” "
prompt='score greater than 60 and less than 60'
x=input(prompt)
if x>=60
if x<60
end
if x<60
disp('Sorry, you failed.')
end
end
if x>=60
disp('Congratulations! You passed!')
end
when i run the program i get the "Congratulations! You passed!" for anything above 60 but when i run the program for # less 60 nothing happens. i just get the inputted number back without the disp.

Accepted Answer

Walter Roberson
Walter Roberson on 4 Oct 2019
Look more carefully at your code. You have
if x>=60
statements between that and the next matching else or elseif or end statement are only executed if x is greater than or equal to 60
if x<60
end
if x<60
disp('Sorry, you failed.')
end
including those ones. You do not reach those statements unles x>=60 and if x>=60 then x<60 cannot possibly be true.
if rand*100 > 60
disp('Hip Hip Ho!')
else
disp('Hop Hep Hey!')
end

More Answers (0)

Categories

Find more on Entering Commands in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!