Differences Between for, while, AND do while loop

  • Home
  • Differences Between for, while, AND do while loop
Shape Image One

Differences Between For, While, And Do While Loop

advanced divider

Differences Between For, While, And Do While Loop:

For loop:

  1. It is pre-test loop because the condition is tested at the start of the loop.
  2. It is known as an entry controlled loop.
  3. If the condition is not true the first time, then control will never enter in a loop.
  4. There is no semicolon after the condition in the syntax of the for loop.
  5. Initialization and updating is the part of the syntax.

 

While loop:

  1. It is pre-test loop because the condition is tested at the start of the loop.
  2. It is known as an entry controlled loop.
  3. If the condition is not true first time then control will never enter in a loop.
  4. There is no semicolon after the condition in the syntax of the while loop.
  5. Initialization and updating is not the part of the syntax.

 

Do while loop:

  1. It is post-test loop because condition is tested at the end of the loop which executes loop at least once.
  2. It is known as exit controlled loop.
  3. Even if the condition is not true for the first time, the control will enter in a loop.
  4. There is a semicolon after the condition in the syntax of the do while loop.
  5. Initialization and updating is not the part of the syntax.

Jump Statements:

These statements change the normal execution of program and jumps over the specific part of program.

Following are the jumps statements used in C++.

 break

continue

goto

return

exit ()

Break statement:

The break statement allows you to exit a loop or a switch statement from any point within its body, bypassing its normal termination expression. It can be used within any C++ structure.

 

Break example:

Copy code

#include<iostream>

using namespace std;

int main()

{

    int count;

    for(count = 1; count <= 100; count++)

    {

        cout << count;

        if(count = 10)  // This line seems to have an error: it should be ‘if (count == 10)’

            break;

    }

    return 0;

}

Continue Statement:

The continue statement is just the opposite of the break statement. Continue forces the next iteration of the loop to take place, skipping the remaining statements of its body.

 

Continue example:

 

 

#include<iostream>

using namespace std;

int main()

{

    // The rest of the continue example code is missing from this extraction

}

{

    int count;

    for(count = 1; count <= 10; count++)

    {

        if(count = 5)

            continue;

        cout << count;

    }

    return 0;

}

 

Goto statement:

In C++ programming, the goto statement is used for altering the normal sequence of program execution by transferring control to some other parts of the program.

 

Goto example:

#include<iostream>

using namespace std;

int main()

{

    float num, average, sum = 0.0;

    int i, n;

    cout << “Maximum number of inputs: “;

    cin >> n;

    for(i = 1; i <= n; i++)

    {

        cout << “Enter number” << i << “: “;

        cin >> num;

        if(num < 0.0)

        {

            // Control of the program move to jump:

            goto jump;

        }

        sum += num;

    }

Jump:

    average = sum / n;

}

cout << “\n Average = ” << average;

return 0;

}

 

Return statement:

  • The return statement returns the flow of the execution to the function from where it is called. This statement does not mandatorily need any conditional statements. As soon as the statement is executed, the flow of the program stops immediately and returns the control from where it was called.

    Syntax:

    return (expression / value)

Exit() statement:

  • The exit function, declared in <stdlib.h>, terminates a C++ program. The value supplied as an argument to exit is returned to the operating system as the program’s return code or exit code.

    Syntax:

    void exit (int);

Quiz

advanced divider