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 2 of 5.
Pallavi said:
1 decade ago
The 4th statement is "CAN BE USED" which suggests in some cases not always. If the statement was always be used for executing the body at least once. Then the answer would be "do...while".
Akash said:
1 decade ago
How is for loop slower than while loop?
Pradnya solke said:
1 decade ago
Only do...while loop can be used to execute statement atleast once.
Niraj said:
1 decade ago
If we want to execute a statement at least once, whatever the condition is true or false in this case we should use do-while loop instead of for loop. In do-while loop first statement is executed then condition is checked. While in FOR loop first condition is checked then statement is executed.
Harish said:
10 years ago
@Akash.
Both while and for loop are looks some what same and only difference is for loop looks simple than while, because all expressions are stated in a single for loop, where user can understand easily by looking for.
Both while and for loop are looks some what same and only difference is for loop looks simple than while, because all expressions are stated in a single for loop, where user can understand easily by looking for.
Sandhya said:
10 years ago
For(;;); here is semicolon is present after for then how it became a infinite loop.
Sushant said:
1 decade ago
I am not agree with 4th statement. Who is agree with 4th statement, please explain how to do following program using for loop & having same answer as after this program execution.
void main()
{
int i=10;
do
{
i++;
printf("i=%d",i);
}while(i<10);
}
This program is gives you answer 11;
Note: If you think about for(;;) statement than we get same answer with while(1) statement also. i.e. I want to say is for & wile are entry controlled block & do....while is exit controlled block.....any one having different suggestion please reply to this post.
void main()
{
int i=10;
do
{
i++;
printf("i=%d",i);
}while(i<10);
}
This program is gives you answer 11;
Note: If you think about for(;;) statement than we get same answer with while(1) statement also. i.e. I want to say is for & wile are entry controlled block & do....while is exit controlled block.....any one having different suggestion please reply to this post.
Pawan kumar saini said:
9 years ago
I think statement 3 is right.
int i=5;
for(;i<4;i++)
{
printf("%d",i);
}
It will not print at least one time.
int i=5;
for(;i<4;i++)
{
printf("%d",i);
}
It will not print at least one time.
Sai lakshmi said:
9 years ago
How for loop is used to execute statements atleast once?
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers