Computer Applications
How many times will the following loop execute?
int x = 2, y = 50;
do
{
++x;
y -= x++;
} while (x <= 10);
return y;
Answer
The given loop will execute 5 times.
Explanation
| Iteration | x | y | Remarks |
|---|---|---|---|
| 2 | 50 | Initial values | |
| 1 | 4 | 47 | ++x ⟹ x = 3 y = y - x++ ⟹ y = 50 - 3 ⟹ y = 47 x = 4 |
| 2 | 6 | 42 | ++x ⟹ x = 5 y = y - x++ ⟹ y = 47 - 5 ⟹ y = 42 x = 6 |
| 3 | 8 | 35 | ++x ⟹ x = 7 y = y - x++ ⟹ y = 42 - 7 ⟹ y = 35 x = 8 |
| 4 | 10 | 26 | ++x ⟹ x = 9 y = y - x++ ⟹ y = 35 - 9 ⟹ y = 26 x = 10 |
| 5 | 12 | 16 | ++x ⟹ x = 11 y = y - x++ ⟹ y = 26 - 11 ⟹ y = 15 x = 12 Loop terminates |
Related Questions
If a = 24, b = 15, find the value of:
a += b++ * 5 / a++ + bGive the prototype of a function search, which receives a sentence sentc and a word wrd and returns 1 or 0.
If
int a[] = {7, 3, 4, 8, 9, 2};what are the values of x and y?(a) x = a[1] * a[0] + a[3]
(b) y = a.length
Write the return types of the following library functions:
(a)
isLetterOrDigit(char)(b)
replace(char, char)