Thread: Why 1 is ignored in this for loop?

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    2

    Why 1 is ignored in this for loop?

    I'm taking the book "C programming, a modern approach 2nd edtiion" and for the code/exercise below, why exactly the "1" is not being printed?

    I get "5 4 3 2" as output, but then why not 1 as well? What am I potentially missing here?

    Code:
    #include <stdio.h>
    
    int main (void)
    {
    int i, j;
    for (i = 5, j = i - 1; i > 0, j > 0; --i, j = i - 1)
    {
            printf("%d ", i);
    
    
    }
      return 0;
    }

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,628
    The loop ends when either i or j reaches 0. Since j starts 1 lower than i, it reaches 0 before i. Since you are only printing the value of i you see 5, 4, 3, 2.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Nov 2019
    Posts
    2
    Quote Originally Posted by john.c View Post
    The loop ends when either i or j reaches 0. Since j starts 1 lower than i, it reaches 0 before i. Since you are only printing the value of i you see 5, 4, 3, 2.
    That's it, thank you very much!

  4. #4
    Registered User
    Join Date
    Aug 2019
    Location
    inside a singularity
    Posts
    308
    - Please indent your code
    - Dry run the code on pen and paper

    Your for loop run condition is (i > 0 , j > 0). There is ",", and this case the compiler uses the right-most condition as the loop run condition.

    So your loop is the same as:
    Code:
    for (i = 5 , j = i - 1; j > 0; --i , j = i - 1)
    So, you basically start of with i = 5, j =4.

    1st run: i = 5, j = 4
    2nd run: i = 4, j = 3
    3rd run: i = 3, j = 2
    4th run: i = 2, j = 1

    Now, i =1 and j = 0 which violates loop condition and you don't get the 1 printed.
    Last edited by Zeus_; 11-30-2019 at 01:34 PM.

  5. #5
    Registered User
    Join Date
    Aug 2019
    Location
    inside a singularity
    Posts
    308
    > The loop ends when either i or j reaches 0. Since j starts 1 lower than i, it reaches 0 before i. Since you are only printing the value of i you see 5, 4, 3, 2.

    Isn't the right-most condition taken as your loop condition? I remember reading that somewhere so please correct me if I'm wrong.

    Going by what you said, (i > 0 , j > 0) is the same as (i > 0 && j > 0).

  6. #6
    Registered User
    Join Date
    Dec 2017
    Posts
    1,628
    @Zeus, you are absolutely correct! I made a mistake there, somehow reading the comma operator as a &&.
    But because it's the comma operator, only the second operand (j > 0) is effective.
    Thanks for the correction.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  7. #7
    Registered User
    Join Date
    Aug 2019
    Location
    inside a singularity
    Posts
    308
    Haha, you got me bothered for a few moments there lol. I thought I was learning everything wrong the right way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 09-22-2016, 09:08 AM
  2. Replies: 1
    Last Post: 03-28-2015, 08:59 PM
  3. Help - Collect data from Switch loop inside While loop
    By James King in forum C Programming
    Replies: 15
    Last Post: 12-02-2012, 10:17 AM
  4. Replies: 23
    Last Post: 04-05-2011, 03:40 PM
  5. for loop ignoring scanf inside loop
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-17-2007, 01:46 AM

Tags for this Thread