‘for’ Loop, five important way to use in C language.

Simply you can use in possible five way.

Here five possible way are describe in easy ways. By this you can start loop with any way depend on your interests and time consuming. So, try and use.

What do you mean by ‘for’ loop?

A ‘for’ loop is a repetition control structure which allows us to write a loop that is executed a specific number of times. The loop enables us to perform n number of steps together in one line. In for loop, a loop variable is used to control the loop. Which are understand by conditional operator;

There are five example..

1. It’s simple loop in this programme. Initilization, condition & increment or decrement in single line..

  • #include<stdio.h>
  • #include<conio.h>
  • void main()
  • {
  • int i;
  • for( i=1;i<=10;i++)
  • {
  • printf(“%d”,i);
  • }
  • getch();
  • }

Results.

2. In this programme, there are two initilization, two conditional & two increment or decrement in single loop line in ‘for’ loop. In single loop line initilization of i & j, condition of i & j, increment or decrement of i & j are done..

  • #include<stdio.h>
  • #include<conio.h>
  • void main()
  • {
  • int i, j;
  • for( i=1, j=2;i<=10, j<=10;i++, j++)
  • {
  • printf(“%d”,i*j);
  • }
  • getch();
  • }

Results.

3. In this programme, initilization done first with counter container but condition done in ‘for’ loop line and increment & decrement in print.

  • #include<stdio.h>
  • #include<conio.h>
  • void main()
  • {
  • int i=1;
  • for( ;i<=10;)
  • {
  • printf(“%d”,i++);
  • }
  • getch();
  • }

Results.

4. In this programme, initilization done first with counter container, condition done in ‘for’ loop line and increment & decrement also..

  • #include<stdio.h>
  • #include<conio.h>
  • void main()
  • {
  • for(int i=1 ;i<=10;i++)
  • {
  • printf(“%d”,i);
  • }
  • getch();
  • }

Results.

5. In this programme, initilization done first with counter container but condition done in ‘for’ loop line and increment & decrement also..

  • #include<stdio.h>
  • #include<conio.h>
  • void main()
  • {
  • int i=1;
  • for( ;i<=10;i++)
  • {
  • printf(“%d\n”,i);
  • }
  • getch();
  • }

Results.

Design a site like this with WordPress.com
Get started