C Programming - Control Instructions - Discussion

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.

[A]. 1
[B]. 1, 2
[C]. 2, 3
[D]. 2, 3, 4

Answer: Option D

Explanation:

No answer description available for this question.

Basanta said: (Aug 6, 2010)  
To execute a group of statement repeatedly we use Loops(may be any kinds of loop).C provides three types of Loop(for, while, do..while) for use in difference places according to situation, that means all things that can be done using a for loop can also be done using a while loop. If you failed give the Question in my mail (basanta.mca[at]gmail.com) I will solve it.

Naive_coder said: (Aug 7, 2010)  
For loop is ultimately broken down to same machine code as a while loop doing same function. So their speed might be same.

Deepa said: (Apr 10, 2011)  
Why statement 4 is true ?

Sundar said: (Sep 1, 2011)  
@Deepa

The following is the valid for loop.

int i=0;

for(;;)
{
printf("%d ", i++);

if(i == 10) break; // condition checking is done here.
}

Thiyagarajan said: (Sep 6, 2011)  
"for loop can be used if we want statements in a loop get executed at least once. ".

How? Can you tell that the above statement is write please explain.

Sundar said: (Sep 7, 2011)  
@Thiyagarajan

Yes. The 'for-loop' can be used if we want statements in a loop get executed at least once.


/* The following is the code that you have requested. */

/* ----- Usually we write code with do-while loop ----- */

int num = 0;

do
{
printf("Enter a number : ");
scanf("%d", &num);

}while(num != 0);


/* ----- Same logic with for loop ----- */

int num = 0;

for(;;)
{
printf("Enter a number : ");
scanf("%d", &num);

if(num == 0) break;
}

I hope this will help you. Have a nice day!

Srinivas16 said: (Sep 9, 2011)  
for loop is not end with semicolon (;)

for(i=0;i<n;i++)
{

}

Then why statement 3 is correct ?

Sarita said: (Sep 14, 2011)  
@Srinivas

See its not compulsary that for loop should have body for execution...so we can write for(i=0;i<n;i++); i.e for without body... but as here our for is 'for(;;)' it go infinite
so statement three is correct..

I hope you understood what i mean to say...

Ranganath M said: (Oct 28, 2011)  
Here I can say that 4th statement is wrong, because it is not sure that a for loop can atleast be executed one time.

Have a look at the following :

int main()
{
int i;
for(i=0;i>5;i++) //at first time only condition fails
printf("hi");
}

Ranganath M said: (Oct 28, 2011)  
For 4th statement do_while loop only correct.

Khush said: (Nov 4, 2011)  
@Ranganath : its mentioned tht "for loop CAN be used..."

Abc said: (Nov 17, 2011)  
main ()
{
int i=2;
printf("%d ", i);

for(;i<=1;)
{
printf("%d ", i++);
}

printf("%d ", i);

}

Girish Nischel said: (Nov 27, 2011)  
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: (Dec 2, 2011)  
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: (Dec 5, 2011)  
@ 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: (Dec 24, 2011)  
Option C would be the answer. There is a conflict between do-while & for. Reported.

Vanchi said: (Feb 8, 2012)  
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: (Jun 9, 2012)  
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: (Aug 3, 2012)  
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: (Aug 24, 2012)  
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: (Sep 4, 2012)  
do while is used to print statements at least once.

Is, for loop is used to print statements at least once.

Piyush Jain said: (Oct 15, 2012)  
Option 4 is correct.

Watch carefully that its written "can be used" in the question.

Vijaykrsihnakasina said: (Jul 1, 2013)  
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);
}

Jot said: (Jul 10, 2013)  
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: (Oct 7, 2013)  
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.

Arpit Gupta said: (Nov 15, 2013)  
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: (Nov 21, 2013)  
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.

Vinod said: (Jan 31, 2014)  
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: (Mar 16, 2014)  
Can anyone tell me how while is faster than for loop?

Husain Barodawala said: (Jun 5, 2014)  
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? )

Pallavi said: (Sep 1, 2014)  
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: (Sep 7, 2014)  
How is for loop slower than while loop?

Pradnya Solke said: (Jul 10, 2015)  
Only do...while loop can be used to execute statement atleast once.

Niraj said: (Aug 11, 2015)  
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: (Oct 7, 2015)  
@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.

Sandhya said: (Nov 27, 2015)  
For(;;); here is semicolon is present after for then how it became a infinite loop.

Gautam said: (Jun 16, 2016)  
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?

Pawan Kumar Saini said: (Oct 16, 2016)  
I think statement 3 is right.

int i=5;
for(;i<4;i++)
{
printf("%d",i);
}

It will not print at least one time.

Sai Lakshmi said: (Jan 3, 2017)  
How for loop is used to execute statements atleast once?

Abhilash said: (Mar 17, 2017)  
How come statement 3 be true?

We cannot put a semicolon after for bracket.

Mrittunjoy Das said: (Jun 27, 2017)  
Here, for loop can be used if we want statements in a loop get executed at least once.

I think this is not a valid because you are justifying it based on a single case. It is not 'for loops' default behavior to execute at least once. It's default behavior of do while.

Raj said: (Feb 15, 2018)  
This statement "for loop can be used if we want statements in a loop get executed at least once" is True or False.

Please anyone give answer.

Nidhi said: (Jun 6, 2019)  
Statement 4 is wrong. "do while" loop is used for at least one execution not for loop.

Sathiya Baskar said: (Jul 18, 2019)  
for(;;);
will not execute..

if for(;;)--->> it will execute infinite times.

Am I right? Please tell me.

Afsha said: (Dec 23, 2020)  
How option 4 is right?

Can anyone explain it?

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.