473,396 Members | 2,057 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

IF...Else Quick Question

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
10 1633
"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
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) {
...
}
}

--
Göran Andersson
_____
http://www.guffa.com
Aug 10 '08 #3
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
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
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
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
Arne Vajhøj <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
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
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
J.B. Moreno wrote:
Arne Vajhøj <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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Elaine Jackson | last post by:
Two quick newbie questions: 1) Does Python have passing-by-reference? 2) In ordinary parlance, "deep" implies "shallow" but not conversely. In the Python "copy" module (if I understand...
4
by: vladimir | last post by:
All, I have seemingly quite known problem, namely I need to watch overloaded in quick watch(by own vector) but I can not. It says overloaded operator is not found. And that is claimed normal...
8
by: Dutchy | last post by:
Dear reader, In an attempt to obtain the path to the quick-launch-folder in order to create a shortcut to my application-updates during installation , I thought to: 1- check if quick launch...
5
by: sparks | last post by:
After trying a combo box to do this. its tied to a lookup table name value yes 1 no 0 they wanted to be able to type in a 1 but display a yes in the field. and store a 1 in the...
1
by: aredo3604gif | last post by:
On Sun, 10 Apr 2005 19:46:32 GMT, aredo3604gif@yahoo.com wrote: >The user can dynamically enter and change the rule connection between >objects. The rule is a "<" and so given two objects: >a <...
18
by: swaroophr | last post by:
Which of switch statement and if-else statement takes less time to execute?
6
by: z_learning_tester | last post by:
Quick question- What happens if you have a private class with a public static method? Can you still say the following? Lets say you are making this call from another class, say class2... int...
2
by: Rudy | last post by:
Hello all! Have a quick question on drop down list. Need to make a year ddl, 1900 to 1987. Is there a quick way to do this. I'm not binding this, just loading it in the items collection. ...
55
by: Steven Nagy | last post by:
Hi all, Sorry I have no time to test this myself.... Can I add the same attribute to a field twice? Eg. Public string myField;
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.