Table of Contents

    Introduction to Loops in C Programming

    In C, and all other modern programming languages, iteration statements (also called loops) allow a set of instructions to be repeatedly executed until a certain condition is reached. This condition may be predetermined (as in the for loop) or open-ended (as in the while and do-while loops).

    In another way we can say, A loop can be used to tell a program to execute statements repeatedly. Or we can say that a loop repeatedly executes the same set of instructions until a termination condition is met.

    Why Loop is Important ?

    Suppose that you need to display a string (e.g., Welcome to atnyla! ) a hundred times. It would be tedious to have to write the following statement a hundred times:

    printf("Welcome to atnyla!");
    printf("Welcome to atnyla!");
    ...
    ...
    ...
    printf("Welcome to atnyla!");

    So, how do you solve this problem? C hava a powerful feature called a loop that controls how many times an operation or a sequence of operations is performed in succession. Using a loop control statement, you simply tell the computer to display a string a hundred times without having to code the print statement a hundred times, as follows:

    Program

    #include
    void main(){
    	
    int count = 0;
       while (count < 100) {
       printf("Welcome to atnyla! \n");
       count++;
       }
       
    }

    Output

    Welcome to atnyla!
    Welcome to atnyla!
    ....
    ....
    ....
    Welcome to atnyla!
    Welcome to atnyla!
    100 times
    Press any key to continue . . .

    Explanation

    The variable count is initially 0 . The loop checks whether count < 100 is true . If so, it executes the loop body to display the message Welcome to atnyla!and increments count by 1 . It repeatedly executes the loop body until count < 100 becomes false. When count < 100 is false (i.e., when count reaches 100 ), the loop terminates and the next statement after the loop statement is executed

    Another important Example ?

    Program

    #include
    void main(){
     int n = 10;
      while(n > 0) {
      printf("%d \n",n);
      n--;
      }
       
    }

    Output

    10
    9
    8
    7
    6
    5
    4
    3
    2
    1
    Press any key to continue . . .

    Types of Loop

    Java provides mainly three types of loop statements:whileloops, do - whileloops, andforloops.

    Type of loops
    Figure: Type of loops

    Sr.No. Loop & Description
    1 while loop

    Inwhile loopfirst checks the condition if the condition is true then control goes inside the loop body otherwise goes outside of the body.

    2 do...while loop

    A do while loop is a control flow statement that executes a block of code at least once, and then repeatedly executes the block, or not, depending on a given condition at the end of the block (in while).

    3 for loop

    For Loop is used to execute set of statementsrepeatedlyuntil the condition is true.

    Practice Quiz 19 MCQs Smart Learning

    Master This Topic with Smart Practice

    Reinforce what you just learned by solving high-quality MCQs. Improve accuracy, boost confidence, and prepare like a topper.

    Topic-wise MCQs
    Instant Results
    Improve Accuracy
    Exam Ready Practice
    Login & Start Quiz Create Free Account
    Save progress • Track results • Learn faster