Can anybody spot the error in this simple program?

Hey, this is probably going to be very obvious to you guys, but I have written this program to assign grades to students marks, and I can't seem to spot the error, it always seems to give me 'fail' and just keeps repeating fail over and over again!
score = input('What did the pupil score?');
while 100>= score >= 0
if 40>score>=0
disp('Fail');
end
if 50>score>=40
disp('Third');
end
if 70>score>=50
disp('Second');
end
if 100>=score>70;
disp('First');
end;
end;
Thank you in advance!

1 件のコメント

Daniel Shub
Daniel Shub 2012 年 3 月 21 日
I always go to http://matlab.wikia.com/wiki/FAQ looking for this question. It surprises me that it is not an FAQ yet.

サインインしてコメントする。

回答 (3 件)

Daniel Shub
Daniel Shub 2012 年 3 月 21 日

1 投票

This 100>= score >= 0 doesn't do what you think it does.
100>= score
either evaluates to 0 or 1, so you then compare
0>=0
or
1>=0
which are both true.
Wayne King
Wayne King 2012 年 3 月 21 日

1 投票

Daniel's absolutely correct. You're writing your inequalities like you would write them on a piece of paper, but MATLAB is a programming language
score = 0;
while (score>=0 && score <= 100)
score = input('What did the pupil score?\n');
if (40>score && score>=0)
disp('Fail');
end
if (50>score && score >=40 )
disp('Third');
end
if (70>score && score>=50)
disp('Second');
end
if (100>=score && score>=70)
disp('First');
end;
end;

1 件のコメント

Eike
Eike 2012 年 3 月 21 日
Since the time of my first C programs I'm waiting for the programming language that allows me to write my inequalities like Edward did... I mean how often do you come across such situations?
Anybody knows a language owning that feature? :)

サインインしてコメントする。

Edward
Edward 2012 年 3 月 21 日

1 投票

That's great thanks guys! If I knew how to, or could, I would give you rep!

1 件のコメント

Daniel Shub
Daniel Shub 2012 年 3 月 21 日
It is easy. Click the triangle next to "0 votes" to upvote and click the big box "accept answer" for the answer that answers your question.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeDownloads についてさらに検索

質問済み:

2012 年 3 月 21 日

Community Treasure Hunt

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

Start Hunting!

Translated by