Home Show/Hide Menu
C Tutorials
➜ C Language
  - Features of C
  - First C Program
  - Compilation in C
  - C Character Set
  - Tokens
  - Keywords
  - Identifiers
  - Constants
  - Operators
  - Data Types
  - Variables
  - Type Casting
  - Comments
  - Input/Output
  - Escape Sequence
  - Programming Errors
➜ Control Statements
  - if Statement
  - if-else Statement
  - Nested if-else
  - else-if
  - switch Statement
  - Loop
  - while loop
  - do while loop
  - for loop
  - break
  - continue & goto
  - Nested Loop
  - Infinite Loop
➜ Functions
  - What is function
  - Call by Value & Reference
  - Recursive function
  - Storage Classes
➜ Array
  - 1-D Array
  - 2-D Array
  - Return an Array
  - Array to Function
➜ C Pointers
  - Pointers
  - Pointer to Pointer
  - Pointer Arithmetic
  - Dangling Pointers
  - sizeof() operator
  - const Pointer
  - void pointer
  - Dereference Pointer
  - Null Pointer
  - Function Pointer
  - Function pointer as argument
➜ Memory Management
  - Memory Layout of C
  - Dynamic memory
  - calloc()
  - malloc()
  - realloc()
  - free()
➜ Strings
  - gets() & puts()
  - String Functions
  - strlen()
  - strcpy()
  - strcat()
  - strcmp()
  - strrev()
  - strlwr()
  - strupr()
  - strstr()
➜ C Math
  - Math Functions
➜ Enum, Structure & Union
  - Enum
  - Structure
  - typedef
  - Array of Structures
  - Nested Structure
  - Structure Padding
  - Union
➜ File Handling
  - fprintf() fscanf()
  - fputc() fgetc()
  - fputs() fgets()
  - fseek()
  - EOF and feof()
  - fsetpos()
  - tmpfile()
  - rewind()
  - ftell()
  - lseek()
➜ Preprocessor
  - Preprocessor
  - Macros
  - #include
  - #define
  - #undef
  - #ifdef
  - #ifndef
  - #if
  - #else
  - #error
  - #pragma
➜ Command Line
  - Arguments

for loop in C Language

In C programming language, for loops are the most complex loops. The for loop is similar to while loop and is used when programmer knows in advance how many times the script should run.
The syntax of a for loop is:

for (initialize counter; test condition; increment/decrement counter) {
       //Statement(s) to be executed repeatedly
}

Parameters:
initialize counter: Initialize the loop counter value once unconditionally at the beginning of the loop.
test condition: In the beginning of each iteration, condition is evaluated. If it evaluates to true, the loop continues and the nested statement(s) are executed. If it evaluates to false, the execution of the loop ends.
increment/decrement counter: At the end of each iteration, increases/decreases the loop counter value

// Program to Print Multiples of a Given Number
#include<stdio.h>
#include<conio.h>
void main()
{
   int i,n,sum=0;

   printf("Input number of terms : ");
   scanf("%d",&n);
   printf("\nThe odd numbers are :");
   for(i=1;i<=n;i++)
   {
     printf("%d ",2*i-1);
     sum+=2*i-1;
   }
   printf("\nThe Sum of odd Natural Number upto %d terms : %d \n",n,sum);
   getch();
}

Output :

Input number of terms : 10                                                                                   
The odd numbers are :1 3 5 7 9 11 13 15 17 19                                                                
The Sum of odd Natural Number upto 10 terms : 100 

Flow Chart of for Loop

ADVERTISEMENT