Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

while loop requires explicit condition, for loop doesn't, why?

Tags:

In C++ you are allowed to have an empty condition inside the for-loop, for example as in for (;;) or for (int x = 0;; ++x). But you can't do while ().

When condition is omitted inside the for loop, condition is assumed to be true (so the loop loops forever). Why isn't this the case with while loops, that is, what's the argument behind not letting while () be an alias of while (true)?

like image 837
Luka Mikec Avatar asked Feb 11 '14 20:02

Luka Mikec


People also ask

Does a while loop require a condition?

A "While" Loop is used to repeat a specific block of code an unknown number of times, until a condition is met. For example, if we want to ask a user for a number between 1 and 10, we don't know how many times the user may enter a larger number, so we keep asking "while the number is not between 1 and 10".

What is the condition of a while loop?

The while loop is used when we don't know the number of times it will repeat. If that number is infinite, or the Boolean condition of the loop never gets set to False, then it will run forever.

Does for loop run only when the condition is true?

A for loop works as long as the condition is true. The flow of control, at first comes to the declaration, then checks the condition. If the condition is true, then it executes the statements in the scope of the loop. At last, the control comes to the iterative statements and again checks the condition.

What is the condition for a while loop to repeat?

while loop is executed once before the test expression is checked. The syntax of repeat..while loop is: repeat { // body of loop } while (condition) Here, The body of the loop is executed at first. Then condition is evaluated.


2 Answers

Presumably, it's a side-effect of the fact that each given clause within the for-statement is optional. There's reasons why some for-loops wouldn't need an assignment; there's reasons why some others wouldn't need a condition; there's reasons why still others wouldn't need an increment. Requiring there to be some minimum number of them would be needlessly-added complexity.

like image 102
Sneftel Avatar answered Oct 19 '22 03:10

Sneftel


The following is from the book Deep C Secrets:

Early C had no separate operators for & and && or | and ||. (Got that?) Instead it used the notion (inherited from B and BCPL) of "truth-value context": where a Boolean value was expected, after "if" and "while" and so forth, the & and | operators were interpreted as && and || are now; in ordinary expressions, the bitwise interpretations were used. It worked out pretty well, but was hard to explain. (There was the notion of "top-level operators" in a truth-value context.)

So there you have it, while() would set the context for a truth value and can't be empty or implied for the same reason if() can't be empty, the mechanics of the early language expected them not to be.

like image 42
Nikos Athanasiou Avatar answered Oct 19 '22 05:10

Nikos Athanasiou