Where to use braces

Discussion in 'C' started by usama_muneeb, Mar 16, 2008.

  1. usama_muneeb

    usama_muneeb Banned

    Joined:
    Mar 7, 2008
    Messages:
    25
    Likes Received:
    0
    Trophy Points:
    0
    I am learning C by the LPE Programming in C book by Stephen Kochan. There are some confusing things in the sample programs. For instance, somewhere, with the for statement, braces have been used like this:

    Code:
    for ( conditions ) {
              expressions;
    }
    While somewhere, they are not:

    Code:
    for ( conditions )
              expressions;
    Where to use them and where not?
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,374
    Likes Received:
    388
    Trophy Points:
    83
    When you have one statement in the loops / conditional statement you do not need to use the braces
     
  3. usama_muneeb

    usama_muneeb Banned

    Joined:
    Mar 7, 2008
    Messages:
    25
    Likes Received:
    0
    Trophy Points:
    0
    Thanks, useful.
     
  4. asadullah.ansari

    asadullah.ansari TechCake

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA
    If one statement in the loops / conditional statement then it does not matter u are putiing braces or not.

    But strong recommendation is that put braces like this

    while(conditions)
    {
    expression ;
    }

    In all excellent industries recommend this.
    Use of it is that No Confusion( If big and too much complex single expression) and easy to see the values by debugging)

    Example:

    while(condition)
    AgprsLink_mp = (AgprsMotLink *) ( InputDataSubLink) *Function(agprs_val,
    pVal_m,
    (PcmLinkDataRes_m *)( nData),
    (PacketFormet_m* ) (nLink) );


    this is simple case, I did'nt remember, I saw manyplace too much complex sinlgle expression and it will be confusion. So better use braces for any loop with a single or more than one statements.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice