please help with this If else statement

clear all
clc
Toefl=input('Toefl=')
Math=input('Mathematic=')
Bio=input('Biologi=')
if(Toefl >= 601) && (Toefl <= 670)
disp ('Excellent')
if(Math >= 601) && (Math <= 670)
disp ('Excellent')
if(Bio >= 601) && (Bio <= 670)
disp ('Excellent')
elseif(Bio >= 501) && (Bio <= 600)
disp ('Good')
elseif(Bio >= 401) && (Bio <= 500)
disp ('Average')
elseif Bio <= 400
disp ('Bad')
else
disp ('Data tidak valid')
end
elseif(Math >= 501) && (Math <= 600)
disp ('Good')
if(Bio >= 601) && (Bio <= 670)
disp ('Excellent')
elseif(Bio >= 501) && (Bio <= 600)
disp ('Good')
elseif(Bio >= 401) && (Bio <= 500)
disp ('Average')
elseif Bio <= 400
disp ('Bad')
else
disp ('Data tidak valid')
end
elseif(Math >= 401) && (Math <= 500)
disp ('Average')
if(Bio >= 601) && (Bio <= 670)
disp ('Excellent')
elseif(Bio >= 501) && (Bio <= 600)
disp ('Good')
elseif(Bio >= 401) && (Bio <= 500)
disp ('Average')
elseif Bio <= 400
disp ('Bad')
else
disp ('Data tidak valid')
end
elseif Math <= 400
disp ('Bad')
if(Bio >= 601) && (Bio <= 670)
disp ('Excellent')
elseif(Bio >= 501) && (Bio <= 600)
disp ('Good')
elseif(Bio >= 401) && (Bio <= 500)
disp ('Average')
elseif Bio <= 400
disp ('Bad')
else
disp ('Data tidak valid')
end
else
disp('Data tidak valid')
if(Bio >= 601) && (Bio <= 670)
disp ('Excellent')
elseif(Bio >= 501) && (Bio <= 600)
disp ('Good')
elseif(Bio >= 401) && (Bio <= 500)
disp ('Average')
elseif Bio <= 400
disp ('Bad')
else
disp ('Data tidak valid')
end
end
elseif (Toefl >= 501) && (Toefl <= 600)
disp ('Good')
elseif (Toefl >= 401) && (Toefl <= 500)
disp ('Average')
elseif Toefl <= 400
disp ('Bad')
else
disp('Data tidak Valid')
end
how to summarize this code
the asked results maybe looks like this
Toefl=602
Toefl =
602
Mathematic=602
Math =
602
Biologi=602
Bio =
602
Excellent
Excellent
Excellent

6 个评论

Is that the required output you want to get?
I would suggest you to have separate if block statement for each subject instead of nesting it with each other, hence organising your datas neatly.
i have tried. But my lecturer said that forbidden. what your opinion?
Could you post the question which was given to you?
the question just spoken in class. It's like quiz that you may skip the exam when you've done it.
the asked question is make code with 3 different input and make the 3 ouput using if else statement in matlab. forbidden to use like what you've said. it's ike grading in student's report.
i've the code and the good result. But it's use C++ code. here the code
#include <iostream>
#include <string>
using namespace std;
string cekNilai(string nilaitoefl) {
int nilaitoefl_int = stoi(nilaitoefl);
string keterangan = "";
if (nilaitoefl_int >= 601 && nilaitoefl_int <= 670) {
keterangan = "excellent";
}
else if (nilaitoefl_int >= 501 && nilaitoefl_int <= 600) {
keterangan = "good";
}
else if (nilaitoefl_int >= 401 && nilaitoefl_int <= 500) {
keterangan = "avg";
}
else if (nilaitoefl_int <= 400) {
keterangan = "bad";
}
else {
keterangan = "invalid input";
}
return keterangan;
}
int main()
{
string toefl1, toefl2, toefl3;
cout << "masukan toefl1: ";
cin >> toefl1;
cout << "masukan toefl2: ";
cin >> toefl2;
cout << "masukan toefl3: ";
cin >> toefl3;
cout << "-------------hasil----------" << endl;
cout << "Toefl-1: " << cekNilai(toefl1) << endl;
cout << "Toefl-2: " << cekNilai(toefl2) << endl;
cout << "Toefl-3: " << cekNilai(toefl3) << endl;
getchar();
getchar();
return 0;
}
i wonder matlab can make this code
:)

请先登录,再进行评论。

 采纳的回答

Jan
Jan 2019-4-13
编辑:Jan 2019-4-15
Similar to your C++ code, just with some shorter names for variables to reduce the chance for typos:
function main
toefl1 = input('masukan toefl1: ');
toefl2 = input('masukan toefl2: ');
toefl3 = input('masukan toefl3: ');
disp(cekNilai(toefl1));
disp(cekNilai(toefl2));
disp(cekNilai(toefl3));
end
function r = cekNilai(n)
if n >= 601 && n <= 670
r = 'excellent';
elseif n >= 501 && n <= 600
r = 'good';
elseif n >= 401 && n <= 500
r = 'avg';
elseif n <= 400
r = 'bad';
else
r = 'invalid input';
end
end

10 个评论

