C Programming - Control Instructions - Discussion
Discussion Forum : Control Instructions - Point Out Correct Statements (Q.No. 7)
7.
Which of the following sentences are correct about a for loop in a C program?
1: | for loop works faster than a while loop. |
2: | All things that can be done using a for loop can also be done using a while loop. |
3: | for(;;); implements an infinite loop. |
4: | for loop can be used if we want statements in a loop get executed at least once. |
Discussion:
48 comments Page 1 of 5.
Veena said:
3 days ago
@All.
Here's my code.
#include <iostream>
int main()
{
The condition 'i < 0' is initially false
for (int i = 0; i < 0; i++)
{
std::cout << "This will not be printed." << std::endl;
}
return 0;
}
Here's my code.
#include <iostream>
int main()
{
The condition 'i < 0' is initially false
for (int i = 0; i < 0; i++)
{
std::cout << "This will not be printed." << std::endl;
}
return 0;
}
Hardik vachhani said:
2 years ago
"For loop works faster than a while loop." - This statement is generally not true. The for loop and the while loop have similar performance characteristics. The choice between them usually depends on the specific requirements of the loop, not on their relative speed.
"All things that can be done using a for loop can also be done using a while loop." - This statement is true. The for loop and the while loop are equivalent in terms of expressiveness; you can accomplish the same tasks with either loop structure.
"for(;;); implements an infinite loop." - This statement is true. The syntax for(;;); is a shorthand way of creating an infinite loop in C. It is equivalent to while(1); or for(;;) { }.
"For loop can be used if we want statements in a loop to get executed at least once." This statement is true. The for loop, like the while loop, allows you to create loops where the condition is checked after the loop body has been executed. You can achieve this by initializing the loop control variable appropriately.
Therefore, statements 2, 3, and 4 are correct. Statement 1 is generally not true, as the performance of a for loop and a while loop is usually comparable.
"All things that can be done using a for loop can also be done using a while loop." - This statement is true. The for loop and the while loop are equivalent in terms of expressiveness; you can accomplish the same tasks with either loop structure.
"for(;;); implements an infinite loop." - This statement is true. The syntax for(;;); is a shorthand way of creating an infinite loop in C. It is equivalent to while(1); or for(;;) { }.
"For loop can be used if we want statements in a loop to get executed at least once." This statement is true. The for loop, like the while loop, allows you to create loops where the condition is checked after the loop body has been executed. You can achieve this by initializing the loop control variable appropriately.
Therefore, statements 2, 3, and 4 are correct. Statement 1 is generally not true, as the performance of a for loop and a while loop is usually comparable.
Veena said:
2 years ago
I think option 2, 3 is correct.
Why not 1,4->?
1. This statement is not necessarily correct. The performance of a "for" loop compared to a "while" loop depends on the specific code and compiler optimizations. The choice between "for" and "while" loops should be based on the readability and logical structure of the code, not just performance.
4. This statement is not correct. "for" loops in C are not typically used if you want to ensure that the loop body is executed at least once. You would typically use a "while" loop or a "do-while" loop for this purpose.
Why not 1,4->?
1. This statement is not necessarily correct. The performance of a "for" loop compared to a "while" loop depends on the specific code and compiler optimizations. The choice between "for" and "while" loops should be based on the readability and logical structure of the code, not just performance.
4. This statement is not correct. "for" loops in C are not typically used if you want to ensure that the loop body is executed at least once. You would typically use a "while" loop or a "do-while" loop for this purpose.
(4)
Afsha said:
5 years ago
How option 4 is right?
Can anyone explain it?
Can anyone explain it?
(4)
Sathiya Baskar said:
6 years ago
for(;;);
will not execute..
if for(;;)--->> it will execute infinite times.
Am I right? Please tell me.
will not execute..
if for(;;)--->> it will execute infinite times.
Am I right? Please tell me.
(4)
Nidhi said:
6 years ago
Statement 4 is wrong. "do while" loop is used for at least one execution not for loop.
(4)
Raj said:
7 years ago
This statement "for loop can be used if we want statements in a loop get executed at least once" is True or False.
Please anyone give answer.
Please anyone give answer.
Mrittunjoy Das said:
8 years ago
Here, for loop can be used if we want statements in a loop get executed at least once.
I think this is not a valid because you are justifying it based on a single case. It is not 'for loops' default behavior to execute at least once. It's default behavior of do while.
I think this is not a valid because you are justifying it based on a single case. It is not 'for loops' default behavior to execute at least once. It's default behavior of do while.
Abhilash said:
8 years ago
How come statement 3 be true?
We cannot put a semicolon after for bracket.
We cannot put a semicolon after for bracket.
Sai lakshmi said:
9 years ago
How for loop is used to execute statements atleast once?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers