+ 2

is there difference between these two codes ? and if there is which is the more professional ? or write the the best than them

float grade ; cout<<"please enter your grade " ; cin>>grade ; if(grade >=50 && grade <=100) { if (grade<=59) { cout<<"F \n" ; } else if (grade<=69) { cout<<"D \n" ; } else if (grade<=79) {cout<<"C \n" ; } else if (grade<=89) {cout<<"B \n"; } else if (grade<100) { cout<<"A \n" ; } else if (grade=100) {cout<<"Perfect \n" ; } } else if (grade <50 && grade >=0) { cout<<"sorry you have not passed \n " ; } else {cout<<"Error \n";} --------------------------------------------------------------------- if (grade >=50&&grade<=59) { cout<<"F \n" ; } else if (grade>=60 &&grade<=69) { cout<<"D \n" ; } else if ( grade>=70 && grade<=79) {cout<<"C \n" ; } else if (grade>=80 && grade<=89) {cout<<"B \n"; } else if (grade>=90 && grade<100) { cout<<"A \n" ; } else if (grade=100) {cout<<"Perfect \n" ; } else if (grade <50 && grade >=0) { cout<<"sorry you have not passed \n " ;} else {cout<<"Error \n";}

1st Sep 2018, 8:54 AM
Omar
Omar - avatar
1 Réponse
+ 5
Omar first one is not better in my opinion as it has nested if....second one is better , but it again depends on scenarioes... for case of not passed, first scenario is best... Because two case are to be evaluated... in other scenario, all above cases would be evaluated for not passed case... so, IMHO I would prefer case 2...
1st Sep 2018, 12:53 PM
Ketan Lalcheta
Ketan Lalcheta - avatar