-2

int i=0;
A1:
cout << “I is “ << i << endl;
i = i + 1;
if ( i <= 10) goto A1;
cout << “the loop is completed\n”;


It is poorly written using goto statements; your job is to convert it into
a. while-loop,
b. do-while loop,
c. for-loop.

3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by blackrainbowhun
-1
#include <iostream>

using namespace std;

int main()
{
    for (int i=0;i<=10;i++)
    {
        cout << "I is " << i << endl;
    }
    cout << "The loop is completed\n";
    return 0;
}

This is the for loop.

#include <iostream>

using namespace std;

int main()
{
    int i=0;
    do
    {
        cout << "I is " << i << endl;
        i++;
    }
    while(i<=10);
    cout << "The loop is completed\n";
    return 0;
}

This is the do-while loop.

#include <iostream>

using namespace std;

int main()
{
    int i=0;
    while (i<=10)
    {
        cout << "I is " << i << endl;
        i++;
    }
    cout << "The loop is completed\n";
    return 0;
}

And this is the while loop.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.