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.
1
1, 2
2, 3
2, 3, 4
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
48 comments Page 1 of 5.

Hardik vachhani said:   2 years ago
"For loop works faster than a while loop." - This statement is generally not true. The for loop and the while loop have similar performance characteristics. The choice between them usually depends on the specific requirements of the loop, not on their relative speed.

"All things that can be done using a for loop can also be done using a while loop." - This statement is true. The for loop and the while loop are equivalent in terms of expressiveness; you can accomplish the same tasks with either loop structure.

"for(;;); implements an infinite loop." - This statement is true. The syntax for(;;); is a shorthand way of creating an infinite loop in C. It is equivalent to while(1); or for(;;) { }.

"For loop can be used if we want statements in a loop to get executed at least once." This statement is true. The for loop, like the while loop, allows you to create loops where the condition is checked after the loop body has been executed. You can achieve this by initializing the loop control variable appropriately.

Therefore, statements 2, 3, and 4 are correct. Statement 1 is generally not true, as the performance of a for loop and a while loop is usually comparable.

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.

Veena said:   2 years ago
I think option 2, 3 is correct.

Why not 1,4->?

1. This statement is not necessarily correct. The performance of a "for" loop compared to a "while" loop depends on the specific code and compiler optimizations. The choice between "for" and "while" loops should be based on the readability and logical structure of the code, not just performance.

4. This statement is not correct. "for" loops in C are not typically used if you want to ensure that the loop body is executed at least once. You would typically use a "while" loop or a "do-while" loop for this purpose.
(4)

Sundar said:   1 decade ago
@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!

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? )

Basanta said:   2 decades ago
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.

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...)

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.

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.

Ranganath M said:   1 decade ago
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");
}


Post your comments here:

Your comments will be displayed after verification.