Q:

Explain each of the following loops. Correct any problems you detect

0

(a)

 for (int ix = 0; ix != sz; ++ix) { /* . . . */ }
if (ix != sz)
 // . . .

(b)

int ix;
for (ix != sz; ++ix) { /* . . . */ }

(c)

for (int ix = 0; ix != sz; ++ix, ++ sz) { /* . . . */ }

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

(a)

    int ix;
    for (ix = 0; ix != sz; ++ix) { /* ... */ }
    //for (int ix = 0; ix != sz; ++ix) { /* ... */ }
    // The loop variable is used outside `for` scope, thus should be defined outside.
    if (ix != sz)
    // ...

(b)

    int ix;
    //for (ix != sz; ++ix) { /* ... */ }
    // When the initialization is unnecessary, a null statement should be used.
    for (; ix != sz; ++ix) { /* ... */ }

(c)

    for (int ix = 0; ix != sz; ++ix, ++ sz) { /* ... */ }
    // The loop will never end.
    for (int ix = 0; ix != sz; ++ix) { /* ... */ }

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now