Find the errors in each of the following code segments and explain how to correct them. a) unsigned int x{1}; while (x <= 10); { ++x; }
时间: 2024-02-14 08:04:04 浏览: 113
深入解析unsigned int 和 int
The error in this code segment is the presence of a semicolon (;) after the while loop condition. This causes the while loop to become an infinite loop, as the body of the loop is never executed.
To correct this error, remove the semicolon after the while loop condition:
```
unsigned int x{1};
while (x <= 10)
{
++x;
}
```
In this corrected code, the while loop will execute the body of the loop (incrementing `x`) until the condition `x <= 10` is no longer true.
阅读全文