220k views
0 votes
Consider the following C program segment. Rewrite it using no gotos or breaks. j = -3; for (i = 0; j < 3; i++) { switch (j + 2) { case 3: case 2: j--; break; case 0: j + 2; break; default: j = 0; } if (j > 0) break; j=3-i }

User SP Sandhu
by
7.6k points

1 Answer

4 votes

Answer:

goto statements are used to control the flow of execution and the program can directly jump to the desired location without using any break statements.

Here is the code using goto and break :

j = -3;

i = 0;

start_loop:

switch (j + 2) {

case 3:

case 2:

j--;

if (j > 0)

goto end_loop;

break;

case 0:

j += 2;

if (j > 0)

goto end_loop;

break;

default:

j = 0;

break;

}

if (j <= 0) {

j = 3 - i;

if (j <= 0)

goto start_loop;

}

end_loop:

User Dvjanm
by
8.4k points