Choose a correct C Statement regarding for loop.for(; ;);a)for loop wo...
We are not specifying condition to exit the loop. Eg. for(a=0;a<10;a++)
Choose a correct C Statement regarding for loop.for(; ;);a)for loop wo...
Explanation:
The for loop syntax
The for loop is a control flow statement that allows us to repeatedly execute a block of code based on a specified condition. It consists of three parts:
1. Initialization: This part is executed only once at the beginning of the loop. It is used to initialize the loop variable.
2. Condition: This part is evaluated before each iteration of the loop. If the condition is true, the loop continues; otherwise, the loop is terminated.
3. Increment/Decrement: This part is executed after each iteration of the loop. It is used to update the loop variable.
The syntax of the for loop is as follows:
for (initialization; condition; increment/decrement) {
// code to be executed
}
The given statement:
for (; ;) {
}
Explanation:
In the given statement, all three parts of the for loop are empty. This means that there is no initialization, no condition, and no increment/decrement.
- Since there is no initialization, the loop variable is not assigned any initial value.
- Since there is no condition, there is nothing to evaluate before each iteration. As a result, the condition is considered to be true.
- Since there is no increment/decrement, the loop variable remains unchanged in each iteration.
As a result, the for loop will continue to execute infinitely without any termination condition. This is known as an infinite loop.
Correct Answer:
The correct answer is option B) for loop works infinite number of times.