Toefl1=602
Toefl2=602
Toefl3=602
Excellent
Excellent
Excellent
I've tried and the result has appeared. Thank you very much.
But there is missing piece that the output hopefully like this
Toefl1=602
Toefl2=602
Toefl3=602
Your Toefl1 grade is Excellent
Your Toefl2 grade is Excellent
Your Toefl3 grade is Excellent
how can i add it to my code.
:)
disp(['Your Toefl1 grade is ', cekNilai(toefl1)])
Thank you very much all.
i have question. can we use just 1 'end'
in the code there are 3 'end' can we use just 1 'end'? if we can use 1 'end', how will the code will be?
@Noah Jacob: The end closes if commands, loops and functions. It is not really meaningful to try to reduce the number of end statements. Of course you can write the code in one script, but this is no advantage for clarity or processing time.
% Script
check = @(x) discretize(600, [-Inf, 0, 400, 500, 600, 670, Inf]);
grade = {'invalid', 'bad', 'avg', 'good', 'excellent', 'invalid'};
for k = 1:3
t = input(sprintf('masukan toefl%d: ', k));
s = grade{check(t)};
sprintf('Your Toefl%d grade is %s\n', t, s);
end
One end only, but it is much more important, that the smarter distcretize is used, which reduces the chance for typos in the if conditions.
i see.
is possible turn the code without function with the same output and how code will be i wonder.
the meaning just 1 'end' is that the code only use if else statement. function is forbidden cause it will make more 'end' in the code. i'm sorry that i miss one term of the quiz that the code ONLY use if else statement, not other (function, loop, etc)
here is the code using C++. how can i convert to matlab's code
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
string toefl1, toefl2, toefl3;
cout << "input toefl1: ";
cin >> toefl1;
cout << "input toefl2: ";
cin >> toefl2;
cout << "input toefl3: ";
cin >> toefl3;
cout << "-------------result----------" << endl;
//result//
string ket_1 = "";
string ket_2 = "";
string ket_3 = "";
int valuetoefl1_int = stoi(toefl1);
int valuetoefl2_int = stoi(toefl2);
int valuetoefl3_int = stoi(toefl3);
bool toefl1_score_excellent = valuetoefl1_int >= 601 && valuetoefl1_int <= 670;
bool toefl1_score_good = valuetoefl1_int >= 501 && valuetoefl1_int <= 600;
bool toefl1_score_avg = valuetoefl1_int >= 401 && valuetoefl1_int <= 500;
bool toefl1_score_bad = valuetoefl1_int <= 400;
bool toefl2_score_excellent = valuetoefl2_int >= 602 && valuetoefl2_int <= 670;
bool toefl2_score_good = valuetoefl2_int >= 502 && valuetoefl2_int <= 600;
bool toefl2_score_avg = valuetoefl2_int >= 402 && valuetoefl2_int <= 500;
bool toefl2_score_bad = valuetoefl2_int <= 400;
bool toefl3_score_excellent = valuetoefl3_int >= 603 && valuetoefl3_int <= 670;
bool toefl3_score_good = valuetoefl3_int >= 503 && valuetoefl3_int <= 600;
bool toefl3_score_avg = valuetoefl3_int >= 403 && valuetoefl3_int <= 500;
bool toefl3_score_bad = valuetoefl3_int <= 400;
if(toefl1_score_excellent && toefl2_score_excellent && toefl3_score_excellent){
ket_1 = "excellent";
ket_2 = "excellent";
ket_3 = "excellent";
}else if(toefl1_score_good && toefl2_score_excellent && toefl3_score_excellent){
ket_1 = "good";
ket_2 = "excellent";
ket_3 = "excellent";
}else if(toefl1_score_good && toefl2_score_good && toefl3_score_excellent){
/******continue**********/
}
cout << "Toefl-1: " << ket_1 << endl;
cout << "Toefl-2: " << ket_2 << endl;
cout << "Toefl-3: " << ket_3 << endl;
getchar();
getchar();
return 0;
}
Take Jan's code and remove the end before the function statement and remove the end at the bottom. There will now be only a single end in it, closing the if statement.
function main
toefl1 = input('masukan toefl1: ');
toefl2 = input('masukan toefl2: ');
toefl3 = input('masukan toefl3: ');
disp(cekNilai(toefl1));
disp(cekNilai(toefl2));
disp(cekNilai(toefl3));
function r = cekNilai(n)
if n >= 601 && n <= 670
r = 'excellent';
elseif n >= 501 && n <= 600
r = 'good';
elseif n >= 401 && n <= 500
r = 'avg';
elseif n <= 400
r = 'bad';
else
r = 'invalid input';
end
@walter is that what you mean?
Yes, other than the change I suggested earlier to
disp(['Your Toefl1 grade is: ', cekNilai(toefl1)])
WIth the single function arranged that way, you only have one "end" statement and so are within the restrictions you have told us about.
Note: if you were to remove the "function main" statement, converting this into a script, then you would need to move the function cekNilai into a separate file in order to maintain the "only one end statement" restriction. Functions that are in scripts need to have "end" statement that matches their "function" line, but functions that are in function-only files do not need that.
Thank you very much for your helping @jan & @walter.
i've submit it and hopefully my lecturer will admit it.
May god bless you two. :)

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Language Support 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by