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 3 of 5.
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.
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.
Arpit gupta said:
1 decade ago
Statement 4 is true in case of do while loop not in case of for loop. Let assume if condition is false initially then for loop never executed. So 4th statement is wrong for for loop.
Hari Kishore said:
1 decade ago
Check the program below:
#include<stdio.h>
int main()
{
int i=0;
for(printf("hi");i>5;i++);
return 0;
}
Statement printf("hi"); which is inside for loop is getting executed even condition is wrong. This cannot be done in while.
So I think 4th option is also correct.
#include<stdio.h>
int main()
{
int i=0;
for(printf("hi");i>5;i++);
return 0;
}
Statement printf("hi"); which is inside for loop is getting executed even condition is wrong. This cannot be done in while.
So I think 4th option is also correct.
Vinod said:
1 decade ago
How the 4th statement is true? oly in do while the statement will get execute at least once. In for loop if the condition fails during 1st iteration itself then the statement will not get executed isn't it?
Akshay Kalra said:
1 decade ago
Can anyone tell me how while is faster than for loop?
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? )
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers