Thread: else if statement with example

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    99

    how to print different status ?

    I want to write c program to display status of student. if student get less then 40 marks then print " Failed". if he get mark's 40-50 then print "average. if he get marks 50- 70 then print "good" if he get mark's 70-100 then print "excellent "

    I was trying to write program but I am not getting status of student. How to print different status of students ?

    Code:
    #include<stdio.h>
        
    int main(void)
    {
     int marks;
     
     printf(" exam marks : ");
     
     scanf("%d", &marks);
     
     
     if (0 < marks && marks > 40)
     {
      printf ("failed");
     }
     
     else if (40 < marks && marks > 50)
     {
           printf ("average");
     }
        
     else if (50 < marks  && marks > 70)
     {
           printf ("Good");
     }
     return 0;
       
     }
    Last edited by vead; 11-30-2017 at 11:09 PM. Reason: right thread title

  2. #2
    Registered User
    Join Date
    Jun 2017
    Posts
    88
    Code:
    if (0 < marks && marks > 40)
    is the same as:
    Code:
    if (marks > 0 && marks > 40)
    in this case, whenever marks is equal or less than 40, this if condition will evaluate to false. It may as well be simplified to:
    Code:
    if (marks > 40)
    In the same way:
    Code:
    else if (40 < marks && marks > 50)
    can be simplified to:
    Code:
    else if (marks > 50)
    and:
    Code:
    else if (50 < marks && marks > 70)
    can be simplified to:
    Code:
    else if (marks > 70)
    So if this is all put together, the code you have written above would work out exactly the same as this code below:
    Code:
    #include <stdio.h>
     
    int main(void) {
        int marks;
        printf(" exam marks : ");
        scanf("%d", &marks);
     
        if (marks > 40)    {
            printf("failed");
        }
        else if (marks > 50) {
            printf("average");
        }
        else if (marks > 70) {
            printf("Good");
        }
    }
    Now looking at this code, it is easier to see that the bottom two else if statements will never be processed. When the top if statement is false, the conditions in the else if statements are set to be false as well. When the top if statement is true, the else if statements will be skipped.
    Last edited by jack jordan; 12-01-2017 at 12:36 AM.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    99
    Quote Originally Posted by jack jordan View Post

    So if this is all put together, the code you have written above would work out exactly the same as this code below:
    Code:
    #include <stdio.h>
     
    int main(void) {
        int marks;
        printf(" exam marks : ");
        scanf("%d", &marks);
     
        if (marks > 40)    {
            printf("failed");
        }
        else if (marks > 50) {
            printf("average");
        }
        else if (marks > 70) {
            printf("Good");
        }
    }
    Now looking at this code, it is easier to see that the bottom two else if statements will never be processed. When the top if statement is false, the conditions in the else if statements are set to be false as well. When the top if statement is true, the else if statements will be skipped.
    No your program are wrong. I have checked it. when I enter exam marks : 42 this print "failed" and when I enter exam marks : 38 this doesn't print anything

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by vead View Post
    No your program are wrong. I have checked it. when I enter exam marks : 42 this print "failed" and when I enter exam marks : 38 this doesn't print anything
    Please read again what Jack said.

    if/else if/else statements are linked together. If one of them gets executed, the others are skipped. Therefore if "marks" is larger than 50, it's also larger than 40 and only the first if would be run.

    What you probably meant to do and totally got backwards is "marks < 40".
    Devoted my life to programming...

  5. #5
    Registered User
    Join Date
    Jun 2017
    Posts
    157
    What should be the status if marks == 50 - average or good?
    What should be the status if marks == 70 - good or excellent?

  6. #6
    Banned
    Join Date
    Aug 2017
    Posts
    861
    I'd try from a top down process of elimination approach. Start at the (top) highest first then work your way down and when you get to the bottom the only thing left is failing scores, so it'd read something like this
    Code:
    if ( condition )
    Good
    else if ( condition )
    average
    else
    fail
    Last edited by userxbw; 12-01-2017 at 08:56 AM.

  7. #7
    Registered User
    Join Date
    Apr 2012
    Posts
    99
    if user enter any marks between 0 to 40 , then print "fail"
    if user enter any marks between 50 to 70 , then print "average"
    if user enter any marks between 70 to 100, then print "good"

    How to write program for above conditions?

  8. #8
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by vead View Post
    if user enter any marks between 0 to 40 , then print "fail"
    if user enter any marks between 50 to 70 , then print "average"
    if user enter any marks between 70 to 100, then print "good"

    How to write program for above conditions?
    You cannot have 70 be equal to both good and average. that is called what?
    then you have a big gap between 40 and 50 what happens to them peoples scores?
    Code:
    #include <stdio.h>
    int main()
    {
        
        printf("Enter score\n");
        int score = 0;
        scanf(" %d", &score);
    
        // applying process of elimination 
    
        if(score >= 70)
            printf("Good\n");
        else if (score >= 41)
            printf("OK\n");
        else
            printf("fail\n");
                
    return 0;    
    }
    just reverse the logic
    Last edited by userxbw; 12-01-2017 at 12:55 PM.

  9. #9
    Registered User
    Join Date
    Apr 2012
    Posts
    99
    Quote Originally Posted by userxbw View Post
    You cannot have 70 be equal to both good and average. that is called what?
    then you have a big gap between 40 and 50 what happens to them peoples scores?

    just reverse the logic
    Have you tested your code?
    When I compile your program I am getting error

  10. #10
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by vead View Post
    Have you tested your code?
    When I compile your program I am getting error
    why? it works on mine. it is basic C code. what is the error?

    this is the results of both methods, the one I posted, then the one you're working on.
    Code:
    $ ./student_grades
    Enter score
    30
    fail
    Enter score again
    70
    HIGH SCORE
    Last edited by userxbw; 12-01-2017 at 01:56 PM.

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I hope that both of you realize that that code requires a compiler supporting the C99 or higher standard, since it uses features not available in C90.

  12. #12
    Banned
    Join Date
    Aug 2017
    Posts
    861
    he is using scanf and nothing more or less then what I used in my code. Look at his post #1
    eidt the only two things I see different is I did't add 'void' in main, and my declaring of int is between the printf and the scanf
    my if, else if, else .. well that is just what should be used. not if , else if, else if
    Last edited by userxbw; 12-01-2017 at 02:03 PM.

  13. #13
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I'm talking about your code from post #8, not his code. The code from post #1 will compile with a C90 compiler, while you're code won't.

  14. #14
    Banned
    Join Date
    Aug 2017
    Posts
    861
    mine
    Code:
    #include <stdio.h>
    int main()
    {
         
        printf("Enter score\n");
        int score = 0;
        scanf(" %d", &score);
     
        // applying process of elimination 
     
        if(score >= 70)
            printf("Good\n");
        else if (score >= 41)
            printf("OK\n");
        else
            printf("fail\n");
                 
    return 0;    
    }
    his
    Code:
    #include<stdio.h>
         
    int main(void)
    {
     int marks;
      
     printf(" exam marks : ");
      
     scanf("%d", &marks);
      
      
     if (0 < marks && marks > 40)
     {
      printf ("failed");
     }
      
     else if (40 < marks && marks > 50)
     {
           printf ("average");
     }
         
     else if (50 < marks  && marks > 70)
     {
           printf ("Good");
     }
     return 0;
        
     }
    the bold is the only two differences I see. as I do not know the differences between C90 and C99 and if it is an if statement maybe brackets even?

    it is not like he cannot make the needed adjustments, that is why I asked what is the error he is getting.
    Last edited by userxbw; 12-01-2017 at 02:11 PM.

  15. #15
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    The problem is that in C90 all variable declarations must be prior to any executable code, you have variables declared in the middle of a block of code. Also line comments are not allowed in C90, you would need to use normal C comments /* comment */.

    And while int main(void) is the preferred way of defining a main that takes no arguments, omitting the void will usually not be an issue for main().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 'If' Statement inside the Switch statement being ignored.
    By howardbc14 in forum C Programming
    Replies: 4
    Last Post: 04-11-2015, 11:45 AM
  2. Statement inside a statement.
    By JOZZY& Wakko in forum C Programming
    Replies: 15
    Last Post: 11-05-2009, 03:18 PM
  3. if statement
    By spencerhs5 in forum C Programming
    Replies: 3
    Last Post: 07-24-2006, 02:16 PM
  4. If Statement
    By boontune in forum C++ Programming
    Replies: 2
    Last Post: 09-17-2003, 07:56 AM
  5. if statement
    By ZakkWylde969 in forum C++ Programming
    Replies: 22
    Last Post: 07-11-2003, 10:48 PM

Tags for this Thread