My first guess would have been brute forcing it like dacaballero. But that was solved. And chances are good it is not even a performance drain. The JiT and other Optimisation are pretty good and might figure out "hey, that value is only actually read once after the last loop. No point doing that line before then". Indeed without the output, there is a decent chance it will cut the whole loop out.
And depending on what you are doing in the long run, a different loop might be better. I like to use the while/do while with a bool control variable.
int x = a * b;
int finalValue = 0;
bool continueLoop = true;
int i = 0; //Counter variable
do{
//Let us only do that one once
int current = Math.Pow(i,2);
if(current < x){
//Nothing to do here.
//I just prefer a empty if/else over a NOT modifier
}
else{
finalValue = current;
continueLoop = false;
}
i++;
}while(continueLoop)
Of course you also asked about using that variable for the next loop. Now you can nest loops, but I do find they get a bit too hard to read to nest even 2 loops. So small modification:
- make above loop or the simple for variant a function. call it innerLoop.
- It takes one input - the int value for x/the endpoint
- it returns the finalValue as return value.
Call the function in this loop:
int endpoint = a * b;
do{
endpoint = innerLoop(endpoint);
Console.WriteLine(endpoint);
}while(true); //you gave no condition, so I infinite loop.