In: Computer Science
The output for the given loops:
1) int x = -2;
while (x <= 2) {
System.out.println(x * x);
x = x + 1;
}
Solution:
Iteration Number |
x |
x<=2 |
Println(x*x) |
x=x+1 |
1st |
-2 |
-2<=2 True |
-2* -2 = 4 |
x= -2+1 = -1 |
2nd |
-1 |
-1<=2 True |
-1 * -1 =1 |
x=-1 + 1 =0 |
3rd |
0 |
0<=2 True |
0*0=0 |
x=0+1=1 |
4th |
1 |
1<=2 True |
1*1 =1 |
x=1+1=2 |
6th |
2 |
2<=2 True |
2*2=4 |
x=2+1=3 |
7th |
3 |
3<=2 False |
Will not execute |
Will not execute |
Output :
4
1
0
1
4
2) int x = 1;
int y = x;
while (x <= 3) {
System.out.println(x + y);
y = x * 5;
x = x + 2;
}
Solution:
Iteration Number |
x |
y |
x<=3 |
Println(x+y) |
y=x*5 |
x=x+2 |
1st |
1 |
1
|
1<=3 True |
1+1=2 |
1*5=5 |
1+2=3 |
2nd |
3 |
5
|
3<=3 True |
3+5=8 |
3*5=15 |
3+2=6 |
3rd |
6 |
15
|
6 <=3 False |
Will not execute |
Will not execute |
Will not execute |
Output :
2
8
3) int x = 0;
int y = -2;
while (x <= 2) {
y = x * x + 1;
System.out.println(x + “, ” + y);
x = x + 1;
}
Solution:
Iteration Number |
x |
y |
x<=2 |
y=x*x+1 |
Print(x+”,”+y) |
x=x+1 |
1st |
0 |
-2 |
0<=2 True |
0*0+1=1 |
0 , 1 |
0+1=1 |
2nd |
1 |
1 |
1<=2 True |
1*1+1=2 |
1 , 2 |
1+1=2 |
3rd |
2 |
2 |
2<=2 True |
2*2+1=5 |
2 , 5 |
2+1=3 |
4th |
3 |
5
|
3<=2 False |
Will not execute |
Will not execute |
Will not execute |
Output:
0 , 1
1 , 2
2 , 5
4) int x = 2;
int y = x + 1;
while (x <= 14) {
System.out.println(x – y);
y = x * 4;
x = x + 3;
}
Solution:
Iteration Number |
x |
y |
x<=14 |
Println(x-y) |
y=x*4 |
x=x+3 |
1st |
2 |
3 |
2<=14 True |
2-3= -1 |
2*4=8 |
2+3=5 |
2nd |
5 |
8 |
5<=14 True |
5-8= -3 |
5*4=20 |
5+3=8 |
3rd |
8 |
20 |
8<=14 True |
8-20= -12 |
8*4=32 |
8+3=11 |
4th |
11 |
32 |
11<=14 True |
32-11= -21 |
11*4=44 |
11+3=14 |
6th |
14 |
44
|
14<=14 True |
14-44= -30 |
14*4=56 |
14+3=17 |
7th |
17 |
56
|
17<=14 False |
Will not execute |
Will not execute |
Will not execute |
Output:
-1
-3
-12
-21
-30
5) How many times will “Here” be printed?
int x = 1;
while (x <= 5) {
int y = 1;
while (y < 10){
System.out.println(“Here”);
y++;
}
x++;
}
Solution:
Here the Outer loop gets excuted for 5 times and the inner loop gets executed for 9 times . The Inner while loop is used to print "Here" 9 times . The outer while is used to print these 9 "Here" 5 times.
As soon the condtion for the inner while loop becomes false i.e when y becomes 10 then it comes out of the loop and the outer loop is executed again.
The execution happens like this:
1st Iteration: The outer while loop gets executed 1st time when x=1 and the inner for loop gets executed while y=1,2.....9 and prints "Here "9 times one below the other.
2nd Iteration:The outer while loop gets executed 2nd time when x=2 and the inner for loop gets executed while y=1,2.....9 and prints "Here" 9 times one below the other.
3rd Iteration:The outer while loop gets executed 3rd time when x=3 and the inner for loop gets executed while y=1,2.....9 and prints "Here" 9 times one below the other.
4th Iteration:The outer while loop gets executed 4th time when x=4 and the inner for loop gets executed while y=1,2.....9 and prints "Here" 9 times one below the other.
5th Iteration:The outer while loop gets executed 5th time when x=5 and the inner for loop gets executed while y=1,2.....9 and prints "Here" 9 times one below the other.
6th Iteration:The condition of the outer loop becomes false i.e i=6 hence it comes out of the loop .
As the outer loop is excuted 5 times and inner loop 9 times so 'Here' gets printed 45 times each 'Here' on next line.
Output:
"Here" is printed 45 times on separate lines.
6) int sum = 0;
for (int i = 1; i <= 10; i++)
sum = sum + 1;
System.out.println(sum);
Solution:
Iteration Number |
sum |
i |
i<=10 |
sum=sum+1 |
i++ |
1st |
0 |
1 |
1<=10 True |
0+1=1 |
2 |
2nd |
1 |
2 |
2<=10 True |
1+1=2 |
3 |
3rd |
2 |
3 |
3<=10 True |
2+1=3 |
4 |
4th |
3 |
4 |
4<=10 True |
3+1=4 |
5 |
5th |
4 |
5 |
5<=10 True |
4+1=5 |
6 |
6th |
5 |
6 |
6<=10 True |
5+1=6 |
7 |
7th |
6 |
7 |
7<=10 True |
6+1=7 |
8 |
8th |
7 |
8 |
8<=10 True |
7+1=8 |
9 |
9th |
8 |
9 |
9<=10 True |
8+1=9 |
10 |
10th |
9 |
10 |
10<=10 True |
9+1=10 |
11 |
11th |
10 |
11 |
11<=10 False |
Will not execute |
Will not execute |
The final Sum value will be printed as output.
Output:
10
7)for (int i = 0; i <=10; i += 2)
System.out.println(i * i);
Solution:
Iteration Number |
i |
i<=10 |
Println(i*i) |
i+=2 |
1st |
0 |
0<=10 True |
0*0=0 |
0+2=2 |
2nd |
2 |
2<=10 True |
2*2=4 |
2+2=4 |
3rd |
4 |
4<=10 True |
4*4=16 |
4+2=6 |
4th |
6 |
6<=10 True |
6*6=36 |
6+2=8 |
6th |
8 |
8<=10 True |
8*8=64 |
8+2=10 |
7th |
10 |
10<=10 True |
10*10=100 |
10+2=12 |
8th |
12 |
12<=10 False |
Will not execute |
Will not execute |
Output:
0
4
16
36
64
100
8)for (int i = 6; i <= 11; i++) {
System.out.print(i + 3);
System.out.println(i);
}
Solution:
Iteration Number |
i |
i<=11 |
Print(i+3) |
Println(i) |
i++ |
1st |
6 |
6<=11 True |
6+3=9 |
6 |
7 |
2nd |
7 |
7<=11 True |
7+3=10 |
7 |
8 |
3rd |
8 |
8<=11 True |
8+3=11 |
8 |
9 |
4th |
9 |
9<=11 True |
9+3=12 |
9 |
10 |
6th |
10 |
10<=11 True |
10+3=13 |
10 |
11 |
7th |
11 |
11<=11 True |
11+3=14 |
11 |
12 |
8th |
12 |
12<=11 False |
Will not execute |
Will not execute |
Will not execute |
note:Here print will print the output on same line i.e 9 and println will move to the next line after printing the output on the same line so it becomes 96 and when next time it prints 10 it starts on next line.
Output:
96
107
118
129
1310
1411
9)for (int j = 15; j > 1; j--)
System.out.print(j);
Solution:
Here the for loop gets executed until the condtion becomes false . Now j =15 and after every iteration the value of j is decremented by 1 i.e j =14 then j=13 so this will continue unt