wali
wali

Reputation: 239

For Loop vs While Loop

In my lecture of Design and Analysis of Algorithms the instructor said the for loop will take less time then while loop for the following sample algo.

1.  for(int i=0;i<5;i++)
    {    
2.      print(i);    
    }

1.  int i=0;
2.  while(i<5)
    {    
3.      print(i);    
4.      i++;    
    }

He said that the compiler will read the 1. of for while 5 times line 2. 4 times thus total time 5+4=9 But in the case of while loop. The compiler will read the 1. for 1 time,2. for 5 time, 3 for 4time and 4. for 4 time. Thus total time 1+5+4+4 = 14time Please tell me is this right. Is for loop is faster than while loop?

Thanks.

Upvotes: 1

Views: 1375

Answers (4)

Samhitha Harini
Samhitha Harini

Reputation: 7

Both the loops are equivalent. Maybe for loop is faster. Because we know the number of iterations for the for loop. so compiler optimizes it and sometimes it may be infinite loop. We don't know the number of iterations for while loop.

Rather than these loops, use do while loop in c. It is faster than others.

Upvotes: 0

Matthieu M.
Matthieu M.

Reputation: 299760

I'll pass on performance (hint: no difference, check the generated IR or assembly for proof) however there are two important differences in syntax and maintenance.

Syntax

The scope of the i variable is different. In the for case, the i is only accessible within the for header and body, while in the while case it is available after the loop. As a general rule, it's better to have tighter scopes, less variables in-flight mean less context to worry about when coding.

Maintenance

The for loop has the neat advantage of grouping all the iterations operations close together, so they can be inspected in one shot and so checked.

Also, there is one important difference when introducing continue statements:

for(int i = 0; i != 10; ++i) {
  if (array[i] == nullptr) { continue; }
  // act on it
}


int i = 0;
while (i != 10) {
  if (array[i] == nullptr) { continue; }
  // act on it
  ++i;
}

In the while case, the introduction of continue has created a bug: an infinite loop, as the counter is no longer implemented.

Impact

for loops are more readable and all-around better for regular iteration patterns. Even better, in C++11 the range-for statement:

for (Item const& item : collection) {
}

where iteration is entirely taken care of by the compiler, so you are sure not to mess up! (it makes the for_each algorithm somewhat moot... and we can wish the older for form starts retreating)

By corrolary: while loops should be reserved to irregular iteration patterns, this way they will attract special care during code review and from future maintainer by highlighting the irregularity of the case.

Upvotes: 2

SK-logic
SK-logic

Reputation: 9714

In all the modern compilers loop analysis is done on a lower level intermediate representation (i.e., when all the high level loop constructs are expanded into labels and jumps). For a compiler both loops are absolutely equivalent.

Upvotes: 2

Joey
Joey

Reputation: 354416

At least with MSVC 16 (VS 2010) the code is pretty much the same in both cases:

for

; Line 5
    xor esi, esi
$LL3@main:
; Line 6
    push    esi
    push    OFFSET ??_C@_03PMGGPEJJ@?$CFd?6?$AA@
    call    _printf
    inc esi
    add esp, 8
    cmp esi, 5
    jl  SHORT $LL3@main

while

; Line 4
    xor esi, esi
$LL2@main:
; Line 6
    push    esi
    push    OFFSET ??_C@_03PMGGPEJJ@?$CFd?6?$AA@
    call    _printf
; Line 7
    inc esi
    add esp, 8
    cmp esi, 5
    jl  SHORT $LL2@main

Code in my Subversion repository.

Upvotes: 6

Related Questions