What Is a Loop? – What Is Loop in C language | A Complete Information

What Is a Loop? – What Is Loop in C language


What Is a Loop? – What Is Loop in C


When we want to run a particular part of the program or a block multiple times, then we use Loop Statements.


This means when we want a particular statement of our program to run again and again until a particular condition is True, then we use Loop Statements.


Let us understand these things with an example.


Example -: If we want to print "Welcome To akashtimes.com" 10 times, then one way can be that we print "Welcome To akashtimes.com" by writing 10 printf() in the program. 


Another way can be that we can use Loop Statements.


#include <stdio.h>  

Void main() 

    printf( "Welcome To akashtimes.com\n"); 

    printf( "Welcome To akashtimes.com\n"); 

    printf( "Welcome To akashtimes.com\n"); 

    printf( "Welcome To akashtimes.com\n"); 

    printf( "Welcome To akashtimes.com\n"); 

    printf( "Welcome To akashtimes.com\n"); 

    printf( "Welcome To akashtimes.com\n"); 

    printf( "Welcome To akashtimes.com\n"); 

    printf( "Welcome To akashtimes.com\n"); 

    printf( "Welcome To akashtimes.com\n"); 


}{codeBox}


Output -:


Welcome To akashtimes.com

Welcome To akashtimes.com

Welcome To akashtimes.com

Welcome To akashtimes.com

Welcome To akashtimes.com

Welcome To akashtimes.com

Welcome To akashtimes.com

Welcome To akashtimes.com

Welcome To akashtimes.com

Welcome To akashtimes.com{codeBox}


If we want to print “Welcome To akashtimes.com” 10 times using Loop Statements, then for this we have to write a Loop Statement in which we will give condition and second we have to write a printf() Statement by which we will get “Welcome To akashtimes.com” will be printed.


#include<stdio.h>

int main() 

    int i = 1;   // initialization expression 

  

    while (i <=10)  // Loop Condition - Test Expression

    { 

        printf( "Welcome To akashtimes.com\n");     

 

         i++;      // update expression 

    } 

    return 0; 

}{codeBox}


Output -:


Welcome To akashtimes.com

Welcome To akashtimes.com

Welcome To akashtimes.com

Welcome To akashtimes.com

Welcome To akashtimes.com

Welcome To akashtimes.com

Welcome To akashtimes.com

Welcome To akashtimes.com

Welcome To akashtimes.com

Welcome To akashtimes.com{codeBox}


If we are printing Welcome To akashtimes.com by writing printf() 10 times in the first way (in which we are printing Welcome To akashtimes.com by writing printf() once) and in a second way (in which we are printing Welcome To akashtimes.com by writing printf() once).


Talking about both these methods, the loop method is very easy and time-saving. In this, we do not need to write the same code, again and again, we have to write the code in a few lines and our work is done.


There are three types of loops in the C language.


Types of Loop in C


In C language, we can use a loop in these three ways -:


  1. while loop
  2. Do while loop
  3. For loop


1. While Loop


While Loop is used when we do not know in advance how many times to run the loop.


In While Loop we write the condition in parenthesis “()” after the while keyword, if the condition is true then the control enters the body of the While Loop and runs the statements inside the While Loop.


As the control comes at the end of the While Loop, the control again goes to the While Loop and the Condition is checked again and if this time also the condition is True then the Statement inside the While Loop is run again.


This process continues until the While Loop condition becomes False. We also use increment (++) and decrement (–) operators to keep changing the condition of the while loop.


While loop is also called entry control loop because condition check is done before control is reached in the while loop's block and if the condition is true then only control enters the body of the while loop and runs a statement inside it.


The syntax of While Loop is something like this -:


while(condition)

{  

//code to be executed  

statements;

statements;

.

.

}{codeBox}


2. Do while loop


Do while loop is used when we want to execute the statement inside the do-while loop block only once.


In the do-while loop, we check the condition at the end and if the condition is True then the control again goes to the Do while loop, and the statement inside it starts executing and if the condition is False then the control Do while The loop is removed and the second statement is executed.


Syntax -:


Do

{  

//code to be executed  

statements;

statements;

statements;

.

.

}while(condition);{codeBox}


3. For Loop


For Loop is used when we have to run a particular statement repeatedly till a particular condition is true.


In the case of For Loop, we would have known in advance how many times the loop would run, so For Loop is also called a pre-tested loop or open-ended loop.


In For Loop, we write both the initialization and control condition of the variable inside the parentheses “ () ” together. If the condition of the For Loop is true, then the statement inside it is run, otherwise, the statement will not run.


Syntax -:


for (initial value; condition; incrementation or decrementation ) 

{

  statements;

}{codeBox}


Conclusion -:


Friends, I hope that after reading this post today, what is your loop? (What is Loop In C ) and how many types of loops are there? All the information related to (Types of Loop in C) would have been found.


If you want a complete tutorial on C language, then see my post-C Language Tutorial, here you will get all the topics of C Programming Language step by step.


Friends, I hope you liked this post. What is the loop in C language? There must have been a lot of information about (What is Loop In C).


If you liked this post, then do not forget to share this post with your friends so that they can also get this information.


If you still have any questions or doubts related to Loop In C, then you must tell me, I will answer all your questions and you can contact us for more information.


Subscribe to our website to get similar information related to Technology, Programming Language, Coding, C Language, C++, Python Course, Java Tutorial. So that we will get notifications of our upcoming new posts soon.



2 Comments

To be published, comments must be reviewed by the administrator.*

  1. Replies
    1. glad to help you, sir.keep coming to our daily blogs update

      Delete
Previous Post Next Post