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

Time, Speed and Distance

Time, Speed And Distance  In this post, We will learn about time, speed and distance. Important Formula Distance = Time x Speed Time = Distance/Speed Speed = Distance/time Important Units Distance: meter (m), kilometre (km) Time: Hour (h), second (s) speed: meter/second (m/s), kilometre/hour  (km/h) Important Conversion 1 km = 1000 meter 1 h = 3600 s 1 km/h = (5/18) m/s 1 m/s = (18/5) km/h Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 

Joint Entrance Exam (JEE): Probability Syllabus

JEE: Probability Syllabus Probability  is a very interesting and different topic of mathematics which doesn't require your previous knowledge of any other chapters other than permutation and combination.  Following topics are the part of Jee syllabus Addition and multiplication rules of probability Addition and multiplication rules of probability Conditional Probability Bayes Theorem Independence of events Computation of probability of events using permutations and combinations Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Haryana Public Service Commission: HPSC 2018

Haryana Public Service Commission: HPSC 2018 Company Name         :  Haryana Public Service Commission: HPSC 2018 Recruitment Post Name                       :   Civil Judge State                            :  Haryana Salary                           :  Rs   27,700-44,770 Number of Vacancy   :  107 Eligibility                     :  Bachelor's Degree in Law Last Date to Apply     :  30-09-2018 Application Details    :  Notification Apply Online              :  Apply Here Government Jobs In  Haryana Please write comments if you f...

Securities and Exchange Board of India Recruitment 2018

Securities and Exchange Board of India  Company Name SEBI Post Name Assistant Manager State India Salary RS 28150-55600 Number of Vacancy 120 Eligibility Graduation Last Date 07-10-2018 Application Details Job Notification Apply Online Job Application Securities and Exchange Board of India (SEBI) Recruitment 2018    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.