Help with arrays without breaks.

This is a discussion on Help with arrays without breaks. within the C Programming forums, part of the General Programming Boards category; I am working on a program that reads from a *.dat file while using functions as well. My programming teacher ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    8

    Help with arrays without breaks.

    I am working on a program that reads from a *.dat file while using functions as well. My programming teacher says it's against good etiquette to use an "if" statement with a "break". I have the function completely written and functional with the break statement:

    Code:
    for (i = 0; i < MAX; i++)
    {
        scanf("%d", &ages[i]);
    
        if (ages[i] == 0)
        {
             break;
         }
    
    }
    I am not trying to get an easy grade for my class. I've read the rules and I understand the policy on homework. I've been struggling with a solution and I would appreciate some solutions to help me learn.
    Last edited by DoctorFu; 12-12-2005 at 02:30 PM.

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by DoctorFu
    IMy programming teacher says it is against good etique to use an if statement with a break.
    I do that all the time. But you could use a do .. while loop
    Code:
    int i = 0;
    do {
        scanf("%d", &ages[i++]);
    } while ( ages[i-1] != 0 && i < MAX );
    Kurt

  3. #3
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    Quote Originally Posted by DoctorFu
    My programming teacher says it's against good etiquette to use an "if" statement with a "break".
    That is a very odd thing for a teacher to have said. Did he or she say why it's bad programing "etiquette"? Using "break" within a loop without an "if" is probably not useful at all. What's your teacher's opinion of using "continue" with an "if"?
    Insert obnoxious but pithy remark here

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    8
    Quote Originally Posted by filker0
    That is a very odd thing for a teacher to have said. Did he or she say why it's bad programing "etiquette"? Using "break" within a loop without an "if" is probably not useful at all. What's your teacher's opinion of using "continue" with an "if"?
    First of all, thank you very much for the help.. I want to get better with C and everyone is so friendly here.

    As for my teacher, I think she means that it's borderline cheating (programmers are always lazy and users are always stupid right?). I think she just wants us to use the method you showed me.

    PS - I asked if the Do..While method is okay and she replied: "Use anything other than "break". You can only use break when using a switch." I will ask her what she thinks about "continue" (how exactly would you use "continue" in this case?). BTW, thanks again Zuk and filker0.

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,167
    As an alternative you could just rewrite your original for() loop like:
    Code:
    for (i = 0; i < MAX && (!i || ages[i - 1] != 0); i++)
    To me, the break is clearer in my opinion. I disapprove of blanket statements like "break should never be used in loops". Sure, there's times when it's abused and makes the loop harder to follow, but sometimes I think it's the better choice.
    Last edited by itsme86; 12-12-2005 at 05:32 PM.
    If you understand what you're doing, you're not learning anything.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Why stop there? DoctorFu, you should ask your instructor if it's even better to make the input part of the test too.
    Code:
    for (i = 0; i < MAX && scanf("%d", &ages[i]) == 1 && ages[i] != 0; i++);
    Then it's even more difficult to read and understand, and yet it does the same thing! (Well, it does it one better.)

    It must be better ettiquette to make code difficult for those that follow you to debug! Just think how much you'd rather debug this than what you first posted!
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    ---
    Join Date
    May 2004
    Posts
    1,379
    I would prefer not to use a break
    in fact I rarely do use breaks except in switch statement.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    One school of thought is that multiple returns, continues, and breaks are poor design. Your teacher appears to be one of these types. The idea is that your code should have one return point per function, and your loops be written in such a way that you never need to break out of them early. Now I'm not saying that that's right or wrong, just that that is how some people view things.

    You could easily fix your loop without over complicating it, or making fun ugly code. It's really quite simple:
    Code:
    for (i = 0; i < MAX; i++)
    {
        scanf("%d", &ages[i]);
    
        if (ages[i] == 0)
        {
             i = MAX -1;
         }
    
    }
    Look, Ma! No break!


    [edit catch="Nice Catch Dave!"]
    If MAX is *_MAX, such as INT_MAX, then i++ at the end of this loop's iteration, gives us:
    a) undefined overflow, on signed types.
    b) 0, due to unsigned overflow

    In either case, it won't be what we want.

    /salute
    Nice catch Dave.
    [/edit]

    [edit2]
    I blame any potential inaccuracies of the previous edit on beer. However, it appears at this time, that the edit is accurate.
    [/edit2]

    Quzah.
    Last edited by quzah; 12-13-2005 at 12:13 AM.
    Hope is the first step on the road to disappointment.

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,668
    If you don't like Dave's one-liner, try
    Code:
    int done = 0;
    for ( i = 0 ; i < MAX && !done ; i++ ) {
      done = scanf ( "%d", &ages[i] ) != 1;  /* we're done if input fails */
      done = done || ages[i] == 0;           /* also done if input was 0 */
    }

  10. #10
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    >>> "Use anything other than "break".

    As a joke, give her a solution with a goto, hey she said anything but a break!
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  11. #11
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    I'm willing to go out on a limb and dissagree with the anti-break-outside-of-switch crowd. The gyrations that you have to go through to avoid the use of "break" as a loop control in non-trivial cases can increase code complexity beyond any benifit you can garner by it. When dealing with boundary conditions (aka "corner conditions" or "edge conditions"), you can end up with some pretty awful code. "break" moves you outside (and to the end) of the innermost containing switch or loop. It always transfers control forward in the program logic. Unlike the much maligned "goto", it is difficult to abuse "break" in such a way as to make the code unreadable.

    The "continue" statement transfers control to the control statement of the inner-most containing loop construct (for, while, or do...while), thus short-circuiting the loop logic in another way.

    Now it may be that your teacher is actually trying to make sure you understand how to make non-trivial loop control (control based on multiple conditions). That is a good thing. But to avoid using "break" outside of a switch at all costs? I think that that actually promotes bad code design.

    As an amusing point, and without using "goto", you might ask your teacher about the following construct:
    Code:
    int done = 0;
    while (!done && [loop-condition])
    {
          ....
          if ([some-condition])
         {
            done = 1;
            continue;
         }
         ....
    }
    Insert obnoxious but pithy remark here

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function to read in two arrays
    By ssmokincamaro in forum C Programming
    Replies: 7
    Last Post: 11-12-2008, 06:59 AM
  2. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 03:24 PM
  3. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  4. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 09:47 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 08:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21