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.
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.
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:
7 years ago
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.
Please anyone give answer.
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.
"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.
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;
}
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;
}
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??
Naive_coder said:
2 decades ago
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:
1 decade ago
Why statement 4 is true ?
Sundar said:
1 decade ago
@Deepa
The following is the valid for loop.
int i=0;
for(;;)
{
printf("%d ", i++);
if(i == 10) break; // condition checking is done here.
}
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:
1 decade ago
"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.
How? Can you tell that the above statement is write please explain.
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!
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!
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers