0

Using the conversion from the for-loop to the while-loop in the example below, can // fixed block be any valid block of code given that you can add any other blocks of code before or after it in the while-loop?

for (int i = 0; i < 10; i++) {
    // fixed block
}


int i = 0;
while (i < 10) {
    // any block
    // fixed block
    // any block
}

To show that the answer may not trivially be "yes", I thought of a block of code that may be a counter example:

for (int i = 0; i < 10; i++) {
    i = i * 2;
    if (1 < 2) {
        print(i);
        continue;
    }
}

The if-statement is there so that in the while-loop you can add a block of code after the continue; statement without getting a possible compiler error/warning. The output of the for-loop is 0, 2, 6 and 14. Adding i++; either before or after the fixed block of code in the while-loop doesn't work, showing that the answer may not be trivial.

Alan
  • 175
  • 4
  • 15
  • 2
    @PaulG: What are you talking about? `for (int i = 0; i < 0; i++);`. – user2357112 Mar 21 '14 at 19:39
  • You're question has evolved into a completely new one and is no longer clear. – PaulG Mar 21 '14 at 20:06
  • Evolved into a completely new one? The question hasn't changed. I can understand that the question may not be clear but sorry, I can't think of a better way present it. Yes, I do ask two questions at the end of the post but the first just leads to the original question. I fear that if I ask the question without giving an example, people will blindly answer "yes". The point of the proposed counter-example is to show that the answer is not trivial. I think the example is throwing people off from addressing the question directly, but I also think excluding it would be worse. – Alan Mar 21 '14 at 21:32
  • Possible duplicate of [Example of a while loop that can't be written as a for loop](https://stackoverflow.com/questions/1514382/example-of-a-while-loop-that-cant-be-written-as-a-for-loop) – Evan Carroll Jul 24 '18 at 21:10

1 Answers1

1

Sure they can, just set up a similar exit condition. I don't think the inverse (while loops converted to for loops) is true though.

Also, yes, you're blocks of code will function the same in both cases, if the code is valid (logically and syntactically), then yes it will work.

PaulG
  • 6,920
  • 12
  • 54
  • 98
  • 2
    `while` loops are even easier to turn into `for` loops than the other way around. `while(whatever){do_thing();}` -> `for(;whatever;){do_thing();}`. You don't even need to introduce extra scopes or rename variables to dodge scope issues. – user2357112 Mar 21 '14 at 19:52
  • Sure it's easy but it depends on where the condition is coming from. With a `for` loop you will almost always have at least one iteration, but with a `while` loop this might not be the case, depends on the condition. `for` loops are built to run at least one iteration because you're declaring the variable, condition and iterator in one place (otherwise why use them?). So if the question was "Can all while-loops be converted to for-loops?" I would say no, not "all". – PaulG Mar 21 '14 at 19:55
  • I'm not sure what kind of `for` loops you've been writing. Surely you've for-looped over an empty array, haven't you? – user2357112 Mar 21 '14 at 19:59
  • @user2357112 True, I have indeed, and I prefer `foreach` or an equivalent for collections. My point is, I wouldn't answer yes to the question had it been reversed, I don't think it would work in "all cases". – PaulG Mar 21 '14 at 20:00