why use nested if-else statement if you can write it in one line?

when i’m showing my assignment code,

maxout = (strcmp(JenisAkaun,"TETAP") == 0) ? 500 : (strcmp(JenisAkaun,"BIASA") == 0) ? 10 : 0;

they look shocked like they never do programming before.. more surprising, even most of my friends from fsksm didn’t know this kind of coding exist.. lol!!! if you also one of them, then you better read back your programming book about conditional operator. so why we should know this thing?

have you ever encounter any programming condition that requires nested if-else statement like this

if ( $sa == a ) {if ( $kit ) {
do_somthing( $kit );
elsif ( $oo ) {
do_something_else( $oo );
}
else {
do_nothing();
}
}
elsif ( $tak == b ) {
if ( $laa ) {
other_func();
}
}
else {
go_die();
}

complex and look ugly isn’t? well you can write it in one line

$sa == a ? $kit ? do_somthing( $kit ) : $oo ? do_something_else( $oo ) : do_nothing() : $tak == b ? $laa ? other_func() : do_nothing() : go_die();

it’s actually look like this if you arrange it properly,

$sa == a ?$kit ? do_somthing( $kit )
: $oo ? do_something_else( $oo )
: do_nothing()
: $tak == b ?
$laa ? other_func() : do_nothing()
: go_die();

sound complex isn’t it?? yes it’s sound very complex for a too many nested if with different variables involved. for this kind of problem, it’s recommended that you better use switch. however, it’s handy when you have a condition where you need to manipulate the same variable over different conditions.

if ($status == "worker") {if ($job == "office") {
discount = 50;
elseif ($job == "site"){
$discount = 40;
}
else {
$discount = 10;
}
}
else if (status == "member") {
if ($point > 100) {
$discount = 20;
}
elseif ($point > 50 && $point < 100) {
$discount = 10;
}
else {
$discount = 5;
}
}
else {
$discount = 0;
}

it must be annoying to write all this esspecially when you are in lazy mode, well here’s a shortest way to write it

$discount = ($status == "worker") ? ($job == "office") ? 50 : ($job == "site") ? 40 : 10 : ($status == "member") ? ($point > 100) ? 20 : ($point > 50 && $point < 100) ? 10 : 5 : 0;

still look complex? well it’s complex for 1st timer, but when you master it, you i’ll find how simple and interesting it’s.
after this you can waste your time writing $price = ($no == 1) ? $price/2 : $price/4; instead of writing

if ($no == 1) { $price = $price/2; }
else { $price = $price/4; }

not recommended to be used in examination if it require too many nested if.. never show ur l33tness in exam because we tend make mistake! unless you are very good, then go ahead.. ;)

thats it for some explaination about conditional operator.

additional note : this mostly being used when writing macros for C programming language. it’s also being use in polymorphsim technique

3 Comments

  1. Posted March 17, 2008 at 7:54 pm | Permalink

    Kalau dalam exam, jangan ngada2 nak buat macam tu unless korang betul2 pasti yang korang punye coding betul. Kalau tak, tak pasal2 hilang markah sebab salah conditions dia.

  2. novatech
    Posted March 20, 2008 at 1:08 am | Permalink

    yaya..betul tu, bak kata pepatah mat saleh “if you in doubt, just let it go”
    kalau ragu ragu tak payah ngada-ngada nak buat

  3. Posted April 17, 2008 at 6:58 pm | Permalink

    cam programming je -_-
    ni C ke