EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Loops in C

Home » Software Development » Software Development Tutorials » C Programming Tutorial » Loops in C

Loops-in-C

Introduction to Loops in C

Loops in C programming language is a conditional concept used for consecutively executing a line or a block of code over and over again until it reaches the value desired by the programmer. In C programming, there are three types of loops, namely For Loop, While Loop and Do While Loop. Loops in C can also be combined with other control statements that include Break statement, Goto statement and Control statement. These loops can be used anywhere in the program, either as entry control or exit control units.

Different Types of Loops

There are 3 different types of Loops in C:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

  • While Loop
  • Do While Loop
  • For Loop

While Loop

In this, the condition is evaluated before processing the body of the loop. If the condition is true, then only the body of the loop is executed. Then the control goes back to the beginning after completing the loop once. The statements in the loop will be executed again and if the condition is true and checked, then this process goes on until the condition becomes false. If the condition is false, the control will go out of the loop. After completion of the loop, the control will go to the statement that is immediately after the loop and the body can contain more than one statement. The curly braces are not that important if it contains only one statement. If the condition is not true in while loop, then loop statements won’t get executed.

Syntax:

while (condition) {
statements;
}

Example:

#include<stdio.h>
#include<conio.h>
int main()
{
int num=1;
while(num<=5)
{
printf("%d\n",num);
num++;
}
return 0;
}

Output:

It will print the numbers from 1 to 5 like below.

Loops in C-1

Do While Loop

In this loop, the statements in the loop need to be executed at least once. After that, it checks the condition. If the condition is true, then it will again have executed the loop otherwise it will exit the loop. It is known as an exit controlled loop. It is similar to while loop and in this the condition is always executed after the body of the loop. The while loop is executed only when the condition is true but sometimes the statement needs to be executed at least once so for that do-while loop has to be used. The difference between while and do-while loop is that in while loop while is written in the beginning and in do-while the condition is mentioned at the end and it ends with a semicolon (;).

Syntax:

do {
statements
} while (expression);

Example:

#include<stdio.h>
#include<conio.h>
int main()
{
int num=1;
do
{
printf ("%d\n",2*num);
num++;
}
while(num<=5);
return 0;
}

Output:

The output of the above program is:

Loops in C-2

For Loop

It executes the set of statements until the time a particular condition is accomplished. It is known as the Open-ended loop. In For loop, we can have more than one initialization or increment/decrement, separated using a comma operator and one condition as well. For loop is used to evaluate the initialization part first and then it checks the condition for true or false. If the condition is true, then it executes the set of statements of for loop. After that it evaluates the increment or decrement condition until the condition becomes false it repeats the same steps. It will exit the loop when the condition is false.

Popular Course in this category
C Programming Training (3 Courses, 5 Project)3 Online Courses | 5 Hands-on Projects | 34+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,570 ratings)
Course Price

View Course

Related Courses
C++ Training (4 Courses, 5 Projects, 4 Quizzes)Java Training (40 Courses, 29 Projects, 4 Quizzes)

Syntax:

for (initial value; condition; incrementation or decrementation )
{
statements;
}

Example:

#include<stdio.h>
#include<conio.h>
int main()
{
int number;
for(number=1;number<=5;number++)
{
printf("%d\n",number);
}
return 0;
}

Output:

Loops in C-1

There are nested For loops as well in which there is outer For loop and inner loop. In this nested loop, the inner loop is repeated for the times for a given condition of outer loop iteration.

Syntax:

for(initialization; condition; increment/decrement)
{
for(initialization; condition; increment/decrement)
{
statement ;
}
}

Example:

#include<stdio.h>
#include<conio.h>
void main( )
{
int i, j;
for(i = 1; i < 5; i++)
{
printf("\n");
for(j = i; j > 0; j--)
{
printf("%d", j);
}
}
}

Output:

Loops in C-3

Other Example:

#include <stdio.h>
#include<conio.h>
int main() {
int i, j;
int table = 2;
int max = 5;
for (i = 1; i <= table; i++) {
for (j = 0; j <= max; j++) {
printf("%d x %d = %d\n", i, j, i*j);
}
printf("\n");
}}

Output:

Loops in C-4

Control Statements

There are some loop control statements that needs to be used in loops for different purpose and to achieve the end result. Below are the different statements that are used:

Break statement:

The break statement is used to exit the loop immediately after execution of particular statement for particular condition.

Syntax:

While (Condition)
{ Statement 1; statement 2;
If (Condition)
{ break;}
Statement 3; }

Continue Statement

It generally skips the statements according to the condition. It is used to send the control directly to the condition and to continue the loop process. For particular condition, it skips the current loop or statements and enters into a new loop or condition.

Syntax:

