You are not logged in.

#1 2007-04-15 17:38:48

Gullible Jones
Member
Registered: 2004-12-29
Posts: 4,863

Something that's been bugging me for a while...

In C, why on Earth would you do this:

if (situation 1)
{
     response 1;
}
else
{
     default response;
}
if (situation 2)
{
     response 2;
}
else
{
     default response;
}
....

When you can do this:

if (situation 1)
{
     response 1;
}
else if (situation 2)
{
     response 2;
}
....
else
{
     default response;
}

There isn't any difference in functionality as far as I can tell... hmm

Offline

#2 2007-04-15 18:04:49

Ramses de Norre
Member
From: Leuven - Belgium
Registered: 2007-03-27
Posts: 1,289

Re: Something that's been bugging me for a while...

There is quite a difference:
In the first piece of code: if situation 1 resolves to true, response 1 is executed, then situation 2 is evaluated and if it resolves to true response 2 is also executed.

In the second piece however, if situation 1 resolves to true, response 1 is executed and then your program skips all else statements and thus there is no chance response 2 will ever be executed.

So if both situation 1 and situation 2 resolve to true, the first code executes both response 1 and response 2, where the second only executes response 1.

Last edited by Ramses de Norre (2007-04-15 21:41:23)

Offline

#3 2007-04-15 20:05:34

Gullible Jones
Member
Registered: 2004-12-29
Posts: 4,863

Re: Something that's been bugging me for a while...

Okay, I get it... thanks. It still seems weird, as in the code in which I saw this "response 1" and "response 2" were (by necessity) mutually incompatible, but at least I know it's not useless.

BTW, is the repetition of the default condition (else {foo}) in the first case actually necessary? It looks like

if (situation 1)
{
     response 1
}
if (situation 2)
{
     response 2
}
else
{
     default response
}

would do the same thing, but I'm not trusting my limited knowledge of programming.

Offline

#4 2007-04-15 21:38:27

Ramses de Norre
Member
From: Leuven - Belgium
Registered: 2007-03-27
Posts: 1,289

Re: Something that's been bugging me for a while...

The difference between following code blocks:

if (situation 1)
{
     response 1;
}
else
{
     default response;
}
if (situation 2)
{
     response 2;
}
else
{
     default response;
}
if (situation 1)
{
     response 1
}
if (situation 2)
{
     response 2
}
else
{
     default response
}

lays in the situation where situation 1 evaluates to false but situation 2 to true, the first block would execute
default response;
response 2;

But the second block would execute
response 2;
because the else is only executed if the second if fails, the first if has no else block specified and thus if the expressions fails nothing happens.

Last edited by Ramses de Norre (2007-04-15 21:39:17)

Offline

#5 2007-04-15 22:32:07

Gullible Jones
Member
Registered: 2004-12-29
Posts: 4,863

Re: Something that's been bugging me for a while...

Alright, thanks for taking the time to answer my newbish questions...

Offline

Board footer

Powered by FluxBB