WHAT IS LOOP IN C++ ? And EXPLAIN ITS TYPES.

What is Loops?

A type of control structure that repeats a statement or set of statements is known as looping structure. It is also known as iterative or repetitive structure. In sequential structure, all statements are executed once. 

    On the other hand, conditional structure may execute or skip a statement on the basis of the given condition. In some situations, it is required to execute a statement or number of statements repeatedly. Looping structures are used for this purpose.

    Loops are basically used for two purposes:

    • To execute a statement or number of statements for a specified number of times such as displaying the user name on screen for 10 times.
    • To use a sequence of values such as displaying the numbers from 1 to 10.

    There are three types of loops available in C++.These are as follows:

    • while loop
    • do while loop
    • for loop

    What is While Loop?

    A while loop in C++ repeatedly executes a block of code as long as the specified condition is true. The condition is checked before the loop body is executed, so if the condition is false at the start, the loop will not run at all.

    While Loop syntax:

    while (condition)

     {

        // code block;

    }

    While loop Flowchart

    • Check the condition.
    • If the condition is true, execute the code block.
    • After the code block is executed, go back to step 1.
    • If the condition is false, exit the loop.

    While loop Example:

    int i = 0;

    while (i < 5)

     {

        std::cout << i << std::endl;

        i++;

    }


    While Loop Explanation:

    This loop prints the values from 0 to 4. The loop continues as long as i is less than 5. After each iteration, i is incremented by 1.

    While Loop Use Cases:

    • Continuously performing an action until a specific condition is met.
    • Waiting for a certain event or condition to change.

    What is Do-While Loop?

    The do-while loop is similar to the while loop, but the key difference is that the condition is checked after the loop's body has been executed. This guarantees that the loop body runs at least once, even if the condition is false on the first check.

    While Loop Syntax:

    do {

        // code block ;

    } while (condition);

    Do While Loop Flowchart:

    • Execute the code block.
    • Check the condition.
    • If the condition is true, go back to step 1.
    • If the condition is false, exit the loop.

    Do while Loop Example:

    int i = 0;

    do {

        std::cout << i << std::endl;

        i++;

    } while (i < 5);


    Do While Loop Explanation: 

    This loop prints the values from 0 to 4. The loop runs the code block first, then checks if i is less than 5. Since the condition is checked after the block of code, the loop executes at least once.

    Do While Loop Use Cases:

    • Running a piece of code at least once regardless of the condition.
    • Input validation, where you want to prompt the user at least once before checking the input.

    What is For Loop ?

    A for loop in C++ is typically used when the number of iterations is known beforehand. It allows you to initialize a variable, check a condition, and update the variable all in one line. The loop executes the code block as long as the condition is true.

    For Loop Syntax:

    for (initialization; condition; update)

     {

        // code block;

    }

    For Loop Flowchart:

    • Initialize the loop variable.
    • Check the condition.
    • If the condition is true, execute the code block.
    • After the code block is executed, perform the update step.
    • Go back to step 2.
    • If the condition is false, exit the loop.

    For Loop Example:

    for (int i = 0; i < 5; i++)

     {

        std::cout << i << std::endl;

    }

    For Loop Explanation:

    This loop prints the values from 0 to 4. It starts by initializing i to 0, then checks if i is less than 5. After printing the value of i, it increments i by 1. The loop continues until i is no longer less than 5.

    For Loop Use Cases:

    • Iterating over a fixed range of values.
    • Looping through arrays or other data structures with a known number of elements.

    Key Differences and When to Use Each:

    • While Loop: Use when the loop should continue as long as a condition is true, and you might not know the number of iterations in advance.
    • Do-While Loop: Use when you need to ensure the loop executes at least once, regardless of the condition.
    • For Loop: Use when you know in advance how many times you want to execute the loop, especially when iterating over a range or collection

    Counter-Controlled Loops

    A loop that repeats a specific number of times is known as a count-controlled loop. This type of loop depends on the value of a variable known as counter variable. The value of counter variable is incremented or decremented each time the body of the loop is executed. 

    This loop terminates when the value of counter variable reaches a particular value. The number of iterations of counter-controlled loop is known in advance.

    The number of iterations of counter-controlled loop depends on the following:

    • Initial value of the counter variable
    • Terminating condition
    • Increment or decrement value

    Sentinel-Controlled Loops

    The sentinel-controlled loop is a type of loop that depends on a special value known as sentinel value. The loop terminates when the sentinel value is encountered. These types of loops are also known as conditional loops.

    For example, a loop may execute while the value of a variable is not -1. Here-1 is the sentinel value that is used to terminate the loop.

    The number of iterations of sentinel-controlled loop is unknown. It depends on the input from the user Suppose the sentinel value to terminate a loop is -1. 

    If the user enters -1 in first iteration, the loop will execute only once. But if the user enters -1 after entering many other values, the number of iterations will vary accordingly. A sentinel value is commonly used with while and do while loops.

    Tags

    Post a Comment

    0 Comments
    * Please Don't Spam Here. All the Comments are Reviewed by Admin.