211k views
2 votes
What is the value of balance after the following code isexecuted?

int balance=10;

while (balance>=1) {
if(balance<9)break;
balance=balance -9;
}
A. -1
B. 0
C. 1
D. 2

User Jouby
by
4.6k points

1 Answer

6 votes

Answer:

C. 1

Step-by-step explanation:

Code

int balance=10;

while (balance>=1)

{

break;

balance=balance -9;

}

We are initializing variable 'balance' of integer type with value 10 in the first line of code. int balance=10;

Then we will check balance value (10) with 1 in While loop.

while (balance>=1)

while (10>=1)

So, 10 is greater than 1. we will get into this loop.

In loop,there is if condition, we will put balance value and check the condition.

if(balance<9)

if(10<9)

So 10 is greater than 9,so this condition fails and we will not get into this condition.

After the if condition, We will get the arithmetic line of code -

balance=balance -9;

In code ,balance value is 10 then applying the value in this line-

balance=10 -9;

So,the balance value is updated as 1.

So, there is no line of code are left in loop,we will again check the while loop's condition

while (balance>=1)

while (1>=1)

As 1 is equal to 1,this condition is true and we will get into the loop

In loop,there is if condition, we will put balance value and check the condition.

if(balance<9)

if(1<9)

So 1 is smaller than 9,so this condition is true and we will get into this condition.

break;

We will encounter break statement,this statement is used to terminate the loop.So,it will terminate the while loop and final updated balance value will be 1.

C program for verifying this code

#include <stdio.h>

int main()

{

int balance=10;

while (balance>=1)

{

if(balance<9)

break;

balance=balance-9;

}

printf("%d",balance);

return 0;

}

//You can copy this code and check on any ide.

Output

1

User Marinus
by
4.1k points