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 5 of 5.
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.
@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.
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...)
Krishna said:
1 decade ago
do while is used to print statements at least once.
Is, for loop is used to print statements at least once.
Is, for loop is used to print statements at least once.
Piyush jain said:
1 decade ago
Option 4 is correct.
Watch carefully that its written "can be used" in the question.
Watch carefully that its written "can be used" in the question.
Vijaykrsihnakasina said:
1 decade ago
You can also do this.
for(;(statement);)
{
(---------)
}
For eg.
int main()
{
int i=5;
for(;i--;)
{
printf("%d ",i);
}
}
Or you can also do this
int main()
{
int i=2;
for(i--;i;)
printf("%d",i);
}
for(;(statement);)
{
(---------)
}
For eg.
int main()
{
int i=5;
for(;i--;)
{
printf("%d ",i);
}
}
Or you can also do this
int main()
{
int i=2;
for(i--;i;)
printf("%d",i);
}
Jot said:
1 decade ago
If you want the statements inside the loop must execute at least once then you need do-while loop. I disagree somehow on the truth of Statement 4.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers