What's new

Noob question po.

Status
Not open for further replies.

Chunchunmaru

Eternal Poster
Joined
Aug 31, 2021
Posts
766
Reaction
288
Points
393
Ano po bang magiging problema ng code kapag puro if ang nilagay at hindi nilagayan ng if else? Pero yung if iba iba ng condition.
 
It depends on the situation.
If lahat IF statements gagamitin mo, kahit masatisfiy na yung first condition, dadaan padin siya sa lahat ng IF statements mo check. While if you are using an else statement, disregarded na lahat kasi may nasatisfy na sa condition. I hope na gets mo yung point ko.. :ROFLMAO:
 
Depende sa conditions ng code mo.

Kung need mo ng default value just in case mag failed yung if else if condition mo,
need mo talaga ng else para mag handle ng final condition (e.g. throw an error or just return a default value)
Pero kung may return kana sa bawal "else if" part or sa pinaka last ng else if, di mo na need ng else.

Pero kung if lang, for example (in C#):

C#:
public bool IsFileExist(string filePath){
    if (!File.Exist(filePath)){
        Console.WriteLine("File does not exist.");
        return False;
    }
    // Dito pwede kana mag return ng True kung checking lang ang purpose.
   // Hindi mo na need ng else kasi nag return na siya sa first condition.
    Console.WriteLine("The file exists!");
    return True;
}

then with else if:
C#:
public void LoadFile(string filePath){

    if (!File.Exist(filePath)){
        Console.WriteLine("File does not exist.");
        return False;
    }
   
    // For example check natin kung end ng file name ay json, txt
   
    if (filePath.EndsWith("json")){
        // Process the json file
    }else if (fielPath.EndsWith("txt")){
        // Process the txt file
    }else{
        Console.WriteLine("Oops. File extension should be json or txt only");
        return;
    }
}

// WITHOUT ELSE
public void LoadFile(string filePath){

    if (!File.Exist(filePath)){
        Console.WriteLine("File does not exist.");
        return False;
    }
   
    // Dito naman since exist yung file, check agad natin kung json or txt ang extension
    if (!filePath.EndsWidth("json") | !filePath.EndsWidth("txt")){
        Console.WriteLine("Oops. File extension should be json or txt only");
        return;
    }
    // Redundant process nato dito kasi checheck na naman kung EndsWidth =D
    // Pero example lang naman for without else

    if (filePath.EndsWith("json")){
        // Process the json file
    }else if (fielPath.EndsWith("txt")){
        // Process the txt file
    }      
    // Dito hindi mo na need ng else
}

Ano bang language ginagawa mo? para makapag bigay ng example
 
Last edited:
Mas maganda magbigay karin ng example :)
Saka don't call yourself a "noob" :) Lahat nagsisismula sa zero
 
Last edited:
Status
Not open for further replies.

Similar threads

Back
Top