A style question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Frederick Ding

    #1

    A style question

    Hi, guys!
    I have two questions of some kind of style problem:
    1. Is there a better one that can substitute this:
    while (1) {
    for (i = 0; i < 6; i++) {
    scanf("%d", &a[i]);
    }
    if ((a[0] == 0) && (a[1] == 0) && (a[2] == 0) && (a[3] == 0) &&
    (a[4] == 0) && (a[5] == 0))
    break;
    else {
    /* do other things */

    }


    }
    I doubt that the while(1) is decent or not?
    Can I find a better one than the "forever" loop?
    2. still in the code above:
    if (i == 0) break;
    else {


    }

    or

    if (i == 0) break;
    ...... /* do other things without else */

    which one is better?


    Thanks!


  • Bjørn Augestad

    #2
    Re: A style question

    Frederick Ding wrote:[color=blue]
    > Hi, guys!
    > I have two questions of some kind of style problem:
    > 1. Is there a better one that can substitute this:[/color]

    Yes, see below.
    [color=blue]
    > while (1) {
    > for (i = 0; i < 6; i++) {
    > scanf("%d", &a[i]);
    > }
    > if ((a[0] == 0) && (a[1] == 0) && (a[2] == 0) && (a[3] == 0) &&
    > (a[4] == 0) && (a[5] == 0))
    > break;
    > else {
    > /* do other things */
    >
    > }
    >
    >
    > }
    > I doubt that the while(1) is decent or not?
    > Can I find a better one than the "forever" loop?
    > 2. still in the code above:
    > if (i == 0) break;
    > else {
    >
    >
    > }
    >
    > or
    >
    > if (i == 0) break;
    > ...... /* do other things without else */
    >
    > which one is better?
    >[/color]

    Here's another way to write the code above. First of all we create a
    couple of functions, one that reads numbers and one that tests the array
    for emptiness. As you see, these are general functions operating on
    arrays of any size. The functions can be optimized later and you can
    even add error handling and asserts...

    int read_numbers(FI LE* f, int dest[], size_t nelem)
    {
    size_t i;
    for(i = 0; i < nelem; i++)
    fscanf(f, "%d", &dest[i]);
    return 1;
    }

    int empty_array(int arr[], size_t nelem)
    {
    size_t i;
    for(i = 0; i < nelem; i++)
    if(arr[i] != 0)
    return 0;

    return 1;
    }

    Back to your loop. No more breaks, only one magic number and no
    while(1). So much nicer, isn't it?
    ....
    int myarray[6];
    size_t nelem = sizeof myarray / sizeof *myarray;

    while(read_numb ers(stdin, myarray, nelem)
    && !array_empty(my array, nelem)) {
    /* Do stuff */
    }

    ....


    Boa

    Comment

    • Chuck F.

      #3
      Re: A style question

      Frederick Ding wrote:[color=blue]
      >
      > I have two questions of some kind of style problem:
      > 1. Is there a better one that can substitute this:
      > while (1) {
      > for (i = 0; i < 6; i++) {
      > scanf("%d", &a[i]);
      > }
      > if ((a[0] == 0) && (a[1] == 0) && (a[2] == 0) &&
      > (a[3] == 0) && (a[4] == 0) && (a[5] == 0))
      > break;
      > else {
      > /* do other things */
      >
      > }
      >
      >
      > }
      > I doubt that the while(1) is decent or not?
      > Can I find a better one than the "forever" loop?[/color]

      How about:

      do {
      for (i = 0, notdone = 1; i < COUNT; i++) {
      if (1 != scanf("%d", &a[i])) {
      notdone = 0; break;
      }
      notdone &= (a[i] != 0);
      }
      if (notdone) do_other_things ();
      while (notdone);

      Note the testing of scanf return, although the action on failure
      may not be what you want. scanf results should always be checked.

      The main point is that doing something and testing the result is
      better characterized by a do while loop. Also altering COUNT
      should not require nitty-gritty changes in the exit test.

      --
      Read about the Sony stealthware that is a security leak, phones
      home, and is generally illegal in most parts of the world. Also
      the apparent connivance of the various security software firms.
      This is my sixth column for Wired.com: It’s a David and Goliath story of the tech blogs defeating a mega-corporation. On Oct. 31, Mark Russinovich broke the story in his blog: Sony BMG Music Entertainment distributed a copy-protection scheme with music CDs that secretly installed a rootkit on computers. This software tool is run without your knowledge or consent—if it’s loaded on your computer with a CD, a hacker can gain and maintain access to your system and you wouldn’t know it. The Sony code modifies Windows so you can’t tell it’s there, a process called “cloaking” in the hacker world. It acts as spyware, surreptitiously sending information about you to Sony. And it can’t be removed; trying to get rid of it ...

      Comment

      • Boudewijn Dijkstra

        #4
        Re: A style question

        Frederick Ding wrote:[color=blue]
        > Hi, guys!
        > I have two questions of some kind of style problem:
        > 1. Is there a better one that can substitute this:[/color]

        Yes. If you don't like functions, I would suggest the following
        replacement to Bjørns beautiful piece of work:

        int a[6];
        int nZeros;

        while (1) {
        nZeros = 0;
        for (i = 0; i < 6; i++) {
        scanf("%d", &a[i]);
        if (a[i] == 0) {
        nZeros++;
        }
        }
        if (nZeros == 6) {
        break;
        }

        /* do other things */
        }
        [color=blue]
        > I doubt that the while(1) is decent or not?[/color]

        As long as the loop termination condition is obvious or as long as it is
        obviously meant to be an infinite loop. The meaning of obvious depends
        on the readers of your code.

        Comment

        • Christopher Benson-Manica

          #5
          Re: A style question

          Chuck F. <cbfalconer@yah oo.com> wrote:
          [color=blue]
          > do {
          > for (i = 0, notdone = 1; i < COUNT; i++) {
          > if (1 != scanf("%d", &a[i])) {
          > notdone = 0; break;
          > }
          > notdone &= (a[i] != 0);
          > }
          > if (notdone) do_other_things ();[/color]
          } /* You're still rusty :-) */[color=blue]
          > while (notdone);[/color]

          --
          Christopher Benson-Manica | I *should* know what I'm talking about - if I
          ataru(at)cybers pace.org | don't, I need to know. Flames welcome.




          Comment

          Working...