Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Q- 30 Given the following examples that accomplish the same task: Example 1: out

ID: 3763316 • Letter: Q

Question

Q- 30

Given the following examples that accomplish the same task:

Example 1:

outer_loop:
for(i=1; i<4; i++) {
   document.write("<br />"+"outer "+i+": ");
   inner_loop:
   for(j=1; j<4; j++) {
     document.write("inner "+j+" ");
     if(j==x) break outer_loop;
   }
}

Example 2:

for(i=1; i<4; i++) {
   document.write("<br />"+"outer "+i+": ");
   for(j=1; j<=i; j++) {
     document.write("inner "+j+" ");
   }
}

Which one should be used?

1- Example 2

2- Example 1

3- both

4- neither

Explanation / Answer

Example 2 will do better because inner loop is running for less number of times in comparison to Example 1