What's the difference?

Asked 2 years ago, Updated 2 years ago, 30 views


int t, i;
t = 0;
for (i = 0; i < 10; i++) {
t++;
}
printf("%d %d\n", t,i);

int a, i, j;
for (i = 0; i < 10; i++)
for (j = 0; j < 5; j++)
a = i +j;
printf("%d", a);

What's the difference? I'd appreciate it if you could explain in detail why the first one is 1010. The second one is i is 9 and j is 4, so it's 13 right?

c

2022-09-22 21:58

1 Answers

A for repeat statement consists of for(initialization; condition; operation) { code to be repeated; } After performing the code in the for statement, perform a operation and check if it meets the condition.
So the first code is that the value of the variable i increases from 0 to 10, and then the condition is checked and the for statement ends, and the value of i is output to 10.
The second code continues to assign a value to variable a within the for statement, and the last action is if the value of i is 9, and the value of j is 4. The value of a becomes 13 because the repeat statement ends with 13 assigned as a value of a.


2022-09-22 21:58

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.