Proper use of the for loop

I am taking the C ++ class right now, and I know that the for loop will work like that, but I was wondering if it was β€œnormal” or β€œright”

for (int i = 0; getline(cin, input); i++)

If it is instead

int i = 0;
while (getline(cin, input))
    i++;

I'm just curious because I know that the for loop should be a contour, but the condition for this is not related to the number of times that I control.

+3
source share
6 answers

Yes, the for loop rules are pretty simple. As long as you follow the rules, the syntax can be anything you want.

Of the two, just select the one you find the most readable.

+6
source

, "" for: ( , K & R "", ), , .

for while , continue , . continue.

+5

, while.

, , , , .

+3

. , , , , "" .

, , . , , .. , . , , - .

, .

+2

.

while - , , . for , , , .

+1
source

While I would normally not think of a loop foras limited to a counted loop with a predetermined boundary, I would usually think of it as where a part of the loop was directly involved, incrementmoving towards the end of the loop.

For example, I think it’s quite reasonable to go through a linked list with something like:

for (ptr = list_head; ptr != NULL; ptr = ptr->next)
    process(ptr->data);
+1
source

All Articles