For loop

This is a discussion on For loop within the C++ Programming forums, part of the General Programming Boards category; Hi I am not quite sure what this for loop means Code: for (sum=a[i][j], k=i-1; k>=0; k--) sum -= a[i][k]; ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    3

    For loop

    Hi I am not quite sure what this for loop means

    Code:
    for (sum=a[i][j], k=i-1; k>=0; k--) sum -= a[i][k];
    Why is there a sum=a[i][j] in the for loop ? Is it initiated every single time or how does this work ??

    Thanks,

    Marcus

  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,490
    Is it initiated every single time or how does this work ??
    NO, it isn't .
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    3
    Can you explain more on that ? Sorry I am new to C++.

    So would it be
    Code:
    for (k=i-1; k>=0; k--) {
    sum=a[i][j]
    sum -= a[i][k];
    }
    or
    Code:
    sum=a[i][j]
    for (k=i-1; k>=0; k--) {
    sum -= a[i][k];
    }

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    And to programming.....whoever wrote this line of code was just being lazy and decided to keep everything on the same line. That line of code is equal to:
    Code:
    sum = a[i][j];
    for(k= i-1; k > 0; k--){
        sum = sum - a[i][k];
    }
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    If you are new to for loops, take a look at cprogs tutorial on loops
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  6. #6
    Registered User
    Join Date
    Jul 2011
    Posts
    3
    Thanks. The reason I was confused was because the sum=a[i][j] in the for loop. Usually for loop only has a intialization, condition and increment but not the first term. Anyways, thanks !

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,261
    Quote Originally Posted by arbitel View Post
    Thanks. The reason I was confused was because the sum=a[i][j] in the for loop. Usually for loop only has a intialization, condition and increment but not the first term. Anyways, thanks !
    It does. The trick is that these are separated by semicolons.
    The bit before the first semicolon runs once, at the beginning.
    The bit between the semicolons is the continuation condition and it runs at the start of every loop.
    The part after the second semicolon is the incrementing condition and is run at the end of every iteration of the loop.

    What you had was two pieces of code in the initialisation part of the for-loop, which is possible when using the comma operator.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 23
    Last Post: 04-05-2011, 03:40 PM
  2. The Infinit loop that doesn't loop.
    By errigour in forum C Programming
    Replies: 1
    Last Post: 11-09-2010, 10:31 AM
  3. for loop ignoring scanf inside loop
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-17-2007, 01:46 AM
  4. loop the loop - feeling foolish
    By estos in forum C Programming
    Replies: 2
    Last Post: 04-07-2007, 02:45 AM
  5. Replies: 3
    Last Post: 03-14-2006, 10:09 AM

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