Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explanation what for(i=0;1;i++) does in C?

what for (i=0;1;i++) exactly does? when the for loop will terminate? after reaching value of i=1? When will that happen? (looked for this type of loop in internet and Book C(how to program, Deitel&Deitel), without any result...)

    int i;
    for (i=0;1;i++)
        {
        if (*Something Happens*)
            break;
        }
like image 822
2Napasa Avatar asked Dec 02 '22 17:12

2Napasa


1 Answers

Since in C an int can be interpreted as a boolean using a zero/non-zero rule (zero means "false", anything else means "true") the loop is going to continue until a break statement is reached inside the loop's body.

You can rewrite the same loop as

for (i=0; ;i++)

because in the absence of a condition in the middle the loop is going to continue until a break as well.

like image 159
Sergey Kalinichenko Avatar answered Dec 11 '22 04:12

Sergey Kalinichenko