473,698 Members | 2,588 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
12 2571
desktop <ff*@sss.comwro te:
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.
Would you program "hello world" like this?

int main() {
bool done = false;
while ( !done ) {
cout << "hello world\n";
done = true;
}
}

or would you find the extra, pointless, structure silly?
Sep 20 '07 #11
In article <fc**********@n ews.net.uni-c.dk>, ff*@sss.com says...
Are there any performance issues between:

bool run = true;
while(run) {

if (bob1 &&){

//dothing
break;
}
[ ... ]
run = false;

}
I'd advise against this, but if you're going to do something along this
line, I'd at least use something like:

do {
if (case1)
whatever();
break;

// ...
} while(0);

This obfuscates the intent to a considerably lesser degree -- in fact,
you'll find do...while(0) in a fair number of macros.
(I am aware that a switch could be used. But sometimes the conditions
consists of more parts and therefore a switch might not apply)
When you're faced with a more complex set of conditions, you can
sometimes move the conditions out into a separate function, have it
produce a simple return, and base the switch on that. This doesn't
always make sense, but can at times.
With nested ifs:
if (bob1){

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

//dothing
break;
} else {
if(bob2) {
Presumably you meant bob3.
//dothing
break;
}
}}
You can simplify this a bit, to something like:

if (bob1) {
whatever();
} else if (bob2) {
whatever2();
} else if (bob3) {
whatever3();
}

Depending on the situation, it can also make sense to encode bob1, bob2
and bob3 into functions (or functors), and then do something like:

typedef bool (*pred)(int);

pred predicates[] = {bob1, bob2, bob3};

for (int i=0; i<3; i++)
if (predicates[i](whatever_data) {
whatever(i);
break;
}

--
Later,
Jerry.

The universe is a figment of its own imagination.
Sep 21 '07 #12
On Sep 20, 9:51 pm, desktop <f...@sss.comwr ote:
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 performance of people trying to read your code will suffer
horribly from something like this. The canonical form is:

if ( bob1 ) {
// ...
} else if ( bob2 ) {
// ...
} else if ( bob3 ) {
// ...
}

Obviously, the position of the braces will vary according to
local conventions; some local conventions even allow omitting
them if the if only controls a single statement. But except for
the possible omission of the braces, the token sequence should
be as above.
(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;
}
}}
Conventionally, "else if" is treated as if it were a specific
keyword in itself. The grammar of C++ allows this (and in
languages where it doesn't work, such as Ada, there is always an
elseif/elsif/elif keyword).

This is *the* universal convention. Anything else is
obfuscation.
I prefer the while version event though it might be a bit
unorthodox.
Not just unorthodox. Pure obfuscation, which will confuse
anyone trying to read your code. ("while" means a loop. If you
use "while" when you can't loop, you're lying to the reader.)

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Sep 21 '07 #13

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

Similar topics

4
7300
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
15231
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
2797
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
2415
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 there a way of shorterning this? freeing up the memory say, bcos what is actually displayed is not...
0
1577
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
19410
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
1442
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
25526
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
4483
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 loop, however, only runs once. Even though the outer loop finishes inormally, the inner loop does not...
0
8680
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
8899
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8871
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7738
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6528
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5861
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4371
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.