notes/education/software development/ECE1400/Chapter 6 Exercises.md

58 lines
1.1 KiB
Markdown
Raw Permalink Normal View History

2024-09-25 03:56:16 +00:00
> 1. What output does the following program fragment produce?
```c
i = 1;
while (i <= 128) {
printf("%d ", i);
i *= 2;
}
```
Output:
```
1 2 4 8 16 32 64 128
```
> 2. What output does the following program fragment produce?
```c
i = 9384
2024-09-25 04:01:16 +00:00
do {
printf("%d ", i);
i /= 10;
} while (i <= 128);
```
Output:
```
9384 938 93 9
```
> 3. What output does the following `for` statement produce?
```c
for (i = 5, j = i - 1; i > 0, j > 0; --i, j = i - 1)
printf("%d ", i);
2024-09-25 04:06:16 +00:00
```
Output:
```
5 4 3 2
```
> 4. Which one of the following statements is not equivalent to the other two (assuming that the loop bodies are the same?)
```c
2024-09-25 04:11:16 +00:00
for (i = 0; i < 10; i++) // (a)
for (i = 0; i < 10; ++i) // (b)
for (i = 0; i ++ < 10; ) // (c)
```
Answer:
C is not the same as A and B, because the increment takes place before the loop body is executed.
> 5. Which one of the following statements is not equivalent to the other two (assuming that the loop bodies are the same)?
```c
while (i < 10) {...} // (a)
for (; i < 10;) {...} // (b)
do {...} while (i < 10); // (c)
```
Answer:
C is not the same as A and B, because the block is executed before the condition is checked.