-6
    if (x>0 && x<6) 
    {
        break;
    }
    else if(x>6)
    {
        break;
    }

versus

    if (x>0) 
    {
        if (x<6) 
        {
            break;
        }
    }
    else
    {
        if (x>6) 
        {
            break;
        }
    }
        

Code 1 doesn't work but code 2 does. Why? I'm a super noobie at programming so please any help would be nice. Programming language is C.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
jason
  • 3
  • 4
    First of all, it would help you in understanding the code if you applied a consistent brace style and indentation. – mkrieger1 May 03 '22 at 12:46
  • 3
    You are going to have to be more specific about what you mean by it 'works' and 'doesn't work'. because as far as I can see, the two are functionally identical. What are you expecting to happen and what is actually happening? – Ben Wainwright May 03 '22 at 12:46
  • I have a function has a calculation e.g. input two values and output = X. This statement above checks whether the value is within 6. For some reason the bottom function works wwhile the top one doesnt. But it doesnt make sense they are logically the same no? – jason May 03 '22 at 12:50
  • Question is somewhat thought provoking. (evidenced by more than one [now deleted] higher rep comments suggesting wrongly that two examples are equal.) And the 1 close vote claims question is lacking details or clarity. It seems sufficiently detailed and clear to me, (Although stepping through with a [debugger](https://stackoverflow.com/questions/4671900/how-do-i-use-the-mingw-gdb-debugger-to-debug-a-c-program-in-windows) would probably negate the need for the question in the first place.) Lastly, how could a question deemed worth so many down-votes evoke such a highly praised answer? – ryyker May 03 '22 at 13:45

2 Answers2

5

The else statement in the second code snippet never gets the control for any positive value of x because the condition of the first if statement at once evaluates to logical true.

if (x>0) {
    if (x<6) 
    {
        break;
    }
}
else {
    if (x>6) 
    {
        break;
    }
}

While in the first code snippet the else statement will get the control for any value of x equal to or greater than 6.

That is if x for example is equal to 7 then the condition in the first if statement

if (x>0 && x<6) 
{
    break;
}

evaluates to logical false. So the else statement gets the control and the condition of the if sub-statemen also evaluates to logical true.

else if(x>6)
    {
        break;
    }
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

If x is superior to 0, it goes inside if (x>0) , so it's absolutely impossible for it to reach the if (x>6) inside the else.

I don't know what you mean by "it works" "it doesn't work", but, the second example is bad in my opinion, it's unclear, and not each case are treated. While the first one allows for more clarity and case treated.

Your second example basically only treat value between 0 and 6. While the first treat any case between 0 and + int max value or something

ryyker
  • 22,849
  • 3
  • 43
  • 87
Elley
  • 720
  • 8
  • 16
  • 1
    Suggest editing post to clarify which of the two examples you are referring to in your first sentence. – ryyker May 03 '22 at 13:01