Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative to writing the same code before the loop

Tags:

arrays

c

I want to make the for loop execute the code in the beginning. However, my beginning state is same as my ending state. Is there any way I can execute the code without writing it before the loop?

for(int i = queue1->front; 
    i%queue1->size != queue1->front; 
    i++) {
    //some code
}

In the code, I am using a circular array and I want to print that using a loop.

like image 258
A. Eser Avatar asked Aug 26 '26 17:08

A. Eser


1 Answers

Aside from using a do-while-loop you could use a flag to enter the loop in any case like this:

int first = 1;
for (int i = queue1->front; i%queue1->size != queue1->front || first; i++){
    first = 0;
    //some code
}

You can even integrate it directly into the for loop header, if you like:

for (int i = queue1->front, f=1; i%queue1->size != queue1->front || f; i++, f=0) {
like image 92
Ctx Avatar answered Aug 28 '26 07:08

Ctx



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!