asked 133k views
4 votes
How many times will the MessageBox.Show method in the following code be processed? For intCount As Integer = 6 To 13 Step 2 MessageBox.Show("Hello") Next intCount a. 3 b. 4 c. 5 d. 8

1 Answer

4 votes

Step-by-step explanation:

let's just imagine what that means to the counter variable and the end criteria of the loop :

starting with 6 the counter is increased at the beginning of every loop by 2, and the loop ends, when the counter is greater than 13.

first loop : counter is 6, 6 <= 13, loop executes.

second loop : counter is 8, 8 <= 13, loop executes.

third loop : counter is 10, 10 <= 13, loop executes.

forth loop : counter is 12, 12 <= 13, loop executes.

fifth loop : counter is 14, 14 > 13, loop stops.

the method is processed 4 times.

in a formal way :

round up (not just round, but really round up) to the next integer

(13-6)/2 = 7/2 = 3.5 ≈ 4

example : instead of step 2 we have step 5 :

(13-6)/5 = 7/5 = 1.4 ≈ 2 (round up in all cases !)

the loop would go for counter = 6 and counter = 11. and then it's over.

answered
User Cedricbahirwe
by
8.4k points