how to stop the for loop at certain i value

Hello! Please, how to stop for loop at the certain i value and embedd do while in there?

Many thanks!



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include<iostream>
using namespace std;
int main(){

int a= 10;
int b=6;

int e;
if (a<b){e=a;}
else
{e=b;}
for (int i=2; i<=e; i++)
{
if (a%i==0)

a=a/i;
cout <<a<<" " <<i<<endl;




}

 
return 0;
}
Last edited on
What do you mean by "stop" and "embedd"?

Your for loop does stop, when e<i.
Are you referring to a nested loop by any chance that only activates when i is a specific value? In this example I just had it output i when it was equal to 3, but if you wanted to stop the loop you can use break instead. If you are looking to stop the outer loop, however, then an if statement within the loop that checks for the desired value can implement the break command.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int main()
{
int a= 10,b=6,e;

if (a<b)
e=a;
else
e=b;
for (int i=2; i<=e; i++)
   {
         if (a%i==0)
         a=a/i;
do
{
cout<<i;
} while (i==3);//end of do/while loop
if (i==3)
break;

cout <<a<<" " <<i<<endl;
   }//end of for loop
return 0;
}
Last edited on
I think he means like this

1
2
3
4
5
6
7
8
9
10
11
12
13
int count = 0;
for( i = 0; i < 10; ++i )
{
    if( i == 5 )
    {
        count = 0;
        do
        {
            std::cout << "i == 5" << std::endl;
            ++count;
        } while( count < 5 );
    }
}
i == 5
i == 5
i == 5
i == 5
i == 5
Topic archived. No new replies allowed.