Computer Science, asked by GracySingh, 1 year ago

please answer my question.

Attachments:

Bunti360: 6 times !
GracySingh: can you tell how?

Answers

Answered by siddhartharao77
6
for(int i = 0; i<3;i++)
{
for(int j = 0;j<2; j++)
{

Explanation:

When i = 0; 0 <3  ---------- True.

j = 0; j < 2  ---------- True  --   It generates a random number.

j = 1; j < 2  ----------- True  --  It generates a random number.

j = 2; j < 2  --------- False  -- Comes out of loop.


When i = 1; 1<3; ----------------  True

j = 0; j<2 ------- True  -------- It generates a random number.

j = 1; j<2    -------- True  ------ It generates a random number.

j = 2; 2<2  ------- False............ It comes out of the loop.


When i = 2; 2 < 3  ------------- True

j = 0; j < 2; ------ True  ----------- It generates a random number

j = 1; j<2   --------- True  -------- It generates a random number.

j = 2; 2 < 2  -------- False  ------- It comes out of loop.


When i = 3 ; 3 < 3  -------------- False

The whole statement is wrong.


Now,

Count how many times it is generating a random number.It is generating 6 times.


Therefore the loop will execute 6 times.


Hope this helps!

siddhartharao77: :-)
Bunti360: Wow, Hard worker ! :)
siddhartharao77: Thanks Bunti360
Bunti360: No thanks, You deserve that !
Rajdeep11111: Great!
Answered by Rajdeep11111
1
HEY FRIEND!


When i = 0:  (True because 0 < 3)

j = 0; (The inner loop executes because 0 < 2)
j = 1; (The inner loop executes because 1 < 2)
j = 2; (The inner loop does not execute because 2 < 2 is not true).
Thus, the inner loop executes 2 times.


When i = 1:  (True because 1 < 3)

j = 0; (The inner loop executes because 0 < 2)
j = 1; (The inner loop executes because 1 < 2)
j = 2; (The inner loop does not execute because 2 < 2 is not true).
Thus, the inner loop executes 2 times again.


When i = 2: (True because 2 < 3)

j = 0; (The inner loop executes because 0 < 2)
j = 1; (The inner loop executes because 1 < 2)
j = 2; (The inner loop does not execute because 2 < 2 is not true).
Thus, the inner loop executes 2 times again.


Now, when i = 3,

The loop won't execute because 3 < 3 will false.

The control will come out of the loop.

Now, count the number of times the loop has executed.

=> When i = 0, it executed 2 times.

=> When i = 1, it executed 2 times.

=> When i = 2, it executed 2 times.

Hence, the loop executed a total of 6 times.


Hope my answer is satisfactory...
THANKS!


Similar questions