Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
108 views
in Technique[技术] by (71.8m points)

How do you do a chain of if statements in C?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...