What will be the value of sum after the following program is executed?
void main()
{
int sum=1, index = 9;
do{
index = index – 1;
sum *= 2;
}while( index > 9 );
}
void main()
{
int sum=1, index = 9;
do{
index = index – 1;
sum *= 2;
}while( index > 9 );
}
A. 1
B. 2
C. 9
D. 0.5
E. 0.25
Answer: Option B
the program ran successfully and terminated. because there is not any printing statement for anything.
It is property of do-while loop to get executed at least once irrespective of condition.
here the value of index is initially 9.
when this goes int do-while loop it gets decremented by 1 as index=index-1;
then sum *=2 is executed which can be expanded as sum=sum*2;
so sum becomes 2;
when condition is checked index=8 so 8>9 becomes false,
thus finally value of sum becomes 2.
how
give explaination of this program