Loop
Loop
1. Write a piece of code that displays the following pattern in the console, using a while loop, and then
using a for loop. Note that the three dots represent a continuation of the pattern (in all 257 integers
should be displayed on the console).
2. Write a piece of code that displays the following pattern in the console, using a while loop, and then
using a for loop. Note that the three dots represent a continuation of the pattern.
3. Write a piece of code that displays the following pattern in the console, using a while loop, and then
using a for loop. Note that the three dots represent a continuation of the pattern.
1 1 2 4 8 ... 8388608
4. Write a piece of code that displays the following pattern in the console such that the total number of
integers displayed is 20, using a while loop, and then using a for loop.
1 1 2 4 8 ...
5. Write a piece of code that displays the following pattern in the console, using a while loop, and then
using a for loop. Hint: each number is half of the previous number.
6. Write a piece of code that displays the following pattern in the console, using a while loop, and then
using a for loop. Hint: each number is half of the previous number.
Processing
1 int a = 10, b = 1000;
2 int c = 4;
3 while(a < b) {
4 c = c + 1;
5 a = a + c;
6 b = b / 4;
7 }
8 print(a+" "+b+" "+c);
8. What is the output of the following code (values of a,b,c )? Draw a truth table to demonstrate your
understanding.
Processing
1 int a = 10, b = 1000;
2 int c = 4;
3 while(a < b && b%c == 0) {
4 c = c + 2;
5 a = a + c;
6 b = b / 5;
7 }
8 print(a+" "+b+" "+c);
9. What is the output of the following code (values of a,b,c )? Draw a truth table to demonstrate your
understanding.
Processing
1 int a = 10, b = 1000;
2 int c = 4;
3 while(a < b || b > c) {
4 c+=3;
5 a+=c;
6 b/=6;
7 }
8 print(a+" "+b+" "+c);
10. What is the output of the following code (values of a,b,c )? Draw a truth table to demonstrate your
understanding.
Processing
1 int a = 10, b = 1000;
2 int c = 4;
3 while(a < b) {
4 if(c%2 == 0) {
5 a*=2;
6 b/=2;
7 }
8 else {
9 a+=10;
10 b-=10;
11 }
12 c++;
13 }
14 print(a+" "+b+" "+c);
11. What is the output of the following code (values of a,b,c )? Draw a truth table to demonstrate your
understanding.
Processing
1 int a = 10, b = 1000;
2 int c = 10;
3 if(a < b) {
4 while(b > a) {
5 a*=2;
6 c++;
7 }
8 }
9 else {
10 while(a >= b) {
11 b/=2;
12 c--;
13 }
14 }
15 print(a+" "+b+" "+c);
12. What is the output of the following code (values of a,b,c )? Draw a truth table to demonstrate your
understanding.
Processing
1 int a = 1000, b = 10;
2 int c = 10;
3 if(a < b) {
4 while(b > a) {
5 a*=2;
6 c++;
7 }
8 }
9 else {
10 while(a >= b) {
11 a/=2;
12 c--;
13 }
14 }
15 print(a+" "+b+" "+c);
13. Write the for equivalent of the following while loop. Also, write down the output of the program
assuming you don’t need the variable a after the loop terminates.
Processing
1 int a = 1, c = 10;
2 while(a <= 5) {
3 c+=a;
4 a++;
5 }
6 print(c);
14. Write the for equivalent of the following while loop. Also, write down the output of the program
(and a truth table for better understanding).
Processing
1 int a = 1, c = 10;
2 while(a <= 5) {
3 c+=a;
4 a++;
5 }
6 print(a+" "+c);
15. Write the for equivalent of the following while loop. Also, write down the output of the program
(and a truth table for better understanding).
Processing
1 int a = 1, b = 120, c = 10;
2 while(b%a == 0) {
3 c+=a;
4 a++;
5 }
6 print(a+" "+b+" "+c);
16. Assuming a display window that is 300 pixels wide, write a piece of code that displays the following
pattern, using a while loop, and then using a for loop. The boundaries need not be drawn.
17. Assuming a display window that is 320 pixels wide, write a piece of code that displays the following
pattern, using a while loop, and then using a for loop. The boundaries need not be drawn.
18. Write a piece of code that displays the following pattern, using a while loop, and then using a for loop.
There should be 8 rectangles along the diagonal. The boundaries need not be drawn.
19. What is the output of the following program (the value of variable result )?
Processing
1 int result = 0;
2 for(int i=1; i<4; i++) {
3 for(int k=3; k<5; k++) {
4 result++;
5 }
6 }
7 print(result);
20. What is the output of the following program (the value of variable result )?
Processing
1 int result = 0;
2 for(int i=1; i<7; i+=2) {
3 for(int k=3; k<=7; k+=4) {
4 result++;
5 }
6 }
7 print(result);
21. What is the output of the following program (the value of variable result )?
Processing
1 int result = 0;
2 for(int i=1; i<7; i+=2) {
3 for(int k=7; k>=i; k-=2) {
4 result++;
5 }
6 }
7 print(result);
22. What is the output of the following program (the value of variable result )? Note:
String + int acts as concatenation. For example, "12" + 5 evaluates to "125" .
Processing
1 String result = "";
2 for(int i=1; i<7; i+=2) {
3 for(int k=7; k>=i; k-=2) {
4 result = result + k;
5 }
6 }
7 print(result);
23. What is the output of the following program (the values of all variables a , result )?
Processing
1 int a = 1;
2 int result = 0;
3 while(a <= 4) {
4 int b = 1;
5 while(b <= 3) {
6 result++;
7 b++;
8 }
9 a++;
10 }
11 print(a+" "+result);
24. What is the output of the following program (the values of a, result )?
Processing
1 int a = 20;
2 int result = 0;
3 while(a > 12) {
4 int b = a;
5 while(b <= 20) {
6 result++;
7 b+=3;
8 }
9 a-=2;
10 }
11 print(a+" "+result);
Processing
1 size(600, 300);
2 boolean flag = true;
3 for(int i=0; i < width; i+=width/3) {
4 for(int k=0; k < height; k+=height/3) {
5 if(flag) {
6 fill(255);
7 }
8 else {
9 fill(0);
10 }
11 rect(i, k, width/3, height/3);
12 flag = !flag;
13 }
14 }
26. Write a piece of code that displays the following pattern in the console, using a nested while loop, and
then using a nested for loop (finally a while inside a for, and also a for inside a while for the sake of
completeness).
1 1 2 3 4 5
2 2 3 4 5 6
3 3 4 5 6 7
4 4 5 6 7 8
5 5 6 7 8 9
6 6 7 8 9 10
27. Write a piece of code that displays the following pattern in the console, using a nested while loop, and
then using a nested for loop (finally a while inside a for, and also a for inside a while for the sake of
completeness).
1 20 18 16 14 12 10 8 6 4 2 0
2 18 16 14 12 10 8 6 4 2
3 16 14 12 10 8 6 4
4 14 12 10 8 6
5 12 10 8
6 10
28. Write a piece of code that displays the following pattern in the console, using a nested while loop, and
then using a nested for loop (finally a while inside a for, and also a for inside a while for the sake of
completeness). The rules are that the first line starts with -20 and each number of the first line is 5
lesser than the previous number on that line. The line finishes as soon as the first multiple of 7 is
encountered and displayed. You are also required to figure out the change from one line to other, and
the patten in each of the subsequent lines.