In: Computer Science
Without using a computer determine the output for each of the
loops.
1.
int x = -2;
while (x <= 2) {
System.out.println(x * x);
x = x + 1;
}
2.
int x = 1;
int y = x;
while (x <= 3) {
System.out.println(x + y);
y = x * 5;
x = x + 2;
}
3.
int x = 0;
int y = -2;
while (x <= 2) {
y = x * x + 1;
System.out.println(x + “, ” + y);
x = x + 1;
}
4.
int x = 2;
int y = x + 1;
while (x <= 14) {
System.out.println(x – y);
y = x * 4;
x = x + 3;
}
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++;
}
6.
int sum = 0;
for (int i = 1; i <= 10; i++)
sum = sum + 1;
System.out.println(sum);
7.
for (int i = 0; i <=10; i += 2)
System.out.println(i * i);
8.
for (int i = 6; i <= 11; i++) {
System.out.print(i + 3);
System.out.println(i);
}
9.
for (int j = 15; j > 1; j--)
System.out.print(j);
10. for (int i= 1; i <= 5; i++){
for( j=1; j <=3; j++)
System.out.print(“*”);
System.out.println(“”);
}
(1) output :
4
1
0
1
4
Explanation : intitially x is -2 then we check if x is less than or equal to 2 since -2 is less than 2 , while loop starts and x*x is printed which is (-2)*(-2)=4 . Hence 4 is printed then we increment x by 1 so x becomes -2+1=-1
Then again while loop starts since -1 is less than 2, x*x= (-1*-1)=1.Hence 1 is printed then we increment x by 1 so x becomes -1+1=0
Then again while loop starts since 0 is less than 2, x*x= (0*0)=0.Hence 0 is printed then we increment x by 1 so x becomes 0+1=1
Then again while loop starts since 1 is less than 2, x*x= (1*1)=1.Hence 1 is printed then we increment x by 1 so x becomes 1+1=2
Then again while loop starts since 2 is equal to 2, x*x= (2*2)=4.Hence 4 is printed then we increment x by 1 so x becomes 2+1=3
Now since 3 is not less than or equal to 2 , while loop exits/stops executing.
(2) output :
2
8
Explanation : intitially x is 1 and we declare value of y equals to value of x , so y=1.Then we check if x is less than or equal to 3. since x=1 is less than 3 , while loop starts and prints (x+y)=1+1=2
Then we calculate y=x*5=1*5=5
And x=x+2=1+2=3
Now x=3,y=5
since x=3 is equal to 3 which is while loop condition , so while loop keeps running and x+y=3+5=8 is printed.
Then we calculate y=x*5=3*5=15
And x=x+2=3+2=5
Now x=5,y=15
since x=5 is not less than or equal to 3 which is while loop condition , so while loop exits i.e. we come out of while loop.
(3) output :
0,1
1,2
2,5
Explanation : intitially x is 0 and y is -2 .since x=0 is less than 2 which is while loop condition , while loop starts.
y is calculated as y=x*x+1=0*0+1=1
x,y is printed so 0,1 is printed since x=0 and y=1
x is again calculated as x=x+1=0+1=1 . so now x becomes x=1
since x=1 is less than 2 , while loop starts again ,
y is calculated as y=x*x+1=1*1+1=2
x,y is printed so 1,2 is printed since x=1 and y=2
x is again calculated as x=x+1=1+1=2 . so now x becomes x=2
since x=2 is equal to 2 , while loop starts again ,
y is calculated as y=x*x+1=2*2+1=5
x,y is printed so 2,5 is printed since x=2 and y=5
x is again calculated as x=x+1=2+1=3 . so now x becomes x=3
since x=3 is not less than or equal to 2 which is while loop condition , so while loop exits i.e. we come out of while loop.
(4) output :
-1
-3
-12
-21
-30
Explanation : intitially x is 2 and y is x+1=2+1=3. since x=2 is less than 14 which is while loop condition , while loop starts.
(x-y) is printed which is (2-3)= -1
y is again recalculated as y=x*4=2*4=8
x is again recalculated as x=x+3=2+3=5
since x=5 is less than 14 , while loop starts again ,
(x-y) is printed which is (5-8)= -3
y is again recalculated as y=x*4=5*4=20
x is again recalculated as x=x+3=5+3=8
since x=8 is less than 14 , while loop starts again ,
(x-y) is printed which is (8-20)= -12
y is again recalculated as y=x*4=8*4=32
x is again recalculated as x=x+3=8+3=11
since x=11 is less than 14 , while loop starts again ,
(x-y) is printed which is (11-32)= -21
y is again recalculated as y=x*4=11*4=44
x is again recalculated as x=x+3=11+3=14
since x=14 is equal to 14 , while loop starts again ,
(x-y) is printed which is (14-44)= -30
y is again recalculated as y=x*4=14*4=56
x is again recalculated as x=x+3=14+3=17
since x=17 is not less than or equal to 14 which is while loop condition , so while loop exits i.e. we come out of while loop.
(5) output :
Here will be printed 45 times
Explanation : Here inner while loop will run 9 times since y=1 and inner while loop will run until y<10 so it will run 9 times.And outer while loop wil run 5 times since x=1 and outer while loop will run until x is less than or equal to 5.
so when first outer while loop starts , inner while loop will run 9 times
similary when 5 outer while loop runs. inner while loop will run 5*9=45 times , printing Here 45 times.
(6) output:
10
Explanation : intially sum is 0. for loop runs 10 times since i=1 and it checks if i<=10 .sum=sum+1 will be calculated 10 times. each time we update sum value and increment i
sum=0
when i =1 , sum=sum+1=0+1=1
when i =2 , sum=sum+1=1+1=2
when i =3 , sum=sum+1=2+1=3
when i =4 , sum=sum+1=3+1=4
when i =5 , sum=sum+1=4+1=5
when i =6 , sum=sum+1=5+1=6
when i =7 , sum=sum+1=6+1=7
when i =8 , sum=sum+1=7+1=8
when i =9 , sum=sum+1=8+1=9
when i =10 , sum=sum+1=9+1=10
Execution then stops since i=10 will be incremented to 11 and for loop condition is not met which is i<=10
(7) output :
0
4
16
36
64
100
Explanation :
when i =0 , i*i=0*0=0 is printed , i+=2 i.e i is incremented by 2 so i=0+2=2
when i =2 , i*i=2*2=4 is printed , i+=2 i.e i is incremented by 2 so i=2+2=4
when i =4 , i*i=4*4=16 is printed , i+=2 i.e i is incremented by 2 so i=4+2=6
when i =6 , i*i=6*6=36 is printed , i+=2 i.e i is incremented by 2 so i=6+2=8
when i =8 , i*i=8*8=64 is printed , i+=2 i.e i is incremented by 2 so i=8+2=10
when i =10 , i*i=10*10=100 is printed , i+=2 i.e i is incremented by 2 so i=10+2=12
Execution stops since i>10 now and for loop condition is not met which is i<=10
(8)
output :
96
107
118
129
1310
1411
Explanation :
when i =6 , i+3=6+3=9 is printed followed by i which is 6 so 96 will be printed on same line(Note- print command is used not println to print 9 so after printing 9 we will not go to next line) and i is incremented by 1 so i=7
when i =7 , i+3=7+3=10 is printed followed by i which is 7 so 107 will be printed on same line(Note- print command is used not println to print 10 so after printing 10 we will not go to next line) and i is incremented by 1 so i=8
when i =8 , i+3=8+3=11 is printed followed by i which is 8 so 118 will be printed on same line(Note- print command is used not println to print 11 so after printing 11 we will not go to next line) and i is incremented by 1 so i=9
when i =9 , i+3=9+3=12 is printed followed by i which is 9 so 129 will be printed on same line(Note- print command is used not println to print 12 so after printing 12 we will not go to next line) and i is incremented by 1 so i=10
when i =10 , i+3=10+3=13 is printed followed by i which is 10 so 1310 will be printed on same line(Note- print command is used not println to print 13 so after printing 13 we will not go to next line) and i is incremented by 1 so i=11
when i =11 , i+3=11+3=14 is printed followed by i which is 11 so 1411 will be printed on same line(Note- print command is used not println to print 14 so after printing 14 we will not go to next line) and i is incremented by 1 so i=12
Execution stops since i>11 now and for loop condition is not met which is i<=11
(9) output:
15141312111098765432
Explanation : intially j=15 and it is decremented by 1 after printing j until j is less than 1
so 15 then 14 then 13 then 12 so on will be printed untill it less than 1 i.e 2
We have used print command(not println) so output is printed on same line.
(10)output :
***
***
***
***
***
Note : here if you dont declare j as int then error will be thrown instead of above output
for( j=1; j <=3; j++) should be for(int j=1; j <=3; j++)
Explanation : Here i represents no of rows and j represents no of colums .
when i = 1 , inner loop will run 3 times printing * followed by space thrice since inner loop condition is j =1 and j<=3.
when i = 2 , inner loop will run 3 times printing * followed by space thrice since inner loop condition is j =1 and j<=3.
when i = 3 , inner loop will run 3 times printing * followed by space thrice since inner loop condition is j =1 and j<=3.
when i = 4 , inner loop will run 3 times printing * followed by space thrice since inner loop condition is j =1 and j<=3.
when i = 5 , inner loop will run 3 times printing * followed by space thrice since inner loop condition is j =1 and j<=3.
so total 5 rows will be printed and in each row there will be stars followed by space combination printed thrice