Computer Science, asked by Daddykokkie, 2 months ago

How many times will this loop execute? Explain your answer.

unsigned char half_limit = 150;

for (unsigned char i = 0; i < 2 * half_limit; ++i)
{
// do something;
}​

Answers

Answered by VampireKiss
34

It is impossible to determine from the information given.

In C or C++, the loop has undefined behavior unless at least one of the following is true:

j is an unsigned integer type.

j is a floating point type and the loop has side effects or I/O.

something sets j to a value larger than 10 across a loop iteration boundary, before undefined behavior occurs due to j = j - 1.

A straightforward approach would use a statement inside the loop such as j = 42.

A baroque approach might declare j as sig_atomic_t, make it visible to a signal handler, and set j from there. That said, the data race inherent in j = j - 1 means you still need some other synchronization.

something triggers an exit from the loop before undefined behavior occurs.

Obvious examples include break and return, and maybe throw. (throw is C++ only)

Less common examples terminate the program, including calls to exit() or abort(), or by failing an assert().

A more baroque approach might involve exec() or longjmp().

the loop body contains a nested loop (either directly or indirectly), where the inner loop runs forever, but that loop does have side effects or I/O.

Example: for (j = 1; j <= 10; j = j - 1) while (true) printf(“hello!\n”);

C++ only: j has a class type, and it overloads operators in such a way that avoids undefined behavior. (Thanks, David Vandevoorde!)

Answered by Vini2345
0

Answer:

Hey Anushka!!! Do u remember me !!!!!???

Similar questions