C Programming - Control Instructions - Discussion

4. 

Point out the error, if any in the while loop.

#include<stdio.h>
int main()
{
    int i=1;
    while()
    {
        printf("%d\n", i++);
        if(i>10)
           break;
    }
    return 0;
}

[A]. There should be a condition in the while loop
[B]. There should be at least a semicolon in the while
[C]. The while loop should be replaced with for loop.
[D]. No error

Answer: Option A

Explanation:

The while() loop must have conditional expression or it shows "Expression syntax" error.

Example: while(i > 10){ ... }


Sourav said: (Aug 26, 2017)  
I think D is the correct answer.

Jack said: (Jan 25, 2018)  
Why does a while loop requires condition whereas for loop doesn't?

Ex: for(;;)
{
printf("%d\n", i++);
if(i>10)
break;
}

Please anyone help me.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.