samedi 24 juillet 2021

I, from my researches concluded that else statement is not necessary but there must be an exceptio

I, from my researches concluded that else statement is not necessary but there must be an exception or I skip something in this code. I know it might be a very straightforward question but since I am pretty new in C I am so confused. Here is the queastion:

int main()
{
    int m, x=1, y=1;
    if(x<5){
        if(y==0)
            m=4;
        m=6;
    }
    m=8;

    printf("m = %d", m);
}

I am expecting to get m=6 as an output since x<5 is true, but I get m=8 (which is supposed to be else statetment of (x<5).

While in this code:

int main()
{
    int m, x=1, y=1;
    if(x<5){
        if(y==0)
            m=4;
        m=6;
    }
    else
        m=8;
    
    printf("m = %d", m);
}

when I add the else statement I get the correct output.

So how is else statement not necessary? Thanks indeed.

Aucun commentaire:

Enregistrer un commentaire