I read the following in a review of Knuth's "The Art of Computer Programming":
"The very 'practicality' means that the would-be CS major has to learn Kernighan's mistakes in designing C, notably the infamous fact that a for loop evaluates the for condition repeatedly, which duplicates while and fails to match the behavior of most other languages which implement a for loop."
(http://www.amazon.com/review/R9OVJAJQCP78N/ref=cm_cr_pr_viewpnt#R9OVJAJQCP78N)
What is this guy talking about? How could you implement a for loop that wasn't just syntactic sugar for a while loop?
Almost all the programming languages provide a concept called loop, which helps in executing one or more statements up to a desired number of times. All high-level programming languages provide various forms of loops, which can be used to execute one or more statements repeatedly.
C is a Procedural Oriented language, whereas C++ is an Object-Oriented Programming language. C supports only Pointers whereas C++ supports both pointers and references. C does not allow you to use function overloading whereas C++ allows you to use function overloading.
For loops are used for sequential traversal. For example: traversing a list or string or array etc. In Python, there is no C style for loop, i.e., for (i=0; i<n; i++). There is “for in” loop which is similar to for each loop in other languages.
Java Simple for LoopA simple for loop is the same as C/C++. We can initialize the variable, check condition and increment/decrement value.
Consider this:
for i:=0 to 100 do { ... }
In this case, we could replace the final value, 100, by a function call:
for i:=0 to final_value() do { ... }
... and the final_value
-function would be called only once.
In C, however:
for (int i=0; i<final_value(); ++i) // ...
... the final_value
-function would be called for each iteration through the loop, thus making it a good practice to be more verbose:
int end = final_value();
for (int i=0; i<end; ++i) // ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With