samedi 3 juillet 2021

else part of the if else statement is not working

I was just trying to write some nested if-else statements and I don't know why else part of the statement is not executing.

void main(){
    int a = 1;
    int b = 2;
    int c = 3;

    if(a>b){
        if(a>c){
            printf("a is greatest");
        }
    }
    else if(b>a){
        if(b>c){
            printf("b is greatest");
        }
    }
// This else statement.
    else{
        printf("c is greatest");
    }
}

1. but if I write these statements using &&, else part of the statement starts working fine.

if(a>b && a>c){
    printf("a is greatest");
}
else if(b>a && b>c){
    printf("b is greatest");
}
else{
    printf("c is greatest");
}

2. If I remove Braces from else-if part, then the program works fine.

if(a>b){
    if(a>c){
        printf("a is greatest");
    }
}
else if(b>a)
    if(b>c){
        printf("b is greatest");
    }
else{
    printf("c is greatest");
}

but if I remove every Braces, then else part don't get executed.

if(a>b)
    if(a>c)
        printf("a is greatest");
        
    
else if(b>a)
    if(b>c)
        printf("b is greatest");
        
else
    printf("c is greatest");

Aucun commentaire:

Enregistrer un commentaire