if statement - c# for loops: assignment for my programming class -


i have following question 1 of assignments programming class , wondering if , explain answer me. started loops , book class isn't helpful... question is:

study following block of code carefully. when submit lab, add comment submission answers following questions code:

for (int j = 0; j < 3; j++) {     (int = 0; < 3; i++)     {         if ( block [i] > block [i+1])             swap (ref block [i], ref block [i + 1]);     } } 

(1) when code above executed, how many times statement executed?

  if ( block [i] > block [i+1]) 

counting work in case, , answer question, think knowing why better.

look @ "for" loops. code in each block repeated x number of times.

in case first 1 repeat 3 times, once when j = 0, once when j = 1 , once when j = 2. when j = 3, condition of j > 3 no longer true.

so know code in j loop run 3 times.

so @ next loop... loop 3 times.

if outer loop call "nested" loop 3 times, , nested loop run it's block of code 3 times, know code in middle run 3 times, 3 times, 3 * 3 = 9.


Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -