To understand the beginner loop

I am currently studying C and want to check the correctness of my understanding of the loop for.

Does the conclusion appear A is 6, because after the 5th cycle of the cycle, adding +1 to a(which makes it 6) is done, and then the condition stops because it is no longer <= 5?

int a;
float b;

b = 0;

for (a = 1; a <= 5; a++)
    b = b + 0.5;

printf ("A is %d\t\t B is %.2f\n", a, b);

Exit

A is 6 B is 2.50
+4
source share
2 answers

Yes.

When the a == 5condition is a <= 5true, therefore, the body of the loop ( b = b + 0.5;) is satisfied . After the body, the part is a++always performed.

It does a == 6. Then the condition a <= 5is equal false, so the cycle ends.

.

+6

. for (init; condition; finish) , :

init;
while (condition) {
    ...insert code here...
    finish;
}
+3

Source: https://habr.com/ru/post/1610639/


All Articles