Can't run my 'if, elseif, else' code

1 visualizzazione (ultimi 30 giorni)
Waqar
Waqar il 15 Mar 2023
Commentato: Dyuman Joshi il 15 Mar 2023
score= randi(100);
if score < 20
grade= 'D';
print grade
elseif score < 40
grade= 'C';
print grade
elseif score < 60
grade= 'B';
print grade
elseif score < 80
grade= 'B+';
print grade
else
grade= 'A';
print grade
end
i tried to run a basic if else loop but no matter what score i input in the command wndow, my answer is always 'A'.

Risposta accettata

KSSV
KSSV il 15 Mar 2023
score= randi(100);
if score < 20
grade = 'D';
elseif score < 40
grade= 'C';
elseif score < 60
grade= 'B';
elseif score < 80
grade= 'B+';
else
grade= 'A';
end
disp(grade)

Più risposte (1)

Dyuman Joshi
Dyuman Joshi il 15 Mar 2023
Modificato: Dyuman Joshi il 15 Mar 2023
if-else is not a loop, they are conditional statements.
If you want to print/display something, use sprintf or fprintf or disp. However, if you want see the value of a variable, type the variable name without using semi colon
score= randi(100)
score = 52
if score < 20
grade= 'D';
elseif score < 40
grade= 'C';
elseif score < 60
grade= 'B';
elseif score < 80
grade= 'B+';
else
grade= 'A';
end
grade
grade = 'B'
  6 Commenti
Waqar
Waqar il 15 Mar 2023
Worked! Thanks a lot!
Dyuman Joshi
Dyuman Joshi il 15 Mar 2023
You are welcome!

Accedi per commentare.

Categorie

Scopri di più su Downloads in Help Center e File Exchange

Prodotti


Release

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by