[ACCEPTED]-Example of a while loop that can't be written as a for loop-control-flow

Accepted answer
Score: 15

Yes, easily.

while (cond) S;

for(;cond;) S;

0

Score: 2

The while loop and the classical for loop are interchangable:

for (initializer; loop-test; counting-expression) {
    …
}

initializer
while (loop-test) {
    …
    counting-expression
}

0

Score: 1

If you have a fixed bound and step and do 13 not allow modification of the loop variable 12 in the loop's body, then for loops correspond 11 to primitive recursive functions.

From a 10 theoretical viewpoint these are weaker than 9 general while loops, for example you can't 8 compute the Ackermann function only with 7 such for loops.

If you can provide an upper 6 bound for the condition in a while loop 5 to become true you can convert it to a for 4 loop. This shows that in a practical sense 3 there is no difference, as you can easily 2 provide an astronomically high bound, say 1 longer than the life of the universe.

Score: 1

Using C

The basic premise is of the question is 10 that while loop can be rewritten as a for loop. Such 9 as

init;
while (conditional) {
  statement;
  modify;
}

Being rewritten as;

for ( init; conditional; modify ) {
  statements;
}

The question is predicated 8 on the init and modify statements being moved into the 7 for loop, and the for loop not merely being,

init;
for (; conditional; ) {
  modify;
}

But, it's 6 a trick question. That's untrue because 5 of internal flow control which statements; can include. From 4 C Programming: A Modern Approach, 2nd Edition you can see an example on Page 119,

n = 0;
sum = 0;
while ( n < 10 ) {
  scanf("%d", &i);
  if ( i == 0 )
    continue;
  sum += i;
  n++;
}

This 3 can not be rewritten as a for loop like,

sum = 0;
for (n = 0; n < 10; n++ ) {
  scanf("%d", &i);
  if ( i == 0 )
    continue;
  sum += i;
}

Why 2 because "when i is equal to 0, the original loop doesn't increment n but the new loop does.

And that essentially boils down 1 to the catch,

Explicit flow control inside the while loop permits execution that a for loop (with internal init; and modify; statements) can not recreate.

Score: 0

While loops can be more helpful when the 6 number of loop iterations are not known 5 while for loops are effective when the loop 4 iterations are known. Consider the following 3 code snippet for student marks, but the 2 number of students is not known

ArrayList 1 studentMarks = new ArrayList();

    int score = 100;
    int arraySize = 0;
    int total = 0;
    System.out.println("Enter student marks, when done press any number less than 0 0r greater than 100 to terminate entrancies\n");
    
    while(score >= 0 && score < 101) {
        System.out.print("Enter mark : ");
        
        score = scan.nextInt();
        if(score < 0 | score > 100)
            break;
        studentMarks.add(score);
        arraySize += 1;
        

        
    }
    // calculating total, average, maximum and the minimum values
    

    
    
    for(int i=0;i<studentMarks.size();i++) {
        total += studentMarks.get(i);
    System.out.println("Element at [" + (i+1)+"] : " +studentMarks.get(i)); 
        
    }
    
    System.out.println("Sum of list element is : " + total);
    System.out.println("The average of the array list : " + (total/(studentMarks.size())));
    Collections.sort(studentMarks);
    System.out.println("The minimum of the element in the list is : " + studentMarks.get(0));
    System.out.println("The maximum of the element in the list is : " + studentMarks.get(studentMarks.size()-1));
    
    scan.close();

More Related questions