C Loops

Advertisements

Loops in C

Loops are used, when we need to execute one or more statements multiple times until some conditions are satisfied.

Types of Loop

  • while loop
  • do...while loop
  • for loop
  • Nested loop

While Loop

While loop is also called entry control loop because, in while loop, compiler will 1st check the condition, whether it is true or false, if condition is true then execute the statements.

Syntax of While Loop


              initialization;

              while (condition)
              {
                    - - - - - - - - - -
                    - - - - - - - - - -

                     inc/dec;
              }

Example of While Loop


	#include<stdio.h>

       void main()
       {
              int a=1,num;

              printf("Enter any number : ");
              scanf("%d",&num);

              while (a<=num)
               {
                   printf("\nHello...!!");
                   a++;
                }
       }

   Output :

              Enter any number : 5
              Hello...!!
              Hello...!!
              Hello...!!
              Hello...!!
              Hello...!!

Do-While Loop

The do-while loop is also called exit control loop because, in do-while loop, compiler will 1st execute the statements, then check the condition, whether it is true or false.

Syntax of Do-While Loop


              initialization;

              do
              {
                    - - - - - - - - - -
                    - - - - - - - - - -

                     inc/dec;
              } while (condition);

Example of Do-While Loop


	#include<stdio.h>

       void main()
       {
              int a=1,num;

              printf("Enter any number : ");
              scanf("%d",&num);

              do
               {
                   printf("\nHello...!!");
                   a++;
                } while (a<=num);
       }

   Output :

              Enter any number : 5
              Hello...!!
              Hello...!!
              Hello...!!
              Hello...!!
              Hello...!!

Difference b/w while loop and do-while loop

while loopdo-while loop
It is entry control loop.It is exit control loop.
In this loop condition is checked before loop execution.In this loop condition is checked at the end of loop.
It will never execute loop if condition is false.It will executes loop at least once when the initial condition is false.
There is no semicolon at the end of while statementThere is semicolon at the end of while statement.

For Loop

In for loop we put initialization, contidion and increment/decrement all together. Initialization will be done once at the beginning of loop. Then, the condition is checked by the compiler. If the condition is false, for loop is terminated. But, if condition is true then, the statements are executed until condition is false.

Syntax of For Loop


              for (initialization;condition;inc/dec)
              {
                    - - - - - - - - - -
                    - - - - - - - - - -
              }
					

Example of For Loop


	#include<stdio.h>

       void main()
       {
              int a,num;

              printf("Enter any number : ");
              scanf("%d",&num);

              for (a=1;a<=num;a++)
                   printf("\nHello...!!");
       }

   Output :

              Enter any number : 24
              Hello...!!
              Hello...!!
              Hello...!!
              Hello...!!
              Hello...!!

Related topics

Advertisement