I want to write a simple while loop as a for. So, the while is
but i can't figure out the for. This doesn't work:Code:while (i!=-1) { //code }
Help me please.Code:int i; for (;i != -1;) { //code }
I want to write a simple while loop as a for. So, the while is
but i can't figure out the for. This doesn't work:Code:while (i!=-1) { //code }
Help me please.Code:int i; for (;i != -1;) { //code }
What's the issue? Do you get a compiler error? Also, i should be initialized to some value before making comparisons against it!
initialize to what? i is scanned every time inside the for loop so it can't be initialized.
Edit: I wrote the code like this and it worked:
for (int i=1;i !=-1; ) {
scanf_s("%d",&i); printf("%d\n",i);
}
but the problem is, the value that the for loop gets is float so I can't put it in the ()-s of for, like this:
float x;
for (x=1.0; x != -1; )
How about this?
Last edited by lmanukyan; 12-14-2015 at 10:02 AM.
What is the while loop that worked and what is the for loop (in the pattern of your post #1) that did not work?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
The for loop evaluates i != -1 up front. If i is left undefined, you're comparing undefined != -1, which is to be avoided.
It depends on what you do. Comparing floating point values for equality needs extra care. If you just want to compare against -1 and you're only working with user input, as in your example, then you could cast the float to and int and compare afterwards.but the problem is, the value that the for loop gets is float so I can't put it in the ()-s of for, like this:
float x;
for (x=1.0; x != -1; )
How about this?
If however you do arithmetic inside the loop, you need to be sure that (int)x is guaranteed to reach the value -1, or the loop will run forever.Code:for(; (int)x != -1;)
In the existing "while" version of the code block, the i variable must have some value in order for program execution to make it into the while block in the first place (but not equal to -1). So what value is it right now? Initialize it to the same value when you convert the code block to a for loop. You could initialize i to any value (!= -1) as long as you are certain you want the for loop to execute the very first time. After that, the i value will be scanned in each time through the loop anyhow.
Alternately, add a couple more lines of code: omit the initialization, but scan in the value of i once before entering the for loop. If it's assigned a value of -1, it won't enter the for loop at all.
Very hard to help, the code snippet on its own makes no sense or gives no indentation of what you are trying to do.
I'll try offer some general guidance and hopefully something will ring true.
The general rule of thumb is, always use a for loop when you 'know' how many times it will loop.. And a while loop when you really don't know or are unsure.
The basic syntax for a while loop is.
A for loop, is pretty much the same thing, but we can declare and assign a value to i (initializing), we can compare and also increment or decremented in one line at the start.Code:int i = 10; // i must be declared and assigned to some value while (i != -1) { // Do something here until i == -1. i--; // i needs to be incremented or decremented }
Hope this helps, if not perhaps post more code and explain what you're actually trying to achieve!Code:for (int i = 10; i != -1; i--) { // Do something i number of times until i == -1 }