Deep C Dives: The Brilliant But Evil for
Written by Mike James   
Tuesday, 19 November 2024
Article Index
Deep C Dives: The Brilliant But Evil for
Conditional Loops
Break and Continue
The Evil Parts

The Evil Parts

The for is a flexible construct capable of being used to implement for loops and a range of different conditional loops. As such it is not a nice simple straightforward enumeration loop and is capable of great misuse.

As each part of the for loop can be an expression, the possibilities are great. For example, you can use three functions to control the loop:

for(init();test();inc()){

and obviously init performs the initialization, test returns a Boolean to control the loop and inc is evaluated at the end of the loop. In this form it is clear that the for loop can do almost anything and implement almost any type of loop with an exit point at the start.

In most cases it isn’t even necessary to resort to using functions to introduce statements into the mix. Often you can get away with just using expressions separated by a comma operator. For example:

for(i = 1,j = 9;j > 0;i++,j--){
    printf("%d %d ", i, j);
}

Notice that we initialize both i and j and that i increments and j decrements each time through the loop. The result is a loop in which i runs from 1 to 9 and j from 9 to 1. There are alternative, clearer, ways of writing this loop.

Final Thoughts

C for loops aren’t really enumeration loops and probably shouldn’t have “for” in their name. In practice, they encompass a wide range of loop types and as such they are dangerous from the point of view of code clarity. You will encounter many robust opinions that this form of loop or that form of loop is evil and should be avoided, and some are. However, what matters, and it matters in all code not just the for loop, is clarity of intent. If a for loop is a good expression of what you intend it to do, then it is a good for loop, no matter what anyone else says.

One way to ensure that this is the case, without having to spend too much time on evaluation, is to restrict yourself to only a small number of variations - for(i = 0;i < n;i++) is good.

Deep C Dives
Adventures in C

By Mike James

Cdive360

Buy from Amazon.

Contents

Preface
Prolog C
Dive

  1. All You Need Are Bits
  2. These aren’t the types you’re looking for
  3. Type Casting
  4. Expressions
  5. Bits and More Bits
  6. The Brilliant But Evil for ***NEW!
  7. Into the Void 
  8. Blocks, Stacks and Locals
  9. Static Storage
  10. Pointers
  11. The Array and Pointer Arithmetic
  12. Heap, The Third Memory Allocation
  13. First Class Functions
        Extract:
    First Class Functions
  14. Structs and Objects
  15. The Union
  16. Undefined Behavior
  17. Exceptions and the Long Jump

<ASIN:B0D6LZZQ8R>

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

Banner


Extend NGINX With The New JavaScript Module
28/10/2024

Inject middleware functionality into NGINX with the expressive power of Javascript. NGINX JavaScript or NJS for short is a dynamic module under which you can use scripting for hooking into the NGINX e [ ... ]



TypeScript Improves Never-Initialized Variables Checks
21/10/2024

Microsoft has announced TypeScript 5.7 in beta, with improvements including stronger checks for variables that have never been initialized before use, and path rewriting for relative paths.


More News

espbook

 

Comments




or email your comment to: comments@i-programmer.info



Last Updated ( Tuesday, 19 November 2024 )