By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,939 Members | 1,602 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,939 IT Pros & Developers. It's quick & easy.

IF...Else Quick Question

P: n/a
I was just surprised to find that "Else if" is not required in the code bit below.
Apparently "If" and "Else if" are used interchangeably. Please correct me if I'm wrong, but it
appears "Else" is just a stylistic choice.

for (int i = 1; i <= 100; i++)
{
if (i == 3)
continue;
if (i == 5)
break;
Console.WriteLine(i);
}

//Compiles just fine and I can tell no difference using this or "else if" in the second if
statement.
Thanks in advance,

Jack
--
--------------------------------- --- -- -
Posted with NewsLeecher v3.9 Beta 5
Web @ http://www.newsleecher.com/?usenet
------------------- ----- ---- -- -

Aug 10 '08 #1
Share this Question
Share on Google+
10 Replies


P: n/a
"Arcticool" <ar*******@hotmail.comwrote:
Apparently "If" and "Else if" are used interchangeably.
It's the same in your example, because "continue;" will jump back to the top
of the loop anyway, so the second "if" won't be processed.

In general, however, they are not the same. Consider:

int i = 5;
if (i <= 5) Console.WriteLine("less than or equal to 5");
if (i == 5) Console.WriteLine("equals 5");

This will print both lines of text, since each "if" is processed
independently.

If you add "else" to the second line, then the second message will never be
printed, because whenever the second condition is true, the first is true as
well, *and* the second condition is only performed in the "else"-case, when
the first was not.

Eq.
Aug 10 '08 #2

P: n/a
ar*******@hotmail.com wrote:
I was just surprised to find that "Else if" is not required in the code bit below.
Apparently "If" and "Else if" are used interchangeably. Please correct me if I'm wrong, but it
appears "Else" is just a stylistic choice.

for (int i = 1; i <= 100; i++)
{
if (i == 3)
continue;
if (i == 5)
break;
Console.WriteLine(i);
}

//Compiles just fine and I can tell no difference using this or "else if" in the second if
statement.
Thanks in advance,

Jack
It's only in situations where the second if is skipped or it's condition
is exclusive of the first condition that there is no difference.

As you are using continue to skip some of the code, it's equivalent to
this code:

for (int i = 1; i <= 100; i++) {
if (i == 3) {
// do nothing
} else {
if (i == 5) {
break;
}
Console.WriteLine(i);
}
}

Note that there doesn't exist any "else if" construct. It's just an else
containing an if. When you write like this:

if (something) {
...
} else if (something) {
...
}

it's actually:

if (something) {
...
} else {
if (something) {
...
}
}

--
Gran Andersson
_____
http://www.guffa.com
Aug 10 '08 #3

P: n/a
ar*******@hotmail.com wrote:
I was just surprised to find that "Else if" is not required in the code bit below.
Apparently "If" and "Else if" are used interchangeably. Please correct me if I'm wrong, but it
appears "Else" is just a stylistic choice.

for (int i = 1; i <= 100; i++)
{
if (i == 3)
continue;
if (i == 5)
break;
Console.WriteLine(i);
}

//Compiles just fine and I can tell no difference using this or "else if" in the second if
statement.
You got two independent and complete if statements up there above there.
It could be something like this too.
if (i==3)
{
continue;
}
else
{
brake; 'anything else but 3
}

---------
if (i == 3)
{
continue;
}
elseif ( i 0 && i < 2)
{
brake;
}
elseif ( something else )
{
do something;
}
Aug 10 '08 #4

P: n/a
Paul E Collins wrote:
"Arcticool" <ar*******@hotmail.comwrote:
>Apparently "If" and "Else if" are used interchangeably.

It's the same in your example, because "continue;" will jump back to the top
of the loop anyway, so the second "if" won't be processed.

In general, however, they are not the same. Consider:

int i = 5;
if (i <= 5) Console.WriteLine("less than or equal to 5");
if (i == 5) Console.WriteLine("equals 5");

This will print both lines of text, since each "if" is processed
independently.

If you add "else" to the second line, then the second message will never be
printed, because whenever the second condition is true, the first is true as
well, *and* the second condition is only performed in the "else"-case, when
the first was not.
Continue is a special case.

In general and which was also the case in the original posters
code, then else has no functional impact only
performance impact if the conditions are mutually exclusive.

Arne
Aug 10 '08 #5

P: n/a
The IL code generated for the if, if case or the if, else if case is the
same, in your example.

Regards,

Bela Istok
"Arcticool" <ar*******@hotmail.comwrote in message
news:ZN******************************@powerusenet. com...
>>I was just surprised to find that "Else if" is not required in the code
bit below.
Apparently "If" and "Else if" are used interchangeably. Please correct me
if I'm wrong, but it
appears "Else" is just a stylistic choice.

