mercredi 13 janvier 2021

Which best way to write if else code in programming language? [closed]

I want your opinions. Here I have written two way of if...else methods. Could anyone suggest me which below method is best practice code or use less memory ?

Method 1 or Method 2 ?

Generally I checked many developers code they generally ignore to write else condition and prefer Method 1 But my opinion says that Method 2 is more optimised than Method 1, because generally Method 1 stores zero everytime in variable $status.

I WANT YOUR OPINION GUYS

Below code is written in PHP language

If Else Method 1:

for($i = 1;$i<=1000000;$i++){
    $status = 0;
    if($status == 1){
        $status = 1;
     }
     echo $status;echo "<br/>";
}

If Else Method 2:

for($i = 1;$i<=1000000;$i++){
    if($status == 1){
        $status = 1;
    }else{
        $status = 0;
    }
    echo $status;echo "<br/>";
}

Aucun commentaire:

Enregistrer un commentaire