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

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);

}

Girish nischel said:   1 decade ago
As per statement 3, i am convinced with the several explanations above! but when v are writing for(;;);...this means v r executing nothing infinetly! so in what case can the above statement is useful as an infinite loop??

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.

Vanchi said:   1 decade ago
For the 4th statement do- while only correct because the other two are entry control loop statements. So it executes the condition at the end when the statement in the loop is get executed at least once.

Kuber said:   1 decade ago
4th statement is wrong.
@Sundar:
you putted that condition inside loop. we can do same thing with while loop also. So i dont think statement 4 is true, its wrong.

Indrasen said:   1 decade ago
for (i = 0; i < 5; i++);
print(i);
Because the first semicolon ends the loop with an empty statement, the loop doesn't actually do anything. The print() function will be printed only once because it's actually outside the for loop entirely.

Chaitu said:   1 decade ago
The fourth statement is about "can we use it r not"..it not asking about its default property,we can use for loop to execute a statement with out conditional check..as said by some of you above..so i think it can be used...(but here its OK ..i can tell it all depends on mind ..of key setter of a exam.(if it appears..:)) how he thinks...)


Post your comments here:

Your comments will be displayed after verification.