for (int i = 1; i <= 100; i++)
{
if (i == 3)
continue;
if (i == 5)
break;
Console.WriteLine(i);
}

//Compiles just fine and I can tell no difference using this or "else if"
in the second if
statement.
Thanks in advance,

Jack
--
--------------------------------- --- -- -
Posted with NewsLeecher v3.9 Beta 5
Web @ http://www.newsleecher.com/?usenet
------------------- ----- ---- -- -
Aug 10 '08 #6

P: n/a
Thanks to all for the responses.
It seems that there are at least three considerations here:
1. Is the second boolean condition exclusive of the first, if not a nested if may be the best
choice.
2. Else If is not a single construct (aha!)
3. Simply using Else may be all that is needed.

Interesting responses. Much appreciated :)

-Jack
--
--------------------------------- --- -- -
Posted with NewsLeecher v3.9 Beta 5
Web @ http://www.newsleecher.com/?usenet
------------------- ----- ---- -- -

Aug 10 '08 #7

P: n/a
Arne Vajhj <ar**@vajhoej.dkwrote:
Paul E Collins wrote:
"Arcticool" <ar*******@hotmail.comwrote:
Apparently "If" and "Else if" are used interchangeably.
It's the same in your example, because "continue;" will jump back to the
top of the loop anyway, so the second "if" won't be processed.

In general, however, they are not the same. Consider:

int i = 5;
if (i <= 5) Console.WriteLine("less than or equal to 5");
if (i == 5) Console.WriteLine("equals 5");

This will print both lines of text, since each "if" is processed
independently.

If you add "else" to the second line, then the second message will
never be printed, because whenever the second condition is true,
the first is true as well, *and* the second condition is only
performed in the "else"-case, when the first was not.

Continue is a special case.

In general and which was also the case in the original posters code,
then else has no functional impact only performance impact if the
conditions are mutually exclusive.
*IF* the conditions are mutually exclusive, but IME it's much more
common to have if else if chains where the conditions are *not*
mutually exclusive.

Also mutually exclusive conditions are more likely to be done as switch
statements instead.

--
J.B. Moreno
Aug 10 '08 #8

P: n/a
MC
>I was just surprised to find that "Else if" is not required in the code bit
>below.
Apparently "If" and "Else if" are used interchangeably. Please correct me
if I'm wrong, but it
appears "Else" is just a stylistic choice.
No, you just happen to have written a program in which they both do the same
thing. The "continue" statement changes the flow of control so that the
following "if" statement is not executed.

if (X) then A; else if (Y) then B;

is not at all equivalent to:

if (X) then A; if (Y) then B;

The second one will execute both A and B if both X and Y are true. The
first one only looks at Y if X was not true.

Aug 10 '08 #9

P: n/a
In article <X5******************************@powerusenet.com> , wrote:
Thanks to all for the responses.
It seems that there are at least three considerations here:
1. Is the second boolean condition exclusive of the first, if not a nested if
may be the best
choice.
An additional consideration is object references.

Consider....

Object x = null;

if (x == null) {Console.WriteLine("x is null")}
else if (x.Value == 1) {Console.WriteLine("x = 1")}
And side effects
if (x.IsSaved) {whatever}
else if (!x.Persist()) {whatever}

who knows what all could change because of the method call?

--
J.B. Moreno
Aug 10 '08 #10

P: n/a
J.B. Moreno wrote:
Arne Vajhj <ar**@vajhoej.dkwrote:
>Paul E Collins wrote:
>>"Arcticool" <ar*******@hotmail.comwrote:
Apparently "If" and "Else if" are used interchangeably.
It's the same in your example, because "continue;" will jump back to the
top of the loop anyway, so the second "if" won't be processed.

In general, however, they are not the same. Consider:

int i = 5;
if (i <= 5) Console.WriteLine("less than or equal to 5");
if (i == 5) Console.WriteLine("equals 5");

This will print both lines of text, since each "if" is processed
independently.

If you add "else" to the second line, then the second message will
never be printed, because whenever the second condition is true,
the first is true as well, *and* the second condition is only
performed in the "else"-case, when the first was not.
Continue is a special case.

In general and which was also the case in the original posters code,
then else has no functional impact only performance impact if the
conditions are mutually exclusive.

*IF* the conditions are mutually exclusive, but IME it's much more
common to have if else if chains where the conditions are *not*
mutually exclusive.
Possible. I don't have any particular feeling for how common or
uncommon it is.
Also mutually exclusive conditions are more likely to be done as switch
statements instead.
For equality tests.

Arne
Aug 10 '08 #11

This discussion thread is closed

Replies have been disabled for this discussion.