Decrease the if statement itself

This is my if statement at the moment:

if (excel_getValue("A" + i) == "" && excel_getValue("A" + (i + 1)) == "" && excel_getValue("A" + (i + 2)) == "" && excel_getValue("A" + (i + 3)) == "" && excel_getValue("A" + (i + 4)) == "" && excel_getValue("A" + (i + 5)) == "" && excel_getValue("A" + (i + 6)) == "" && excel_getValue("A" + (i + 7)) == "" && excel_getValue("A" + (i + 8)) == "") 

Is there a way to reduce this statement? Should I build this if statement in a for loop ??

BTW, this if-statement, if it is already in forloop, and I use forloop

+5
source share
4 answers

You can use All :

 if(Enumerable.Range(0, 9).All(c => excel_getValue("A" + (i + c)) == "")) { } 
+19
source

There is a simple way to “reduce” (at least “decorate”) the for outline ... this will not be more indicative, so you choose:

 bool do = true; for(var j = 0; j < 9; j++) { if (excel_getValue("A" + (i+j)) != "") { do = false; break; } } if(do) { /* whatever */ } 

Or using Linq:

 if(Enumerable.Range(0, 9).All(x => excel_getValue("A" + (i + x)) == "")) { /* whatever */ } 

It can probably be easily shortened, but I think the point is that it should be “prettier” and not “better” or “difficult”. Nothing would change your initial level of performance :-)

+2
source

Yes, a for loop is a good way to simplify your code in this case. Sort of:

  bool result = true; for(int j = 0; j < max; j++) { result = result && excel_getValue("A" + (i + j)) == ""; } 
0
source

You can try something like this:

 if (Enumerable.Range(0, 8).All(p => excel_getValue("A" + (i + p)) == "")) 
0
source

All Articles