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.
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.
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?
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?
Sandhya said:
10 years ago
For(;;); here is semicolon is present after for then how it became a infinite loop.
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.
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.
Pradnya solke said:
1 decade ago
Only do...while loop can be used to execute statement atleast once.
Akash said:
1 decade ago
How is for loop slower than while loop?
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".
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? )
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? )
Akshay Kalra said:
1 decade ago
Can anyone tell me how while is faster than for loop?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers