Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to do multiple operations in increment part of for loop in C/C++?

Is it possible to do multiple operations in increment part of for loop in C/C++? Something like this:

int a = 0, b = 0, c = 5;
for(; a < c; increase a by 1 and increase b by 2)
like image 519
Luka Tiger Avatar asked Oct 07 '13 23:10

Luka Tiger


2 Answers

Use the comma operator:

for (; a < c; ++a, b += 2)
like image 170
David G Avatar answered Oct 05 '22 01:10

David G


Yes it is possible. You can also declare multiple variables inside the loop and don't need to do it before.

for (int a = 0, b = 0, c = 5; a < c; ++a, b += 2)
like image 24
AliciaBytes Avatar answered Oct 05 '22 01:10

AliciaBytes