semi colon, the difference in loop without body

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lyle777
    New Member
    • Apr 2010
    • 8

    #1

    semi colon, the difference in loop without body

    Hi everyone

    the book that i'm learning from says that this for loop has no body,
    first of all, I don't even no what that means, this for loop looks quit
    the usual.

    besides the above issue another bazaar thing is that all other for loops
    do not have a semi-colon after the brackets, this one does and if
    i remove it i get a funny output, if i place the semi-colon there i get
    SUM IS 55 as an output and I dont understand why.
    help please.

    #include <iostream>
    #include <cstdlib>
    using namespace std;
    int main()
    {
    int i, sum = 0;

    //sum the numbers from 1 to 10
    for(i=1; i <= 10; sum += i++);
    cout << "sum is " << sum;

    cin.get();
    cin.get();
    return 0;
    }
  • Dheeraj Joshi
    Recognized Expert Top Contributor
    • Jul 2009
    • 1129

    #2
    If you end the loop with semicolon that means end of line

    if you want some calculation to be performed for say 10 times. Your loop should looks like this
    Code:
    for(i=0;i<10;i++)
    {
    //Your code
    }
    sum = 55 is a correct answer
    Traverse your loop. Loop is calculating the value correctly.

    Code:
    sum = sum + i
    sum = 0+1;
    sum = 1+2;
    sum = 3+3;
    sum = 6+4;
    sum = 10+5;
    sum = 15+6;
    sum = 21+7;
    sum = 28+8;
    sum = 36+9;
    sum = 45+10;
    Look into your for loop
    Code:
    for(i=1; i <= 10; sum += i++)
    I think it should not be
    Code:
    sum+=i++
    it should be
    Code:
    for(i=1;i<=10;i++)
    Regards
    Dheeraj Joshi

    Comment

    • lyle777
      New Member
      • Apr 2010
      • 8

      #3
      thanks alot man

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        The semicolon only looks like it is at the end of the for statement. Actually it is the body of the for statement -- a body that accomplishes nothing. I disagree with saying the loop "has no body"; I think it is more accurate to say that the loop has a null or empty body. (You might prefer not to further overload the word null.) The following code snippets are all equivalent:
        Code:
        for(i=1; i <= 10; sum += i++);
        
        for(i=1; i <= 10; sum += i++)
           ;
        
        for(i=1; i <= 10; i++)
            sum += i;
        By the way, there ought to be an initialization statement prior to the loop that sets sum to zero. You could embed that initialization statement in the for instruction. These snippets are equivalent:
        Code:
        sum = 0;
        for(i=1; i <= 10; sum += i++);
        
        for(sum=0,i=1; i <= 10; sum += i++);

        Comment

        • jkmyoung
          Recognized Expert Top Contributor
          • Mar 2006
          • 2057

          #5
          At dheerajjoshim.
          The loop IS correct, if you read the definition of what the program is supposed to do.

          I find calling the loop empty deceiving, as these are equivalent:
          Code:
          for(i=1; i <= 10; sum += i++) ;
          OR
          i=1;
          for( ; i <= 10; ){
            sum+= i;
            i++; 
          }
          In c code the loop 'looks' empty.
          --
          In compiled machine code they would look the same. The loop is most definitely not empty, in strict loop sense. As a side note, for loops can be converted to while loops like so, where a, and c are statements, and b is a comparison.
          Code:
          for(a;b;c){
            //loop contents
          }
          becomes
          a;
          while(b){
            //loop contents
            c;
          }

          Comment

          • donbock
            Recognized Expert Top Contributor
            • Mar 2008
            • 2427

            #6
            I didn't say the loop was empty. I said the body of the loop was empty; where body has a strict syntactical meaning.

            Compare line 1 and lines 6-7 of the first code block in my earlier reply. These two snippets have equivalent semantics [meaning] but different syntax [expression].

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #7
              Originally posted by jkmyoung
              where a, and c are statements, and b is a comparison.
              Code:
              for(a;b;c){
                //loop contents
              }
              }
              Strictly a, b and c are all expressions. An expression is any statement that returns a value. B does not have to be a comparison for instance

              Code:
              int i, sum=0;
              for(i=11; i -= 1; sum += i) 
                  ;
              cout << sum << endl;
              Not that I would recommend that syntax

              Comment

              • Dheeraj Joshi
                Recognized Expert Top Contributor
                • Jul 2009
                • 1129

                #8
                Yes my bad. Loop is correct.
                But it is always prefered to write your code clearly.

                Rather than adding
                Code:
                sum+=i++
                in for loop.
                Add
                Code:
                sum = sum +i
                This increases program readability. It really helps the guy who is maintaining the program.

                Regards
                Dheeraj Joshi

                Comment

                • donbock
                  Recognized Expert Top Contributor
                  • Mar 2008
                  • 2427

                  #9
                  Which is better:
                  Code:
                  sum += i;
                  or
                  sum = sum + i;
                  Reasonable people can disagree. Personally, I prefer the += form for the following reasons:
                  1. I immediately know the variable on the left side of the equals sign is being incremented. There is no need to compare the variable names on each side of the equals.
                  2. Typing in the variable name twice introduces the small risk there will be a typo in one of them. Suppose both sum and som exist in my program, is the following a typo or is it what I really want?
                  Code:
                  sum = som + i;

                  Which is better:
                  Code:
                  sum += i++;
                  or
                  sum += i;
                  i++;
                  Using pre- or post-increment in a larger expression is certainly tricky. However, you need to be able to accurately read this sort of code if you are going to use C professionally -- even if you don't intend to use it yourself.

                  In questions of style, I contend it is much more important to be consistent with norms of your coworkers than it is to eke out a marginal [and debateable] improvement through an innovative style.

                  Comment

                  • jkmyoung
                    Recognized Expert Top Contributor
                    • Mar 2006
                    • 2057

                    #10
                    Looking back at my previous post, I was concerned about a problem that did not exist, and thus sidetracked the thread a little.

                    sum+= i;
                    vs
                    sum = sum + i; ?

                    sum+= i; can be optimized by the compiler better. I almost always go with this option.

                    Comment

                    Working...