473,569 Members | 2,759 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

While vs nested if?

Are there any performance issues between:

bool run = true;
while(run) {

if (bob1 &&){

//dothing
break;
}
if(bob2) {

//dothing
break;
}

if(bob2) {

//dothing
break;
}

run = false;

}

(I am aware that a switch could be used. But sometimes the conditions
consists of more parts and therefore a switch might not apply)
With nested ifs:
if (bob1){

//dothing
break;
} else {
if(bob2) {

//dothing
break;
} else {
if(bob2) {

//dothing
break;
}
}}
I prefer the while version event though it might be a bit unorthodox.
Sep 20 '07 #1
12 2558
desktop wrote:
Are there any performance issues between:

bool run = true;
while(run) {

if (bob1 &&){

//dothing
break;
}
if(bob2) {

//dothing
break;
}

if(bob2) {
This is unreachable. You already have 'bob2' above. I assume
you meant 'bob3'.
>
//dothing
break;
}

run = false;

}

(I am aware that a switch could be used. But sometimes the conditions
consists of more parts and therefore a switch might not apply)
With nested ifs:
if (bob1){

//dothing
break;
'break' outside of any loop is a syntax error.
} else {
if(bob2) {

//dothing
break;
} else {
if(bob2) {

//dothing
break;
}
}}
I prefer the while version event though it might be a bit unorthodox.
I don't believe that "issues" can be "between". I think "issues"
can be "with" or sometimes "in". In the code you posted, however,
I see a simple issue: the former does not convey the intent. If
you intended to have a branching processing, do it. Why wrap it
into an artificial logic structure? And there is no need to nest
anything, just chain them (which is basically the same as nesting
only without curly braces):

if (bob1) {
do one
}
else if (bob2) {
do two
}
else fi (bob3) {
do three
}

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 20 '07 #2
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

desktop wrote:
I prefer the while version event though it might be a bit unorthodox.
IMHO I have always preferred the nested approach for simple 2 ( max 3
level ) checks, if it's something more complicated I use a Bit Toggle
approach with `case` or `if`, I don't quite understand why you would need a
`while` like that, do you have a more clear example?

- --
Guillermo Antonio Amaral Bastidas (gamaral)
# Free & Open-source Software Advocate
# KDE Developer: gamaral
@ http://blog.guillermoamaral.com/
$ irc: gamaral@freenod e pgp: 0x5D297B74

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)

iD8DBQFG8tS3xxx W3F0pe3QRArKiAJ 4/2vhLzT2l0gEVcrW wRxKpfgcK6ACfaz Ty
U0g+mOPw1LP+Gw9 XKKj30nQ=
=3Rt5
-----END PGP SIGNATURE-----
Sep 20 '07 #3
Victor Bazarov wrote:
desktop wrote:
>Are there any performance issues between:

bool run = true;
while(run) {

if (bob1 &&){

//dothing
break;
}
if(bob2) {

//dothing
break;
}

if(bob2) {

This is unreachable. You already have 'bob2' above. I assume
you meant 'bob3'.
>//dothing
break;
}

run = false;

}

(I am aware that a switch could be used. But sometimes the conditions
consists of more parts and therefore a switch might not apply)
With nested ifs:
if (bob1){

//dothing
break;

'break' outside of any loop is a syntax error.
>} else {
if(bob2) {

//dothing
break;
} else {
if(bob2) {

//dothing
break;
}
}}
I prefer the while version event though it might be a bit unorthodox.

I don't believe that "issues" can be "between". I think "issues"
can be "with" or sometimes "in". In the code you posted, however,
I see a simple issue: the former does not convey the intent. If
you intended to have a branching processing, do it. Why wrap it
into an artificial logic structure? And there is no need to nest
anything, just chain them (which is basically the same as nesting
only without curly braces):

if (bob1) {
do one
}
else if (bob2) {
do two
}
else fi (bob3) {
do three
}

V
From a cosmetic point of view I still prefer the while/break version
event though your chain version is an improvement.

But maybe its better to stick with convention instead of inventing new
styles.
Sep 20 '07 #4
On Sep 20, 3:17 pm, desktop <f...@sss.comwr ote:
From a cosmetic point of view I still prefer the while/break version
event though your chain version is an improvement.

But maybe its better to stick with convention instead of inventing new
styles.
At least if (and only if) you're going to insist on using your loop
structure, use something like:

do {
if (...) {
...
break;
}

if (...) {
...
break;
}
} while (0);

