1. What is the primary purpose of a 'for' loop in C?
Answer:
Explanation:
A 'for' loop is typically used in C to execute a block of code a specific number of times, with the number of iterations usually determined by the loop's initialization and condition.
2. What are the three parts of a 'for' loop declaration in C?
Answer:
Explanation:
A 'for' loop in C consists of three parts: initialization (executed once before the loop starts), condition (checked before each iteration), and update (executed after each iteration).
3. Which part of the 'for' loop is optional?
Answer:
Explanation:
All three parts of the 'for' loop (initialization, condition, update) are optional in C. Omitting the condition creates an infinite loop.
4. How is a 'for' loop commonly used in array processing?
Answer:
Explanation:
'For' loops are commonly used to iterate over each element in an array, typically by using an index variable that is incremented in each iteration.
5. What is the scope of a variable declared in the initialization part of a 'for' loop in C?
Answer:
Explanation:
A variable declared in the initialization part of a 'for' loop is local to the loop and cannot be accessed outside the loop.
6. What will be the output of the following C code snippet?
for (int i = 0; i < 3; i++) {
printf("%d", i);
}
Answer:
Explanation:
The loop iterates three times with 'i' taking values 0, 1, and 2, resulting in the output "012".
7. How do you create an infinite 'for' loop in C?
Answer:
Explanation:
An infinite 'for' loop can be created by leaving the condition empty (for ( ; ; ) { }) or by using a condition that always evaluates to true.
8. What does the 'break' statement do inside a 'for' loop?
Answer:
Explanation:
The 'break' statement is used to exit the loop immediately, regardless of the loop condition.
9. What is the use of the 'continue' statement in a 'for' loop?
Answer:
Explanation:
The 'continue' statement skips the remaining part of the loop body for the current iteration and proceeds with the next iteration of the loop.
10. What will happen if you omit all three parts of the 'for' loop declaration?
Answer:
Explanation:
Omitting all three parts (initialization, condition, and update) of the 'for' loop creates an infinite loop because there is no condition to terminate the loop.
11. Can the initialization part of the 'for' loop contain multiple statements in C?
Answer:
Explanation:
The initialization part of a 'for' loop can contain multiple statements, separated by commas.
12. Is it possible to declare more than one variable in the initialization part of a 'for' loop in C?
Answer:
Explanation:
Multiple variables can be declared in the initialization part of a 'for' loop, but they must all be of the same type.
13. What will be the output of the following C code snippet?
for (int i = 10; i >= 0; i -= 2) {
printf("%d ", i);
}
Answer:
Explanation:
The loop starts at 10 and decrements 'i' by 2 in each iteration, printing the even numbers down to 0.
14. What is the correct way to write a nested 'for' loop in C?
Answer:
Explanation:
Nested 'for' loops are written by placing one 'for' loop inside the body of another, with each loop having its own control variables and conditions.
15. What is the role of the update part of the 'for' loop in C?
Answer:
Explanation:
The update part of the 'for' loop is executed after each iteration of the loop body and is typically used to modify the loop variable(s), such as incrementing or decrementing a counter.