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 2 of 5.

Sarita said:   1 decade ago
@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...

Mrittunjoy Das said:   8 years ago
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.

Veena said:   5 days ago
@All.

Here's my code.

#include <iostream>

int main()
{
The condition 'i < 0' is initially false
for (int i = 0; i < 0; i++)
{
std::cout << "This will not be printed." << std::endl;
}
return 0;
}

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);
}

Indrasen said:   1 decade ago
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.

Girish nischel said:   1 decade ago
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??

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.

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?

Vanchi said:   1 decade ago
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.

Yash said:   1 decade ago
@ 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.


Post your comments here:

Your comments will be displayed after verification.