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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to handle mutiple if's?

Status
Not open for further replies.

Umamageswari

Programmer
Mar 18, 2003
7
IN
Hi,

In my code there comes many condition checks. So i use many if blocks which shows the code very complex. Is there any way to reduce multiple if's.

Can anyone help me. Thanks in advance.
 
You may move some IF's into a new [private] method.

Regards, Dima
 
If you use many 'if ... else if' statements, consider using switch/case statements.

For example:

switch (someInt) {
case 1:
doSomething();
break;
case 2:
doSomethingElse();
break;
case 3:
freakOut();
break;
}

... instead of:

if ( someInt == 1 ) {
doSomething();
} else if ( someInt == 2 ) {
doSomethingElse();
} else if ( someInt == 3 ) {
freakOut();
}

Hope that helps.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top