Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Eliminate if-else statements - less bulky code 2

Status
Not open for further replies.

Sedai

Programmer
Mar 4, 2001
158
US
Hi!

is there any way to eliminate some of these if else statements [without using goto and labels] in the following piece of code

Code:
//.....other code

if(s.getType()==Node::INT){
   i = s.pop();
   if(!s.empty()){
     cerr << &quot;ERROR: Invalid prefix format [too many operators?] in line &quot;
	  << count
	  << &quot;.\nIgnoring line.\n&quot;;
	  s.flush();
   }
   else
     cout << &quot;Line &quot; << count << &quot; Result: &quot; << i << endl;
		}
else{
     cerr << &quot;ERROR: Invalid prefix format [too many operators?] in line &quot;
	  << count
	  << &quot;.\nIgnoring line.\n&quot;;
     s.flush();
}

there are several ways i can re-arrange these, checking with negation (!), but i always end up with two pairs of if-else. it seems like i could eliminate one of them since in two cases i have the same output.... is it possible?

thanks.

p.s. s is a Stack object. Stack class implemented with Nodes, but of course that has no bearing with the case.
Avendeval Sedai
[sub]yavoor@yahoo.com[/sub]

 
Not really. What you're doing isn't excessive. Far better then doing GOTO's... The only time it's okay to use GOTO's is if you just can't find another way. Even then, it's better to walk away and come back later looking for a different approach then a GOTO.
 
It looks like you may be wondering how to rewrite your code without having to double up on the block which outputs the error message. I do this &quot;doubling-up&quot; constantly and time and time again, I find myself re-writing everything without the ifs and elses. Try rewriting your code this way:

int problem = FALSE;

if( s.getType() != NODE::INT ) problem=TRUE;
if( !problem ){
i = s.pop();
if( s.empty() ) problem=TRUE;
}
if( problem ) {
cerr << &quot;ERROR Invalid prefix format [too many operators?] in line &quot;
<< count
<< &quot;.\nIgnoring Line.\n&quot;;
s.flush();
}
else cout << &quot;Line &quot;
<< count
<< &quot; Result: &quot;
<< i
<< endl;

You can always re-write code so that there are no 'elses', no 'gotos', and so that no block of code is repeated. But you will pay for your cleverness by having all these extra flag variables making your code indecipherable.
---
Paris, 1849



 
A switch statement is a good way to get away from the if...else statement.
 
A safer method than GOTO, but with a simiar feel is the try/catch blocks. It would look something like this.

try
{
if (s.getType() != NODE::INT )
throw 1;

i = s.pop();
if (s.empty())
throw 2;

// Output good stuff here

}
catch (int nError)
{
// Output error message here
}


Basically the way it works is that any throw performed inside a try block will first be examined by the following catch block to see if what was &quot;thrown&quot; can be handled. In the above example a &quot;throw 2.0&quot; would not be caught. It basically skips over the code from the throw to the catch. It will also unwind the stack for you, if you want to get fancy. You can throw from a subroutine, and be caught in the calling routine. This kind of exception handling is a usefull tool, but be warned that if you throw something, and don't catch it, it will give you an assertion error eventually, as it keeps searching back for something to catch the exception.

Dragoon
 
cool!

thanks Dragoon!
a very useful technique in other cases too Avendeval Sedai
[sub]yavoor@yahoo.com[/sub]

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top