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 4 of 5.

Srinivas16 said:   1 decade ago
for loop is not end with semicolon (;)

for(i=0;i<n;i++)
{

}

Then why statement 3 is correct ?

Sarita said:   1 decade ago
@Srinivas

See its not compulsary that for loop should have body for execution...so we can write for(i=0;i<n;i++); i.e for without body... but as here our for is 'for(;;)' it go infinite
so statement three is correct..

I hope you understood what i mean to say...

Ranganath M said:   1 decade ago
Here I can say that 4th statement is wrong, because it is not sure that a for loop can atleast be executed one time.

Have a look at the following :

int main()
{
int i;
for(i=0;i>5;i++) //at first time only condition fails
printf("hi");
}

Ranganath M said:   1 decade ago
For 4th statement do_while loop only correct.

Khush said:   1 decade ago
@Ranganath : its mentioned tht "for loop CAN be used..."

Abc said:   1 decade ago
main ()
{
int i=2;
printf("%d ", i);

for(;i<=1;)
{
printf("%d ", i++);
}

printf("%d ", i);

}

Basanta said:   2 decades ago
To execute a group of statement repeatedly we use Loops(may be any kinds of loop).C provides three types of Loop(for, while, do..while) for use in difference places according to situation, that means all things that can be done using a for loop can also be done using a while loop. If you failed give the Question in my mail (basanta.mca[at]gmail.com) I will solve it.

Khushi said:   1 decade ago
How statement 3 is infinite loop????
for(i=0;i<5;i++);
this will terminate after 5 iterations...
and value of i would be 5 now...

Can anyone explain???

Yash said:   1 decade ago
@ Girish & Khushi

If we write any statement after statement 3 that is not executed because the statement 3 executes infinitely.

This works like "while(1);" in C and while(true); in java.

Jit said:   1 decade ago
Option C would be the answer. There is a conflict between do-while & for. Reported.


Post your comments here:

Your comments will be displayed after verification.