Open calbch opened 2 months ago
I think you are right, but this exercise was done by @7etsuo. So let's wait for his explanation of what he meant by that.
I'll take a look at this tonight.
Reference Code:
/** LEARNING
* Your goal is to understand how the for statement works.
*
* The for statement has the following syntax:
*
* for (initialization; condition; increment) {
* statement;
* statement;
* ...
* }
*
* The for statement is equivalent to the following while statement:
*
* initialization;
* while (condition) {
* statement;
* statement;
* ...
* increment;
* }
*/
/** EXERCISE
* Your job is to write a for statement equivalent to the following
* while statement:
*
* i = 0; // initialization
* while (i < 10) { // condition
* printf("%d\n", i);
* verify_count(&count, i);
* i++; // increment
* }
*/
// ❌ I AM NOT DONE
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
// DO NOT CHANGE THIS
static int track = 0, count = 0;
// DO NOT CHANGE THIS
void verify_count (int *count, int i) {
assert(i == track++);
(*count)++;
}
int main() {
int i = rand() % 100 + 11; // DO NOT CHANGE THIS
// YOUR CODE HERE
for (initialization; condition; increment) {
// END YOUR CODE
printf("%d\n", i);
verify_count(&count, i); // DO NOT CHANGE THIS
}
// BONUS: print the value of i here and try to understand why it's 10
// DO NOT CHANGE THIS
assert (count == 10);
return 0;
}
Solution:
/ * i = 0; // initialization
* while (i < 10) { // condition
* printf("%d\n", i);
* verify_count(&count, i);
* i++; // increment
* }
*/
int i = rand() % 100 + 11; // DO NOT CHANGE THIS
// YOUR CODE HERE
for (i=0; i<10; i++) {
// END YOUR CODE
printf("%d\n", i);
verify_count(&count, i); // DO NOT CHANGE THIS
}
// BONUS: print the value of i here and try to understand why it's 10
/* Hint look at the last value i printed in the for loop */
printf("i=%d\n", i);
// DO NOT CHANGE THIS
assert (count == 10);
return 0;
In the context of C programming, within a for loop, the initialization expression is distinct from variable declaration, although it often includes variable declarations as a convenience.
Here's a breakdown:
Variable Declaration: This involves specifying a type and associating it with a variable name. For example: int i; declares a variable i of type int.
Initialization: This step assigns a value to the variable at the time of its creation or at the start of the loop. In a for loop, the initialization expression is executed only once, before the loop begins. It often includes declaring a variable, but can also be used to set an existing variable to a new value.
for (int i = 0; i < 10; i++) {
}
int i = 0 is both declaring and initializing i. However, if i were declared before the loop:
int i;
for (i = 0; i < 10; i++) {
}
Here, i = 0 in the loop is purely initialization, not declaration.
The details are in the definitions.
Shouldn't it say "why it's not 10" in the bonus?
Since
i
is shadowed inside of the scope of the for loop, final value would be the initial random value assigned during it's definition.What am I missing here?