Skip to main content

Loop In C Programming Language

Loop In C Programming Language

Introduction

In this post, we will learn about loops in c programming language. In C programming language, loops are used to execute a set of statements repeatedly until a particular condition is satisfied.
Remember in our very first we wrote a program to print "Hello, World". Now suppose we want to repeat this for 1000 times, means we want to print Hello, World 1000 in a program. It is not practically possible to repeat printf("Hello, World"); 1000 times and because here statement print "Hello, World" is repeatable we can use a loop.

Types of Loop in C programming language

There are three types of loop in C programming language.
  • For Loop
  • While Loop
  • Do While Loop

For Loop in C

These are the steps that we follow while using for loop in c
  1. It first evaluates the initialization code.
  2. Then it checks the condition expression.
  3. If it is true, it executes the for-loop body.
  4. Then it evaluates the increment/decrement condition and again follows from step 2.
  5. When the condition expression becomes false, it exits the loop.
Example: Print "Hello, World" 1000 times using for loop.
 #include<stdio.h>  
 void main( )  
 {  
   int i;  
   for(x = 0; i < 1000; i++)  
   {  
     printf("Hello, World\n");  
   }  
 }  

While loop in C

while loop can be addressed as an entry control loop. It is completed in 3 step
  1. Variable initialization.(e.g int i = 0;)
  2. Condition(e.g while(i <= 10))
  3. Variable increment or decrement ( i++ or i-- or i = i + 2 )
Example: Print "Hello, World" 1000 times using While loop.
 #include<stdio.h>  
 int main()  
 {  
     int i = 0;  
      while(i<1000) {  
           printf("Hello, World\n");  
         i = i + 1;  
      }       
 }  

Do While loop in C

In some situations, it is necessary to execute the body of the loop before testing the condition. Such situations can be handled with the help of do-while loop.
  1. Variable initialization
  2. Execute body
  3. check the condition
Example: Print "Hello, World" 1000 times using do While loop.
 #include <stdio.h>  
 void main( )  
 {  
   int i = 0;  
   do {  
           printf("Hello, World\n");  
           x = i + 1;  
      }while(i<1000)  
 }  

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.




Comments

Popular posts from this blog

SIMPLE PYTHON HTTP SERVER

SIMPLE PYTHON HTTP SERVER In many use cases, we come across the situation where we need a simple server to test some command or test some applications. But creating a server programmatically is a time taking process. To solve the problem Python has created SimpleHTTPServer module. In this post, we will learn how to create a simple HTTP server in a single command on Ubuntu.  Create SimpleHTTPServe Using Commandline  python -m SimpleHTTPServer 8001 Above command will create a simple HTTP server for you at port 8001 Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Golang - Run & Compile

GOLANG RUN & COMPILE In the previous post, we have discussed how to install Go on Linux. In this post, We will discuss how to compile and run a Go program. Create folder HelloWorld mkdir HelloWorld cd HelloWorld Create file HelloWorld.go touch HelloWorld.go Copy below code into HelloWorld.go file package main import "fmt" func main() { fmt.Println("Hello, World") } Compile go build Run ./HelloWorld Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

BSNL Recruitment

BSNL Recruitment      Bharat Sanchar Nigam Limited (BSNL)  has released a notification for the post of   Management Trainees . Company Name BSNL Post Name    Management Trainees State India Salary Rs.  24,900-50,500 Number of Vacancy 300 Eligibility B.Tech Last Date 26-01-2019 Application Details Job Notification Apply Online Job Application BSNL Recruitment       Government Jobs in India Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. Rozgardesh ( रोजगार देश )

South East Central Railway: SECR

South East Central Railway: SECR Company Name         :  South East Central Railway: SECR Post Name                       :   Apprentice State                            :  Maharashtra Salary                           :  Number of Vacancy   :  313 Eligibility                     :  10th Pass  Last Date to Apply     :  15-09-2018 Application Details    :  Notification Apply Online              :  Apply Here Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.