In: Computer Science
What is the output of the following codes, consider each case independently. I have an example of Case 1 to show you how to complete each case at the end of this Module.
Case 1: What will be the value of x after the following loop is executed:
x = 0
For i = 1 To 2
For j = 1 To 3
x = x * i * j
Next j
Next i
Case 2: What will be the value of x after the following loop is executed:
x = 1
For i = 1 To 2
For j = 1 To 3
x = x * i * j
Next j
Next i
Case 3: What will be the value of x after the following loop is executed:
x = 0
For i = 1 To 2
For j = 1 To 3
x = x * i + j
Next j
Next i
Case 4: What will be the value of x after the following loop is executed:
x = 0
For i = 1 To 2
For j = 1 To 3
x = x + i * j
Next j
Next i
Please see below for an example of completed Case 1. Follow the same method to complete the other three cases. Let me know your questions.
x |
i |
j |
NOTES |
0 |
x is initialized to 0 |
||
x = 0*1*1, the new value for x is 0 |
1 |
1 |
First we start with the outer loop (for I ) and continue with inner loop (for J ). |
x = 0*1*2, the new value for x is 0 |
1 |
2 |
Still in the inner loop when J becomes 2, note I is 1 |
x = 0*1*3, the new value for x is 0 |
1 |
3 |
Still in the inner loop when J becomes 3, note I is 1 |
x = 0*2*1, the new value for x is 0 |
2 |
1 |
Here program continues with the outer loop, where I is 2 and moves to inner loop where J is 1 |
x = 0*2*2, the new value for x is 0 |
2 |
2 |
Program continues with the inner loop when J becomes 2, note I is 2 |
x = 0*2*3, the new value for x is 0 |
2 |
3 |
Program continues with the inner loop when J becomes 3, note I is 2 |
X = 0 |
Is the final value for x when both loops are completed |
case 1 is already solved so i start with case 2
Case 2:
x |
i |
j |
NOTES |
1 |
x is initialized to 1 |
||
x = 1*1*1, the new value for x is 1 |
1 |
1 |
First we start with the outer loop (for I ) and continue with inner loop (for J ). |
x = 1*1*2, the new value for x is 2 |
1 |
2 |
Still in the inner loop when J becomes 2, note I is 1 |
x = 2*1*3, the new value for x is 6 |
1 |
3 |
Still in the inner loop when J becomes 3, note I is 1 |
x = 6*2*1, the new value for x is 12 |
2 |
1 |
Here program continues with the outer loop, where I is 2 and moves to inner loop where J is 1 |
x = 12*2*2, the new value for x is 48 |
2 |
2 |
Program continues with the inner loop when J becomes 2, note I is 2 |
x = 48*2*3, the new value for x is 288 |
2 |
3 |
Program continues with the inner loop when J becomes 3, note I is 2 |
X = 288 |
Is the final value for x when both loops are completed |
Case 3:
x |
i |
j |
NOTES |
0 |
x is initialized to 0 |
||
x = 0*1+1, the new value for x is 1 |
1 |
1 |
First we start with the outer loop (for I ) and continue with inner loop (for J ). |
x = 1*1+2, the new value for x is 3 |
1 |
2 |
Still in the inner loop when J becomes 2, note I is 1 |
x = 3*1+3, the new value for x is 6 |
1 |
3 |
Still in the inner loop when J becomes 3, note I is 1 |
x = 6*2+1, the new value for x is 13 |
2 |
1 |
Here program continues with the outer loop, where I is 2 and moves to inner loop where J is 1 |
x = 13*2+2, the new value for x is 28 |
2 |
2 |
Program continues with the inner loop when J becomes 2, note I is 2 |
x = 28*2+3, the new value for x is 59 |
2 |
3 |
Program continues with the inner loop when J becomes 3, note I is 2 |
X = 59 |
Is the final value for x when both loops are completed |
Case 4:
x |
i |
j |
NOTES |
0 |
x is initialized to 0 |
||
x = 0+1*1, the new value for x is 1 |
1 |
1 |
First we start with the outer loop (for I ) and continue with inner loop (for J ). |
x = 1+1*2, the new value for x is 3 |
1 |
2 |
Still in the inner loop when J becomes 2, note I is 1 |
x = 3+1*3, the new value for x is 6 |
1 |
3 |
Still in the inner loop when J becomes 3, note I is 1 |
x = 6+2*1, the new value for x is 8 |
2 |
1 |
Here program continues with the outer loop, where I is 2 and moves to inner loop where J is 1 |
x = 8+2*2, the new value for x is 12 |
2 |
2 |
Program continues with the inner loop when J becomes 2, note I is 2 |
x = 12+2*3, the new value for x is 18 |
2 |
3 |
Program continues with the inner loop when J becomes 3, note I is 2 |
X = 18 |
Is the final value for x when both loops are completed |