I have a little special question about the for loop. Normally you could do the count as I have declared with a count from 0 - 10.
What I do inside the loop is to identify many scenarios if the count should start from 0 - 10 or vice versa 10 - 0 to "optimize" the speed.
I can change Start, End and StepValues inside the loop but there is one thing left that should be my question.
If I change Start to 10, End to 0 and Step to - Step,
I would want to change "i < End" to "i > End".
So my question is if this is possible in any way to achieve. A kind of "intelligent" loop.

``````int Start = 0;
int End = 10;
int Step = 1;

for (int i = Start; i < End; i+=Step)
{

//I can change Start to 10 inside the loop
Start = 10;

//I can change End to 0 inside the loop
End = 0;

//I can change Step to a negative value for countdown inside the loop
Step = - Step;

}``````

you can't change the < operator to >, but you can change i to be greater than End and Step to negative, which I believe will accomplish the same thing.

If I understand you would meen an example like the code below.
I use a file to write i to it but nothing is written to the file.
If I change to: for( int i = 0; i < 10; i+=1 )
then it writes i to the file.

``````ofstream File1;
File1.open("C:\\File.txt");

for( int i = 10; i < 0; i+=-1 )
{

File1<< i << '\n';

}``````

you can't change the < operator to >, but you can change i to be greater than End and Step to negative, which I believe will accomplish the same thing.

If I understand you would meen an example like the code below.
I use a file to write i to it but nothing is written to the file.
If I change to: for( int i = 0; i < 10; i+=1 )
then it writes i to the file.

``````ofstream File1;
File1.open("C:\\File.txt");

for( int i = 10; i < 0; i+=-1 )
{

File1<< i << '\n';

}``````

This flunks the for-loop condition right way and thus you'll never go through the loop even once since (10 < 0) is false and therefore the loop is "exited" before it even starts:

``for( int i = 10; i < 0; i+=-1 )``

Yes thats true. Thank you for that. I found out a "way around" like this with your help.
Using negative numbers that I then later will convert to positive values etc...

``````ofstream File1;
File1.open("C:\\File.txt");

for( int i = -1; i < 0; i+=-1 )
{
if( i == -10 ){break;}
File1 << i << '\n';

}
File1.close();``````

This flunks the for-loop condition right way and thus you'll never go through the loop even once since (10 < 0) is false and therefore the loop is "exited" before it even starts:

``for( int i = 10; i < 0; i+=-1 )``

I have a little special question about the for loop. Normally you could do the count as I have declared with a count from 0 - 10.
What I do inside the loop is to identify many scenarios if the count should start from 0 - 10 or vice versa 10 - 0 to "optimize" the speed.
I can change Start, End and StepValues inside the loop but there is one thing left that should be my question.
If I change Start to 10, End to 0 and Step to - Step,
I would want to change "i < End" to "i > End".
So my question is if this is possible in any way to achieve. A kind of "intelligent" loop.

``````int Start = 0;
int End = 10;
int Step = 1;

for (int i = Start; i < End; i+=Step)
{

//I can change Start to 10 inside the loop
Start = 10;

//I can change End to 0 inside the loop
End = 0;

//I can change Step to a negative value for countdown inside the loop
Step = - Step;

}``````

instead of using i+=step....U should use i-=step

instead of using i+=step....U should use i-=step

I'm afraid not -- once the program is compiled the += operator can't be changed to -=.

You can make it work by changing the for statement to :

for (int i = Start; ((Step<0)?(i>End):(i<End)); i+=Step)

This will allow the for loop to evaluate the correct ending based on the "Step" being positive or negative. Just remember that whatever "i" is currently is assigned to it will continue from that value unless you reassign it. so to get the loop to start from 10 you will need to set "i" = 10 at the start of the loop or to 11 at the bottom of the loop so that it will "Step" down to 10 and rerun the loop.

The loop as is, changed with the for statement above will:
1. Assign "i"=0
2. Reassign Start, End, and Step to new values
3. Since "i" was not reassigned, it will execute i+=Step set "i" to -1 and exit since "i" is now > "End" which is set to 0.

In addition to my last post you can keep "Step" positive and use the += and -= by using the following:

for (int i = Start; (Start>End) ? i>End : i<End; (Start>End) ? i-=Step : i+=Step)

So you only need to assign the Start and End values. (Still may need to reassign "i" to run through loop with i=Start.)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.