Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to optimize C code with else if conditions

I’m learning some basics of C (I’m actually doing Harvard’s cs50x) and I wrote this code:

#include <cs50.h>
#include <stdio.h>

int main(void) {
    while(0 == 0){
        printf("1. SUM\n2. SUBSTRACTION\n3. MULTIPLICATION\n4. DIVISION\n5. QUIT\n");
    
        int a = get_int("Choose one option: ");
        
    
    
    
        if(a == 1) {
            int x = get_int("x: ");
            int y = get_int("y: ");
            int sum = x + y;
            int sub = x - y;
            int division = x/y;
            int mul = x*y;
            printf("Result = %i\n\n", sum);
        }
    
        else if (a == 2) {
            int x = get_int("x: ");
            int y = get_int("y: ");
            int sum = x + y;
            int sub = x - y;
            int division = x/y;
            int mul = x*y;
            printf("Result = %i\n\n", sub);
        }
    
        else if (a == 3) {
            int x = get_int("x: ");
            int y = get_int("y: ");
            int sum = x + y;
            int sub = x - y;
            int division = x/y;
            int mul = x*y;
            printf("Result = %i\n\n", mul);
        }
    
        else if (a == 4) {
            int x = get_int("x: ");
            int y = get_int("y: ");
            int sum = x + y;
            int sub = x - y;
            int division = x/y;
            int mul = x*y;
            printf("Result = %i\n\n", division);
        }
        
        else if (a == 5) {
            printf("Alright! See you soon!\n");
            break;
        }
    
        else {
            printf("Invalid Input\n");
            break;
        }
    
    }
}

It works exactly as I want it to, but I feel like it could be written in a better way.

I really don’t like all that repetition of int variables and I think that it could be optimized someway but don’t know how.

Can anyone help me?
Thank you in advance 😀

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Calculating all results in every case is kind of useless. The variables containing the results are quite useless aswell because you could make the calculation in the printf function, like this:

printf("Result = %i\n\n", x+y);

lastly you could change the if statements to a switch case which would make the code look a bit cleaner, performance wise the switch case part wouldn’t have any impact, but the other changes will.

And like Weather Vane commented, you can just do while(1).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading