Thread: else if to switch possible ?

  1. #1
    Registered User
    Join Date
    May 2019
    Posts
    38

    Lightbulb else if to switch possible ?

    hello friends,

    I want to replace else if with switch statement here

    Code:
    #include <stdio.h>#include <stdlib.h>
    
    
    int main()
    {
    
    
        float grade1;
        float grade2;
        float grade3;
    
    
      printf("Enter your 3  test grades \n");
      scanf(" %f" ,&grade1);
      scanf(" %f" ,&grade2);
      scanf(" %f" ,&grade3);
    
    
      float avg =(grade1 +grade2 + grade3 ) / 3 ;
    
    
       printf("Average : %.2f \n" ,avg);
    
    
     if (avg>=90){
    
    
         printf("You are Excellent in studies\n");
       } else if (avg>=80){
    
    
         printf("You are great in studies\n");
       } else if (avg>=70){
    
    
         printf("You are good in studies\n");
       }else if (avg>=60){
    
    
         printf("You are average in studies\n");
       }else{
    
    
         printf("You need to improve in studies\n");
       }
    
    
    
    
        return 0;
    }
    hows that possible ?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,232
    You cannot use a switch in C for this as the switch's case values must be integers. Even if you were dealing with an integer range rather than a float range, the if-else chain would still be better than exhaustively listing all the integers in each range.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,525
    In this particular case, where you can reduce the average to a smaller range...
    Code:
    int range = avg / 10;
    switch ( range ) {
      case 9:
        printf("You are Excellent in studies\n");
        break;
      case 8:
        printf("You are great in studies\n");
        break;
      case 7:
        printf("You are good in studies\n");
        break;
      case 6:
        printf("You are average in studies\n");
        break;
      default:
        printf("You need to improve in studies\n");
        break;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    550
    In this case you can avoid if..else of switch entirely (but it is a little bit confusing to someone else to read the code):
    Code:
    // test.c
    #include <stdio.h>
    
    int main ( void )
    {
      static const char * const msgs[] = { "improve", "average", "good", "great", "excellent" };
      double a;
      int g, offset;
    
      // get the avarage from keyboard...
      scanf( "%lf", &a );
    
      g =  ( a >= 90.0 );
      g += ( a >= 80.0 );
      g += ( a >= 70.0 );
      g += ( a >= 60.0 );
    
      printf ( "%g: %s\n", a, msgs[g] );
    }
    Compiling and running:
    Code:
    $ cc -o test test.c
    $ ./test <<< 10
    10: improve
    $ ./test <<< 62
    62: average
    $ ./test <<< 73
    73: good
    $ ./test <<< 81
    81: great
    $ <<< 99
    99: excellent

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,859
    Salem beat me to it!... By about 7 hours! Haha

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with switch(x)
    By oreo in forum C Programming
    Replies: 3
    Last Post: 09-15-2010, 08:22 AM
  2. A switch that doesn't switch
    By Death_Wraith in forum C++ Programming
    Replies: 5
    Last Post: 11-06-2004, 12:18 PM
  3. please help about switch
    By frankieso in forum C Programming
    Replies: 10
    Last Post: 10-20-2004, 03:00 PM
  4. switch
    By tommy_cgl in forum C++ Programming
    Replies: 2
    Last Post: 03-29-2003, 01:25 PM
  5. Need help with switch
    By 3kgt in forum C Programming
    Replies: 2
    Last Post: 02-26-2003, 12:43 PM

Tags for this Thread