please answer my question.
Attachments:
Bunti360:
6 times !
Answers
Answered by
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!
{
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!
Answered by
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!
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
Accountancy,
7 months ago
Geography,
7 months ago
Chemistry,
1 year ago
Biology,
1 year ago
Math,
1 year ago