Loops in C language #1

Loops 
 
          In looping, sequence of statement are executed until some conditions for the termination of the loop are satisfied. A program loop therefore consist of two segments, one known as the body of the loop and other known as the control statement.


In C there are 4 loops as follow:
  1. for loop
  2. while loop
  3. do while loop
  1. for loop
syntax of for loop

for(initialization; condition; increment/decrement)
   {
     statement;
      ...............
      ...............
      ...............
   }
     
Example of for loop.
#include<stdio.h>#include<conio.h>
int main()
{
   int a=10, i;
   clrscr();
   for(i=1; i<=a; i++)
   {
     printf("%d\n",a);
   }
   return0;
}

Ouput:-
1
2
3
4
5
6
7
8
9
10

Explanation:- 

     for(i=1; i<=a; i++) in this for loop i=1 is initial value of i, i<=a means value of i is from 1 to 10 because value of a is 10, and i++ is increase value by 1.

same program using decrement sign 
   
#include<stdio.h>
#include<conio.h>
int main()
{
   int a=10, i;
   clrscr();
   for(i=a; i>=1; i--)
   {
     printf("%d\n",a);
   }
   return0;
}

Output:-
10
9
8
7
6
5
4
3
2
1
Explanation:-
i-- is decrease the value of i by 1.

If any doubt or query comment below......
In next blog we will learn further loops. 

Key Note:- when we use int main as main function then instead of getch() use return0; both have same function.

No comments

Powered by Blogger.