java - Executing code N times and other code N+1 times -


the question while-loops in need code executed n times , other code n+1 times. not concatening strings, use bad-coded yet short example.

let me explain question providing example.

say want concatenate n+1 strings, glueing them "\n", example. have n+1 lines of text then, need add n times "\n".

is there boilerplate solution type of loop in have execute code n times , other code n+1 times? i'm not asking solution concatenate strings! (bad) example. i'm looking general solution.

the problem have code duplication, code example i'll (bad pseudo code, know have use stringbuilder etc.):

string[] lines = <some array of dimension n+1>; string total = lines[0]; (int = 1; < n + 1; i++){     total += "\n" + lines[i]; } 

the problem becomes worse if code has executed n+1 times, becomes larger, of course. like

codea(); // adding line of text (int = 1; < n + 1; i++){     codeb(); // adding "\n"     codea(); } 

to remove duplication, can different checking inside loop, too, find quite stupid know beforehand check pre-determined, false first iteration:

for (int = 0; < n + 1; i++){     if (i > 0){         codeb(); // adding "\n"     }     codea(); } 

is there solution this, sort of while-loop initializes once codea() en keeps looping on codeb() , codea()?

people must have run before, guess. wondering if there beautiful solutions this.

to dissapointment, believe there no such construct satisfies conditions have stated them , attempt explain why (though can't prove in strictly mathematical way).

the requirements of problem are:

  1. we have 2 parts of code: codea() , codeb()
  2. the 2 parts executed different number of times, n , n+1
  3. we want avoid adding condition inside loop
  4. we want execute each part many times strictly necessary

2) direct consequence of 1). if didn't have 2 parts of code not need different number of executions. have single loop body.

4) again consequence of 1). there no redundant execution if have single loop body. can control execution through loop's condition

so restrictions 1) , 3).

now inside loop need answer 2 questions on each iteration: a) execute codea()? , b) execute codeb()? not have enough information decide since have single condition (the condition of loop) , condition used decide if both of code parts executed or not.

so need break 1) and/or 3) either add condition inside loop or delegate decision other code (thus not having 2 parts anymore).

apparently example of delegation (i using string concatenation example):

string [] lines = ... (int = 0; < n; i++){    // delegate utility class linebuilder (perhaps extension of stringbuilder) concatenate lines    // class still need check condition e.g. first line skip "\n"    // since have delegated decisions not have 2 code parts inside loop    linebuilder.addline( lines[i] ); } 

now more interesting case of delegation if delegate decision data itself (this might worth keeping in mind). example:

list<line> lines = arrays.aslist(              new firstline("every"),    // note class different              new line("word"),               new line("on"),               new line("separate"),               new line("line") );  stringbuffer sb = new stringbuffer();  (line l : lines) {     // again decision delegated. data knows how print     // line return: "\n" + s     // firstline return: s     sb.append( l.getprintversion() ); } 

of course of above not mean couldn't implement class tries solve problem. believe though beyond scope of original question not mention overkill simple loops


Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -