while() and do…while() statement in C

Syntax :

while(Condition)
{
        Statement 1;
        Statement 2;
        ...
}

 

  • Note that if we will not put condition in ‘while’ statement then it generates Syntactical Error.

For Example :

 

main()
{
    int i,num;
    printf("Enter the Number : ");
    scanf("%d", &i);
    i = 1;
    while(i <= 10)
    {
          printf("%d * %d = %d\n",num,i,num*i);
          i++;
    }
}

 

Answer: ?? (Try doing it yourself.)

Example 2 :

main()
{
    int i;
    printf("Hello..");
    i = 0;
    while(i <= 4)
    {
          printf("hai ...i = %d \n",i);
          i++;
    }
    printf("Bye... i = %d\n",i);
}

 

Answer: Try!😊

Hello..
Hai ...i = 0
Hai ...i = 1
Hai ...i = 2
Hai ...i = 3
Hai ...i = 4
Bye... i = 5

 

do … while():

Syntax :

do {

statements;

....

}while(condition);
  • While() loop is called entry controlled loop.
  • Do…while() loop is called exit controlled loop.
  • Do…while() loop executes minimum one time whereas while() executes minimum zero time.

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Powered by WordPress.com.

Up ↑

%d bloggers like this: