question about the increase in a for loop

is it possible to make it increase more than just 1 in a for loop? As in is there something else besides variable++ or variable--?
variable += n?
1
2
3
4
5
for (int x = 0; x < 20; x += 2)
{
    cout << x;
    cout << "\nLooks like x has gone up by 2!\n";
}
thanks
actually i have another question now but i didn't feel like opening a new thread. In this code why is the square root calculated 1 higher than it should be? I'm guessing the loop is running 1 too many times but could you explain how to fix that.
1
2
3
4
5
6
7
double Correct_Result;
double Testing_Number(1);
std::cout<<"Number to find the square root of: ";
std::cin>>Correct_Result;
for(double Test_Result=0;Test_Result!=Correct_Result;Testing_Number++){
Test_Result=Testing_Number*Testing_Number;}
std::cout<<"The square root is "<<Testing_Number<<std::endl;}

I was at 1 point just having the program subtract 1 before displaying the result but I considered that kinda hackish so i wanna do it properly
That code could use a bit LOT of indentation and spacing.
I don't think that's the ideal way to find a square root. It assumes that the user is asking for a perfect square and I don't know the mathematical algorithms to find a "raw" square root.
I believe I see what's going wrong, though. You find the right number, but then at the end of the loop it's incremented again, which pushes it one over. You can just subtract one at the end of the loop.
yeah i know it could use a lot of indention and spacing. It's part of a much bigger program so i tried removing some of the extra indention that would make it hard to read and accidentally deleted some extra. And yea I know it expects that. Once I figured this out I was planning on making it so that if it wasn't a square root that it would say something like perfect squares only. Also the point was so that I didn't need to have -1 at the end because I was doing that but I wanted a better way to do it.
Well, let's see.... the thing is that for increments at the end. So if you, say, wrote a while loop, and incremented at the start, it would fix your problem. I can't really say because I'm not that good at ordering these things out. Try drawing out a flow of the problem; it shouldn't be too hard to visualize. I'm just really bad at that kind of thing.
hmm okay thanks for the help ill go research while loops AGAIN because i keep forgetting how to use them because i barely ever need to lol. Also if someone a bit more knowledgeable on this sorta thing could help that would be great
The three parts of the for statement are not mandatory - just rearrange things:

1
2
3
4
5
6
7
8
9
10
double Correct_Result;
double Testing_Number(0); //This line changed
std::cout<<"Number to find the square root of: ";
std::cin>>Correct_Result;
for(double Test_Result=0;Test_Result!=Correct_Result; /*leave this part empty*/)
{
    Testing_Number++; //put this here
    Test_Result=Testing_Number*Testing_Number;
}
std::cout<<"The square root is "<<Testing_Number<<std::endl;}
lol I just did that as you typed that because I was just about to post here that I fixed it myself anyway thanks for the help everyone!
Topic archived. No new replies allowed.