-3

The question is asking for a comparison between costs of for and while loops, relative to the cost of if statements.

Say an if statement has a relative cost of 1, what's the relative cost of for and while loops?

Assume that a single boolean value is being passed onto the while loop and if statement when comparing and the while is allowed to iterate X times. Assume that the for also iterates X number times.

ThePixelPony
  • 721
  • 2
  • 8
  • 30

2 Answers2

9

In terms of performance, if statements are usually of O(1) complexity, where as for and while loops are usually O(n), where n is the amount of loops it takes depending on what the conditions are. For example:

for (int i = 0; i < 100; i++)
{
    // Some statements
}

Would be O(100), because it is looped through 100 times. It does not matter how many statements are inside the for loop, unless there are additional loops inside the initial loop. Then you get to O(n^2) complexity, which is quadratic and thus highly inefficient.

However, do not let this scare you. Loops exist for a reason, and they are necessary in many programs.

Lawrence Aiello
  • 4,560
  • 5
  • 21
  • 35
  • @laiello - Nah .. I know. just too brief is all. maybe we judge quickly – Caffeinated Nov 18 '14 at 16:46
  • also if you need to loop 100 times it would certainly be more clean then 100 if statements. also you will barely notice these time differences with todays computers unless you are doing something very intensive. – jgr208 Nov 18 '14 at 16:58
  • I agree, but I implore you to go to an algorithms professor and say to him "Loop complexity does not matter". Maybe not for overall programs, but in Computer Science, it is very important. – Lawrence Aiello Nov 18 '14 at 17:07
0

Loops are language constructs. You can write a while loop to do the same thing as a for loop and vice versa.

Say an if statement has a relative cost of 1, what's the relative cost of for and while loops?

for loops and while loops also check a condition. In that sense, they also have a cost of 1. The point is that there is typically something in the loop that will affect that condition.

Consider this code

public static void main(String[] args) throws Exception {
    for (int i = 0; i < 10; i++) {

    }

    System.out.println("");

    int i = 0;
    while (i < 10) {
        i++;
    }
}

The bytecode generated is

    // start for
     0: iconst_0
     1: istore_1
     2: iload_1
     3: bipush        10
     5: if_icmpge     14
     8: iinc          1, 1
    11: goto          2
    // end for
    14: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
    17: ldc           #3                  // String
    19: invokevirtual #4                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
    // start while
    22: iconst_0
    23: istore_1
    24: iload_1
    25: bipush        10
    27: if_icmpge     36
    30: iinc          1, 1
    33: goto          24
    36: return
    // end while

You'll notice both loops do the exact same thing and generate the exact same bytecode.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724