Completing C code with D style

russhy russhy at gmail.com
Wed Nov 3 20:36:08 UTC 2021


The code "as is" is perfectly fine

I don't understand why you guys offer OP such complicate/bloated 
examples, it'll only make things confusing and slow down 
compilation time with templates and imports, this is not needed 
at all


One change, use .length property instead of the hardcoded value

Keeping things simple helps debugging!

```D
int main() {

   int numbers[10] = [-3, 14, 47, -49, -30, 15, 4, -82, 99, 26];
   char negativity, even;
   write("Would you like in list (n=negatives, p=positives, 
b=both)? ");
   readf(" %c", & negativity);

   write("Would you like in list (e=evens, o=odds, b=both)? ");
   readf(" %c", & even);

   // you can access .length property of 
slices/arrays/static_arrays in D
   for (int i = 0; i < numbers.length; ++i) {
     if (negativity == 'n') {
       if (numbers[i] > 0)
         continue;
     } else {
       if (negativity == 'p')
         if (numbers[i] < 0)
           continue;
     }

     if (even == 'e') {
       if (numbers[i] % 2)
         continue;
     } else {
       if (even == 'o')
         if (!(numbers[i] % 2))
           continue;
     }
     writefln("%d", numbers[i]);
   }

   return 0;
}
```


More information about the Digitalmars-d-learn mailing list