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.
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;
}
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
}