While & do while loop

I've been trying to figure out what is going on within these two loops? Assuming all variables were of type int.

1
2
3
4
5
6
7
8
9
10
11
12
13
i =1;
while (i*i < 10)
{
    j = i;
    while (j*j < 100)
    {
        cout << i + j << endl;
        j *= 2;
    }
    i++;
}
cout << ā€œ\n*****\nā€;


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
i = 0;
do
{
    j = i * i * i;
    cout << i;
    do
    {
        k = i + 2 * j;
        cout << j << k;
        j += 2;
    }
    while (k <= 10);
    cout << endl;
    i++;
} while (j <= 5);
What is the question? If you just want to know the values for the variables in each cycle just add some cout << here and there, maybe like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
i =1;
while (i*i < 10)
{
    cout << "i = " << i << endl;
    j = i;
    while (j*j < 100)
    {
        cout << "j = " << j << endl;
        cout << "i + j = " << i + j << endl;
        j *= 2;
    }
    cout << "j*j was greater than 100. Loop terminated" << endl;
    i++;
    cin.get(); // Will pause the execution until you press enter, so you can read the console
}
cout << ā€œ\n*****\nā€;


You can change the information you print according to what you want to know, or add more print instructions
Topic archived. No new replies allowed.