Loops in C | Set 2

Updated

Loops are very useful when you want to perform a task repeatedly. Loops are used to execute a set of statements repeatedly until a particular condition is satisfied.

A loop consists of two parts, a body of a loop and a control statement. The purpose of the loop is to repeat the same code a number of times.

Types of Loop :

There are 3 types of Loop in C language, namely:

1. for loop –
A for loop is a more efficient loop structure in ‘C’ programming. The general structure of for loop is as follows:

Syntax :

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

Note that the initial value of the for loop is performed only once.

  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 evaluate the increment/decrement condition and again follows from step 2.

When the condition expression becomes false, it exits the loop.

2. while loop –
It tests the condition before executing the loop body. The basic format of while loop is as follows:

Syntax :

// variable initialization;
while(condition)
{
    // statements;
    // variable increment or decrement; 
}

3. do while loop –
It is more like a while statement, except that it tests the condition at the end of the loop body. If post-test is required, use a do-while loop.

Syntax :

initialization expression;
do
{
   // statements

   update_expression;
} while (test_expression);

Loop Control Statements :

Loop control statements change execution from its normal sequence. These control statements may used to terminate or continue forcefully before normal end of loop.

  1. ‘break’ statement –
    It terminates the loop or switch statement and transfers execution to the statement immediately following the loop or switch.

  2. ‘continue’ statement –
    It is opposite of ‘break’ statement, It continue statement is used inside loops. It forces the next iteration of the loop to take place, skipping any code in between.

  3. ‘goto’ statement –
    It transfers control to the labeled statement. It provides an unconditional jump from the ‘goto’ to a labeled statement in the same function.

Infinite Loop :

It is a piece of coding that lacks a functional exit so that it repeats indefinitely. A loop becomes an infinite loop (sometimes called an endless loop) if a condition never becomes false. It may because of increment or decrement parameter is not set properly or absent. Or may because of conditions are always true.

Example :

#include 
 
int main () {

   for( ; ; ) {
      printf("This this a infinite loop\n");
   }

   return 0;
}

So, this loop will not be terminate. You can terminate an infinite loop by pressing Ctrl + C keys.



Please write comments if you find anything incorrect. A gentle request to share this topic on your social media profile.

Next tutorial
CPP_Programmer Published 19 Aug 2020 · 3 min read

Related tutorials

  • Call by value, call by address and call by reference in C++

    The parameters of the function call are known as actual parameters and the parameters of the function definition are known as formal parameters. There are 3 ways of…

    3 min read
  • What should we use void main() or int main() ?

    There is question that what should we use void main() or int main() ? void main() { /* ... */ } Or, int main() { /* ... */…

    2 min read
  • C Comments

    Prerequisite – C Language Introduction C comments explain code and improve readability. These do not affect program execution. You can use comments in C to clarify code and describe…

    1 min read
  • C Programming Language Standard

    Prerequisite – C Language Introduction The C programming language has various versions: C89/C90, C99, C11, and C18. GATE CS 2027 Online Test Series72 tests in the real GATE…

    2 min read
  • Features of C Programming Language

    Prerequisite – C Language Introduction C is a simple language made in 1972 by Dennis Ritchie. It’s for system programming. C has low-level memory access, basic keywords, good…

    1 min read
  • C Language Introduction

    C is a programming language developed by Dennis Ritchie in 1972. It has a significant historical impact. It was originally created at Bell Laboratories of AT&T Labs for…

    4 min read