-5
if (grade >= 75) {
    printf("\nPASSING");
    getch();
    goto Sol;

    if (grade >= 90) {
        printf("\nWITH HONORS");
        getch();
        goto Sol;

        if (grade >= 95) {
            printf("\nWITH HIGH HONORS");
            getch();
            goto Sol;

            if (grade >= 98) {
                printf("\nWITH HIGHEST HONORS");
                getch();
                goto Sol;

                if (grade >= 101) {
                    printf("INVALID");
                    getch();
                    goto Sol;
                }
            }
        }
    }
    else {
        printf("FAILURE");
        getch();
        goto Sol;
    }

This is my code so far but it won't work. What should I do to make it work?

letsintegreat
  • 3,328
  • 4
  • 18
  • 39
H2WO
  • 31
  • 7
  • 4
    `if ( ... ) { ... } else if (...) { ... } else if (...) { ....} else {...}` – KamilCuk Jan 28 '21 at 09:56
  • 5
    Don't use goto statement, For a beginner it might be tempting but as you start learning more, NOT using jump will make your life easier. – Mukul Kumar Jan 28 '21 at 09:57
  • 8
    If you follow @KamilCuk's approach, make sure to start with the highest grade. Oh yeah, and don't use `goto` :) – Robby Cornelissen Jan 28 '21 at 09:57
  • After a quick google search, I found a place which explains if - else to beginners [try this](https://www.programiz.com/c-programming/c-if-else-statement) – Mukul Kumar Jan 28 '21 at 10:09
  • My hunch would be that once you switch to an `if {} else` chain, your "need" for `goto` will be obviated completely, and a regular old loop will do. – StoryTeller - Unslander Monica Jan 28 '21 at 10:16
  • Think logically. If somethinf is larger than 101 then for **sure** it larger than 75. So any grade >= 75 will only will be checked by the first if. You need to check order to the ifs. – 0___________ Jan 28 '21 at 10:53
  • Do nor use goto. Read in any C book about conditional execution., – 0___________ Jan 28 '21 at 10:54
  • Also, don't learn C programming by using some MS DOS libraries from 1989. – Lundin Jan 28 '21 at 11:36
  • What should your program output if `grade == 98`? Should it output "WITH HIGHEST HONORS" or "PASSING WITH HIGHEST HONORS". Two answers seem to assume the former, whereas two other answers assume the latter. If you [edit] your question in order to clarify this, then I will vote to reopen your question. – Andreas Wenzel Jan 28 '21 at 12:02
  • Thanks for the comments and help guys I already fixed my code this morning – H2WO Jan 29 '21 at 05:56
  • the code that @KamilCuk commented was the first thing I did but it did not work...or maybe I was the one who coded it wrong? but thank you for the help tho I already fixed it earlier this morning with the help of my cousin – H2WO Jan 29 '21 at 05:58
  • also I would like to ask why is using `goto` bad? because here in my school the teacher is advising us to use that – H2WO Jan 29 '21 at 05:59

4 Answers4

1

There are multiple problems in your current code:

  • you use goto, which is unnecessary and considered bad practice in most cases.

  • the tests are in an incorrect order as every score greater or equal to 75 will be reported as PASSED but will not be tested for greater praise.

  • also improve consistency in the newline usage.

Here is a modified version:

printf("\nGRADUATION STATUS: ");
if (grade >= 101) {
    printf("INVALID\n");
} else
if (grade >= 98) {
    printf("PASSING WITH HIGHEST HONORS\n");
} else
if (grade >= 95) {
    printf("PASSING WITH HIGH HONORS\n");
} else
if (grade >= 90) {
    printf("PASSING WITH HONORS\n");
} else
if (grade >= 75) {
    printf("PASSING\n");
} else {
    printf("FAILURE\n");
}
getch();
chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • @Blastfurnace: indeed... fixed now – chqrlie Jan 28 '21 at 13:18
  • so this is how you should do it...i used chains of if statement in my code no wonder mine looked so complicated – H2WO Jan 29 '21 at 06:04
  • also I would like to ask why is using `goto` bad? because here in my school our teacher recommends us to use that to save time because we are using TURBO C++ to code – H2WO Jan 29 '21 at 06:07
  • @H2WO: See my answer for links to discussions about why `goto` is bad in most situations. Basically, you should only use it when there is no better alternative (which is rather seldom). In most cases, you can use a `for`, `while`, or `do`...`while` loop instead, and use the `continue` and `break` statements instead. Since you did not show us the `Sol` label, I can't tell you if it is necessary in this case. – Andreas Wenzel Jan 29 '21 at 10:18
1

I believe you are looking for the following solution:

if ( grade >= 101 ) {
    printf( "INVALID\n" );
}
else if ( grade >= 75 ) {
    printf( "PASSING" );
    if ( grade >= 98 ) {
        printf( " WITH HIGHEST HONORS" );
    }
    else if ( grade >= 95 ) {
        printf( " WITH HIGH HONORS" );
    }
    else if ( grade >= 90 ) {
        printf( " WITH HONORS" );
    }
    printf( "\n" );
}
else {
    printf( "FAILURE\n" );
}
getch();

Note that it is normal to output \n at the end of the output, not at the start.

There is no need to call the function getch once in every code branch. It is sufficient to call it once at the end.

It is also worth pointing out that when learning programming, you should generally try to avoid the use of goto, as its use is considered bad practice in most situations. There are only a few cases in which the use of goto is considered appropriate. See the following links for further information: 1 2 3

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
  • thanks for the help the code that I passed to my teacher was so long and complicated didn't know that it can just be as simple as this one – H2WO Jan 29 '21 at 06:03
-1

I think you want the output to be:

    either
        PASSING
        (WITH HONORS or WITH HIGH HONORS or WITH HIGHEST HONORS)

    or
        FAILURE (for when grade is lower than 75)

    or
        INVALID

Now pay attention to the branching. You have total 3 main branch here. 1 is when the grade is above 75 2 is when the grade is below 75 3 is when the grade is either less than 0 or greater than 100

each of these branches define 1 if case. The branch 1 can further be broken down like this:

    if grade >= 75 PASSING
        if grade >= 98 WITH HIGHEST HONORS
        else if grade >= 95 WITH HIGH HONORS
        else if grade >= 90 WITH HONORS

This should be how you structure the if-else block.

Also a note about goto, it's a bad practice, generally you can achieve the same results using a loop with break statement

Arun123
  • 118
  • 6
  • Try to "rubber duck" debug your code. When it works post the answer - but not before. This "solution" will not do what you thing it will – 0___________ Jan 28 '21 at 10:56
  • I agree that `goto` is bad practice in this case. However, using `goto` is not always bad practice. See these links for further information: [1](https://stackoverflow.com/q/24451/12149471) [2](https://stackoverflow.com/q/46586/12149471) [3](https://codereview.stackexchange.com/q/250656/235181) – Andreas Wenzel Jan 28 '21 at 11:02
  • is using a code like `if grade >= 90 WITH HONORS else if grade >= 95 WITH HIGH HONORS else if grade >= 98 WITH HIGHEST HONORS` the same as your code? or the order of the if statements affect your code? – H2WO Jan 29 '21 at 06:12
  • think of it this way, you are matching a value to a range of possible values. So if you start checking with the lowest range first, you are never going to reach the higher ranges. So say you are checking for `>=90` as your first condition. a number which is 91 as well as a number which is 98 both will satisfy this. but you want 98 to be evaluated differently than 91. hence you always go with the range checks in descending order. – Arun123 Jan 29 '21 at 10:03
-1

There are few things here you could do to improve the clarity of the code. First, goto statements are not really what you want to be using in this situation (see comments to your question). Secondly, the logic in your code is broken.

If you want to use if statements, consider adding else if that will change how the branches of the program are traversed; to make things work, you will also need to change the boundaries that you use for different "grades":

if (grade >= 75 && grade < 90) {
    printf("\nPASSING");
} else if (grade >= 90 && grade < 95) {
   printf("\nWITH HONORS");
} else if (grade >=95 && grade < 98) {
   printf("\nWITH HIGH HONORS");
} else if (grade >= 98 && grade <= 100) {  // note <= 100 rather than < 101, since 100 is still a valid grade
   printf("\nWITH HIGHEST HONORS");
} else if (grade > 100) { // again, most likely you want to treat anything above 100 as invalid
   printf("INVALID");
} else {
   printf("FAILURE");
}
getch();

Finally, you might want to encapsulate these statements within a function and return the value, as opposed to printing it directly. This will help you navigate the code later on, without having to ever think about goto statements.

Marceli Wac
  • 375
  • 3
  • 13
  • 1
    If you switch the order of your conditions, you can avoid needing to check for an upper boundary. Start with highest value and continue towards lower. – Gerhardh Jan 28 '21 at 11:31
  • Although the OP did not express themselves clearly, I believe it is the intent of the OP to output "PASSING WITH HIGHEST HONORS" instead of only "WITH HIGHEST HONORS". In that case, both cases must be combined. See my answer for a solution which does this. – Andreas Wenzel Jan 28 '21 at 11:48
  • @AndreasWenzel I think that's one of the possiblities, but considering the fact that they're printing the outputs separately in the original question (`PASSING` and `WITH HIGHEST HONOURS` are two separate conditions that are not executed after another), my answer deliberately does not concatenate PASSING with anything else. Likewise, if that was the case I believe introducing variable that holds the value that is printed later would have been a better choice. – Marceli Wac Jan 29 '21 at 16:45
  • @Gerhardh - I agree, re-ordering statements would have been more sensible choice. At the moment, this is however closest to the structure in the asked question and there probably is some value in pointing out that just looking at the lower bounds (as in original question) wouldn't result in desired outcome, as all the answers would be printed. Reversing the order is definitely the way to go, but there is a chance that the reason behind it might not be clear, or be confusing to the OP. I won't edit my answer since there is already answer that does that. Thanks for pointing it out though! – Marceli Wac Jan 29 '21 at 16:45