Thread: while --> for

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    82

    while --> for

    I want to write a simple while loop as a for. So, the while is
    Code:
    while (i!=-1) {
    //code
    }
    but i can't figure out the for. This doesn't work:

    Code:
    int i;
    for (;i != -1;) {
    //code
    }
    Help me please.

  2. #2
    Guest
    Guest
    What's the issue? Do you get a compiler error? Also, i should be initialized to some value before making comparisons against it!

  3. #3
    Registered User
    Join Date
    Nov 2015
    Posts
    82
    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.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is the while loop that worked and what is the for loop (in the pattern of your post #1) that did not work?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Guest
    Guest
    Quote Originally Posted by lmanukyan View Post
    initialize to what? i is scanned every time inside the for loop so it can't be initialized.
    The for loop evaluates i != -1 up front. If i is left undefined, you're comparing undefined != -1, which is to be avoided.

    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?
    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.
    Code:
    for(; (int)x != -1;)
    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.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    5
    Quote Originally Posted by lmanukyan View Post
    I want to write a simple while loop as a for. So, the while is
    Code:
    while (i!=-1) {
    //code
    }
    but i can't figure out the for. This doesn't work:

    Code:
    int i;
    for (;i != -1;) {
    //code
    }
    Help me please.
    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.

  7. #7
    Registered User
    Join Date
    Dec 2015
    Posts
    34
    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.

    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
    }
    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:
    for (int i = 10; i != -1; i--)
    {
        // Do something i number of times until i == -1
    }
    Hope this helps, if not perhaps post more code and explain what you're actually trying to achieve!

  8. #8
    Registered User
    Join Date
    Nov 2015
    Posts
    82
    Lawson thanks for reminding me about an important part -
    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
    . Guest this is a good idea too, thanks.

Popular pages Recent additions subscribe to a feed