This way, the compiler will know without a doubt that the loop only
executes once and should optimize it out (if it's at all intelligent).

Sep 20 '07 #5
desktop wrote:
Victor Bazarov wrote:
>desktop wrote:
>>Are there any performance issues between:

bool run = true;
while(run) {

if (bob1 &&){

//dothing
break;
}
if(bob2) {

//dothing
break;
}

if(bob2) {

This is unreachable. You already have 'bob2' above. I assume
you meant 'bob3'.
>>//dothing
break;
}

run = false;

}

(I am aware that a switch could be used. But sometimes the
conditions consists of more parts and therefore a switch might not
apply)

From a cosmetic point of view I still prefer the while/break version
event though your chain version is an improvement.
What cosmetic point of view? *How* is

bool run = true;
while (run) {
// blah
break;
// blahblah
break;
run = false;
}

better than

// blah
// blahblah

_cosmetically_?
But maybe its better to stick with convention instead of inventing new
styles.
Conventions differ...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 20 '07 #6
desktop wrote:
bool run = true;
while(run) {

if (bob1 &&){

//dothing
break;
}
if(bob2) {

//dothing
break;
}

if(bob2) {

//dothing
break;
}

run = false;

}
I think you have done it way too complicated with that while. This
would be a bit simpler:

do
{
// the ifs
} while(false);

Probably easier for the compiler to optimize too.
if (bob1){

//dothing
break;
} else {
if(bob2) {

//dothing
break;
} else {
if(bob2) {

//dothing
break;
}
}}
You could save yourself typing many brackets by doing it like this:

if(cond1)
{
...
}
else if(cond2)
{
...
}
else if(cond3)
{
...
}

Becomes cleaner too.
Sep 20 '07 #7
desktop <ff*@sss.comwri tes:
Are there any performance issues between:

bool run = true;
while(run) {

if (bob1 &&){

//dothing
break;
}

if(bob2) {

//dothing
break;
}

if(bob2) {

//dothing
break;
}

run = false;

}
As far as I understood correctly I'd use:

#v+
do {
// some code
if (foo) break;
// some more code
if (bar) break;
// even more code more code
if (baz) break;
// and some final part;
} while (0);
#v-
With nested ifs:

if (bob1){
//dothing
break;
} else {

if(bob2) {

//dothing
break;
} else {
if(bob2) {

//dothing
break;
}
}}
On a second though I might misunderstood you and thus I'd use:

#v+
if {foo) {
// do something
} else if (bar) {
// do something else
} else if (baz) {
// or maybe do something completely different
}
#v-

--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl >---<jid:mina86*chr ome.pl>--ooO--(_)--Ooo--
Sep 20 '07 #8

desktop wrote:
Are there any performance issues between:
[snip]

I seriously doubt it. The else's aren't evaluated
if any of the ifs prior evaluate to true.
I would rather think the while version is marginally
slower due to the additional evaluation for no reason
whatsoever.

Also, one often finds the performance bottlenecks
is not where you think they are. 80% of the code
execute 20% of the time, and pose no serious
performance bottleneck (I think, or might of
read somewhere), therefore don't sweat it.

Regards,

Werner

Sep 20 '07 #9
On 2007-09-20 21:51, desktop wrote:
Are there any performance issues between:

bool run = true;
while(run) {

if (bob1 &&){

//dothing
break;
}
if(bob2) {

//dothing
break;
}

if(bob2) {

//dothing
break;
}

run = false;

}
The way I see it this is just another way to write

if (bob1)
goto end;

if (bob2)
goto end;

if (bob3)
goto end;

end:

Of course, you do not actually use goto, but you get the same effect.
The problem is that goto is not disliked because of its syntax, but
because of its behaviour. People generally expect loops to do several
iterations, and only in special cases will they do only one, so a loop
that always do only one loop is a bit weird and might confuse readers.

As for performance, I am not sure (and it depends on the code generated
by the compiler) but I think the branch prediction hardware generally
expect the loops to do more than one iteration.

--
Erik Wikström
Sep 20 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
7298
by: Paul Charlton-Thomson | last post by:
Hi, I am trying to execute 2 queries and then output the results in 2 loops but maybe I'm using the wrong approach. Can anyone help me here ... $query_1 = "SELECT field_1 FROM table_1"; $result_1 = mysql_query($query_1) or die(mysql_error());
14
15205
by: jsaul | last post by:
Hi there, wouldn't it be useful to have a 'while' conditional in addition to 'if' in list comprehensions? foo = for i in bar: if len(i) == 0: break foo.append(i)
36
2776
by: invni | last post by:
I have a nested while. How do I go from the inner while to the beginning of the outer while? Can this be done without using goto? while_1() { some codes here while_2() { if true go to the beginning of while_1 }
3
2400
by: monomaniac21 | last post by:
hi all i have a script that retrieves rows from a single table, rows are related to eachother and are retrieved by doing a series of while loops within while loops. bcos each row contains a text field they are fairly large. the net result is that when 60 or so results are reitreved the page size is 400kb! which takes too long to load. is...
0
1567
by: all the gear no idear | last post by:
Hello im trying to make a nested while loop I would like to be able to run the following function 26 time incrementing the value of snb each time else if(answer=='b') { printf("\ntype the information you whish to be decryption using the Caesar cifer\n"); scanf("%c",&c); while(c!='`')
14
19400
by: Jan Schmidt | last post by:
Hi, in a nested do-while-loop structure I would like to "continue" the outer loop. With goto this should be no problem in while-loops. However, for do-while I cannot get it to work (without a strange workaround construct): -- do { // ...
2
1438
by: desktop | last post by:
I have this nested while: int numsx = { 11, 22, 33, 44, 55, 77, 88, 99}; int* startx = numsx; int endx = sizeof(numsx) / sizeof(numsx); int* endxp = numsx + endx;
11
25504
by: Rene | last post by:
Quick question, what is the point for forcing the semicolon at the end of the while statement? See example below: x = 0; do { x = x + 1; }while (x < 3); What's the point of having the semicolon after the (x < 3)? Why can't the
3
4476
by: numlock00 | last post by:
I have a nested 'while' loop that won't repeat, no matter how many times the outer loop repeats. The outer loop reads through an array of elements; the inner loop Ithe 'while' loop) is supposed to apply each of these elements while reading an input file. The outer loop is working fine. It will run through every element of the array. The inner...
0
7697
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7612
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8120
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7672
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7968
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5512
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.