0% found this document useful (0 votes)
10 views13 pages

Assignment 3

The document outlines various common programming errors in Java, including incorrect variable names in for-loops, infinite loops due to missing increments, and off-by-one errors. Each example is accompanied by a correction and the expected output. The document serves as a guide for debugging and understanding control flow in Java programming.

Uploaded by

NEHA SHARMA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views13 pages

Assignment 3

The document outlines various common programming errors in Java, including incorrect variable names in for-loops, infinite loops due to missing increments, and off-by-one errors. Each example is accompanied by a correction and the expected output. The document serves as a guide for debugging and understanding control flow in Java programming.

Uploaded by

NEHA SHARMA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

1.

For-loop wrong variable name

java

CopyEdit

for (int i = 0; i < 5; i++) {

System.out.println(j);

 Correction: use System.out.println(i);

 Output:

CopyEdit

2. Infinite while

java

CopyEdit

int n = 0;

while (n < 3) {

System.out.println(n);

 Problem: n never increments → infinite loop

 Correction: add n++;

 Output:

CopyEdit
0

3. Do-while runs once

java

CopyEdit

int m = 5;

do {

System.out.println(m);

} while (m < 3);

 Note: Even though the condition is false initially, body runs once

 Output:

CopyEdit

4. Off-by-one in for-loop

java

CopyEdit

for (int i = 1; i <= 5; i++) {

System.out.println(i);

 This is correct: prints 1 through 5

 Output:

CopyEdit

1
2

5. While team summation

java

CopyEdit

int sum = 0, x = 1;

while (x <= 10) {

sum += x;

x++;

System.out.println(sum);

 Correct

 Output:

CopyEdit

55

6. Do-while sum of digits

java

CopyEdit

int num = 123, sum = 0;

do {

sum += num % 10;

num /= 10;
} while (num > 0);

System.out.println(sum);

 Correct

 Output:

CopyEdit

7. Nested for loops multiplication table (bug: wrong loop limit)

java

CopyEdit

for (int i = 1; i <= 3; i++) {

for (int j = 1; j <= 3; j++) {

System.out.print(i * j + " ");

System.out.println();

 Output:

CopyEdit

123

246

369

8. While decrement (bug: mis-initialized)

java

CopyEdit

int c = 5;
while (c > 0) {

System.out.print(c + " ");

c--;

 Output:

CopyEdit

54321

9. Do-while with boolean flag

java

CopyEdit

boolean flag = false;

do {

System.out.println("Hello");

} while (flag);

 Runs once

 Output:

nginx

CopyEdit

Hello

10. For-loop skipping even numbers

java

CopyEdit

for (int i = 0; i < 10; i++) {

if (i % 2 == 0) continue;
System.out.print(i + " ");

 Output:

CopyEdit

13579

11. Off-by-one sum (bug: < vs <=)

java

CopyEdit

int s = 0;

for (int i = 1; i < 5; i++) {

s += i;

System.out.println(s);

 Sums 1 to 4

 Output:

CopyEdit

10

12. Reverse countdown using while

java

CopyEdit

 Output:

CopyEdit

543210
13. Counting down – do-while variant

java

CopyEdit

int r = 3;

do {

System.out.print(r + " ");

r--;

} while (r > 0);

 Output:

CopyEdit

321

14. Sum of even up to N

java

CopyEdit

int N = 6, se = 0;

for (int i = 2; i <= N; i += 2) {

se += i;

System.out.println(se);

 Output:

cpp

CopyEdit

12 // (2 + 4 + 6)
15. Buggy while loop factorial

java

CopyEdit

int f = 1, k = 1;

while (k <= 5) {

f *= k;

k++;

System.out.println(f);

 Correct

 Output:

CopyEdit

120

16. Do-while and modulus

java

CopyEdit

int n2 = 2;

do {

System.out.println(n2);

n2 *= 2;

} while (n2 <= 16);

 Output:

CopyEdit

4
8

16

17. For-loop skipping numbers (bug: continue logic)

java

CopyEdit

for (int i = 1; i <= 5; i++) {

if (i == 3) continue;

System.out.print(i + " ");

 Output:

CopyEdit

1245

18. While loop reading until negative

java

CopyEdit

int[] arr = {1, 3, 5, -1, 7};

int idx = 0;

while (idx < arr.length && arr[idx] >= 0) {

System.out.print(arr[idx] + " ");

idx++;

 Stops at –1

 Output:

CopyEdit
135

19. Do-while array sum example

java

CopyEdit

int[] a = {2, 4, 6};

int i = 0, total = 0;

do {

total += a[i];

i++;

} while (i < a.length);

System.out.println(total);

 Output:

CopyEdit

12

20. For loop range bug (incorrect increment)

java

CopyEdit

for (int i = 0; i <= 10; i += 3) {

System.out.print(i + " ");

 Output:

CopyEdit

0369
21. While loop conditional inside

java

CopyEdit

int x2 = 1;

while (x2 <= 10) {

if (x2 == 7) break;

System.out.print(x2 + " ");

x2++;

 Output:

CopyEdit

123456

22. Do-while break scenario

java

CopyEdit

int y = 1;

do {

if (y == 3) break;

System.out.print(y + " ");

y++;

} while (y <= 5);

 Output:

CopyEdit

12
23. For with empty body bug

java

CopyEdit

int sum2 = 0;

for (int i = 1; i <= 5; i++);

sum2 += 10;

System.out.println(sum2);

 Bug: semicolon ends loop → executes sum2 += 10 once

 Output:

CopyEdit

10

24. While with post-increment bug

java

CopyEdit

int p = 0;

while (p++ < 3) {

System.out.print(p + " ");

 Evaluate:

o check p++<3 (uses old p), then increment

o inside prints new p

 Output:

CopyEdit

123
25. Do-while with pre-decrement

java

CopyEdit

int z = 4;

do {

System.out.print(z-- + " ");

} while (--z > 0);

 Let’s simulate:

o first print 4, z becomes 3

o --z makes it 2 → >0 → loop

o print 2, then z– gives 1

o --z makes 0 → not >0 → end

 Output:

CopyEdit

42

You might also like