While (Condition)
{ Statement 1; statement 2;
If (Condition)
{ continue;}
Statement 3; }

Goto statement

It is used to transfer the protocol to a labeled statement.

Example:

#include<stdio.h>
#include<conio.h>
int main()
{
int number;
number=0;
repeat:
printf ("%d\n",number);
number++;
if(number<=5)
goto repeat;
return 0;
}

Output:

Loops in C-5

Conclusion – Loops in C

Above are the loops that are defined in the C programming language. To select a particular loop for solving the problem or writing the program, the program has to be very careful with the requirements of the client. The program has to analyze the problem, what type of checks are required like pre and post check. Looping in C or in any programming language is one of the key concepts. There are generally two types that are entry controlled and exit controlled loop. The loops or statement blocks execute a number of times until the condition becomes false. So, it is better to analyze the issue or problem properly and select the loop accordingly for better performance of the program and memory usage.

Recommended Articles

This has been a guide to Loops in C. Here we discuss the introduction along with the different types of loops. You may also have a look at the following articles to learn more –

  1. C++ String Functions
  2. C++ Operators
  3. Continue Statement in C++
  4. Goto Statement in C

C Programming Training (3 Courses, 5 Project)

3 Online Courses

5 Hands-on Projects

34+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
C Programming Tutorial
  • Loops
    • Loops in C
    • For Loop in C
    • While Loop in C
    • Do While Loop in C
    • Nested Loop in C
    • Infinite Loop in C 
  • Basic
    • Introduction to C
    • What is C
    • Career in C Programming
    • Advantages of C
    • How to Install C
    • Best C Compilers
    • Data Types in C
    • Variables in C
    • C Keywords
    • C Command
    • Command Line Arguments in C
    • C Literals
    • Constants in C
    • Unsigned Int in C
    • String in C
  • Pointers
    • Pointers in C
    • Null pointer in C
    • Function Pointer in C
    • Double Pointer in C
    • Void Pointer in C
    • Const Pointer in C
    • Dangling Pointers in C
    • Pointer Arithmetic in C
  • Operators
    • C Operators
    • Arithmetic Operators in C
    • Relational Operators in C
    • Assignment Operators in C
    • Logical Operators in C
    • Conditional Operator in C
    • Modulus Operator in C
    • Ternary Operator in C
    • Address Operator in C
    • Unary Operator in C
    • Operators Precedence in C
    • Left Shift Operator in C
  • Control Statement
    • Control Statements in C
    • If Statement in C
    • If-else Statement in C
    • Else if Statement in C
    • Nested if Statement in C
    • #else in C
    • Structure Padding in C
    • Nested Structure in C
    • Continue Statement in C
    • Break Statement in C
    • Switch Statement in C
    • Goto Statement in C
  • Function
    • Math Functions in C
    • Hashing Function in C
    • Recursive Function in C
    • Power Function in C
    • fputs in C
    • C puts() Function
    • fprintf() in C
    • fseek() in C
    • Stderr in C
    • ASCII Value in C
    • strcat() in C
    • Inline Function in C
    • sizeof() in C
    • Function Prototype in C
    • C ftell()
  • Array
    • Arrays in C Programming
    • 2-D Arrays in C
    • 3D Arrays in C
    • Multidimensional Array in C
    • Array Functions in C
    • Strings Array in C
  • Sorting
    • Sorting in C
    • Heap Sort in C
  • Advanced
    • Constructor in C
    • Encapsulation in C
    • C Storage Classes
    • Static Keyword in C
    • File Handling in C
    • Queue in C
    • Hexadecimal in C 
    • typedef in C
    • Memory Allocation in C
    • Linked List in C
    • Volatile in C
    • Tokens in C
    • Expression in C
    • Regular Expression in C
    • Error Handling in C
    • Types of Errors in C
    • Preprocessor in C
    • Preprocessor Directives in C
    • fscanf() in C
    • #Pragma in C
    • #ifndef in C
    • #undef in C
    • Macros in C
  • C programs
    • Patterns in C Programming
    • Star Patterns in C
    • Number Patterns in C
    • Swapping in C
    • Reverse Number in C
    • Palindrome in C Program
    • Factorial in C
    • Fibonacci Series in C
    • Square Root in C
    • Random Number Generator in C
    • Prime Numbers in C
    • Escape Sequence in C
    • Reverse String in C
    • Leap Year Program in C
    • Anagram Program in C
    • Strong Number in C
    • String Concatenation in C
    • C Programming Matrix Multiplication
    • Decimal to Octal in C
    • Expression Evaluation in C
    • Decimal to Hexadecimal in C
  • Interview question
    • C Programming Interview Questions

Related Courses

C Programming Training Course

C++ Training Course

Java Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - C Programming Training (3 Courses, 5 Project) Learn More