Computer Science, asked by shafeeqkhan022, 8 months ago

How many times does the following code segment execute

int x=1, y=10, z=1;​

Answers

Answered by manpreet3676
0

Answer:

bhehhejhdhhrujrhhwhirhe

Answered by mad210203
0

The code will execute only 1 time.

Explanation:

Given program:

int x=1, y=10, z=1;

do{y-–; x++; y-=2; y=z; z++} while (y>1 && z<10);

  • Given program is written in C language.
  • Given program contains a do while loop.
  • A do while loop is similar to a while loop, but do loop will execute at least one time.

To find how many times the code will execute, we will do some modifications in the program.

Modified program:

#include<stdio.h>

void main()

{

int x=1, y=10, z=1;

do {

   y--;  

   x++;

   y-=2;

   y=z;

   z++;

   printf("Executed");

}

while (y>1 && z<10);

}

Let us understand the program now.

#include<stdio.h>

void main()

{

int x=1, y=10, z=1; //Declaration of variables

do {

   y--;  //y value is decremented by one; y = 9

   x++; //x value is incremented by one; x = 2

   y-=2; //y = y - 2; y = 9 - 2 = 7

   y=z; //y = 1

   z++; //z value is incremented by one; z = 2

   printf("Executed"); // This statement prints Executed when do loop executes.

}

//After execution of statements of do loop, it will check the condition.

// Here, condition is y>1 and z<10. But the value of y is 1. So, the condition fails and while loop terminates.

// But do while loop is different from while loop. In do while loop, statements are executed first and then condition is checked. So, all statements in do are executed once. So, the output will be Executed.

while (y>1 && z<10);

}

Refer the attached image for output.

Therefore, the given code will execute only once.

Attachments:
Similar questions