Skip to main content

Loops in the C Language

C Language Beginner level

Loops in the C Language : 

The concept of Loops in the programming languages makes them awesome . A loop can iterate a statement as the number of times we want . Let's suppose I say someone to write a statement thousand times , he/she writes statements one by one thousand time , So it goes very lengthy and heavy process for a human being . But rather than human being A Computer program makes this heavy process very light because we only need to write a loop and the particular statement once and the compiler ( Compiler is a software that compiles C Code line by line to perform actions and showing the output . ) will write that statement thousand times .

There are various kind of loops , we use in C Language are as -

  • For Loop
  • While Loop
  • Do While Loop 
  • Nested Loop 

For Loop :
We mostly use for loop in the programming and can understand the syntax with below C code -

#include <stdio.h>
void main()
{
int i ;
for(i=0 ; i<101 ; i++)
{
printf(" Hello World \n ") ;
}

return 0 ;


Note :   

  • Here we declared a counter i to count the statement 100 time . Obiviously this counter i is of Integer Data Type . 
  • Inside the for loop we initiated the first value of counter as i=0 and we are running this loop till the condition i < 101 and i++ increases i any time till the condition i < 101 is true . 
  • we wrote the Hello World inside the printf() function in between the for loop brackets . Thus this For Loop prints the Hello World 100 times by writing the code of some lines . 
  • \n stands for new line , In each iteration " Hello World " prints in the new line on the screen .
While Loop : 

The syntex of the while loop to print the " Hello World " 100 times is as - 


#include <stdio.h>
void main()
{
int i ;
while(i<101)
{
printf( " Hello World " ) ;
}

return 0 ;

Note :

  • While loop executes till the condition i < 101 is true . 

Do While Loop :

The Above programe of printing Hello World 100 times using do-while loop can be coded as -

#include <stdio.h>
void main()
{
int i ;
do{
printf( " Hello World " )  ;

while(i<101) ;

return 0 ;
}

Note :

  • In do-while loop the printf() executes one time definitely and check the condition in while block . 

Let's see about Strings implementation in C Codes in the next page . 


                                                 << Back            >>Next

Comments