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.
1
1, 2
2, 3
2, 3, 4
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
48 comments Page 1 of 5.

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.
(4)

Afsha said:   5 years ago
How option 4 is right?

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.
(4)

Nidhi said:   6 years ago
Statement 4 is wrong. "do while" loop is used for at least one execution not for loop.
(4)

Gautam said:   9 years ago
4th option is incorrect as:

for(i=5;i<=2;i++)
printf("%d",i+1);

This won't execute even once.

Anyone tell me, if the above program is false?

Arpit gupta said:   1 decade ago
Statement 4 is true in case of do while loop not in case of for loop. Let assume if condition is false initially then for loop never executed. So 4th statement is wrong for for loop.

Hari Kishore said:   1 decade ago
Check the program below:

#include<stdio.h>
int main()
{
int i=0;
for(printf("hi");i>5;i++);
return 0;
}

Statement printf("hi"); which is inside for loop is getting executed even condition is wrong. This cannot be done in while.

So I think 4th option is also correct.

Vinod said:   1 decade ago
How the 4th statement is true? oly in do while the statement will get execute at least once. In for loop if the condition fails during 1st iteration itself then the statement will not get executed isn't it?

Akshay Kalra said:   1 decade ago
Can anyone tell me how while is faster than for loop?

Husain Barodawala said:   1 decade ago
Why Statement 4 is true.

For Cannot be executed "at-least once", We can terminate it in the first Conditional comparison.

Example:

for(i=1; i>5 ; i++) {"This will not be executed"}.

And If You are genius enough to make it exec atleast once than I may say that -

It is also Possible for "do-while" NOT to execute even atleast for once.

Example:

do{
break; //below lines will not be executed even for a single run.
}
(I know this is stupidity, but what about other programs given by others? )


Post your comments here:

Your comments will be displayed after verification.