Thread: For Loop understanding

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    12

    For Loop understanding

    This program gives 2 2 2 as output but i don't get how? According to my understanding,
    Initialization Cond Increment
    0 true 1 so 1+2=3 should be printed then j=2-1=1 used
    insert
    Code:
    
    #include<stdio.h>
    voidmain(){
    inti,j=2;
    
    for(i=0;i<=5,j>=0;i++){
             printf("%d ",i+j);
             j--;
        }
    
    }





    And this program gives 2 2 2 2 2 . How does order in condition part affect output?
    insert
    Code:
    
    #include<stdio.h>
    int main(){
    inti,j=2;
    
    for(i=0;j>=0,i<=5;i++){
             printf("%d ",i+j);
             j--;
        }
    return0;        
    }


  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Note that void main should be int main, and consequently unless you are compiling with respect to C99 or later, you should return 0; at the end of the main function. Along with consistent indentation, formatting tweaks, and fixes to the markup by posting in plain text, your code would look like:
    Code:
    #include<stdio.h>
    
    int main(void) {
        int i, j = 2;
    
        for (i = 0; i <= 5, j >= 0; i++) {
            printf("%d ", i + j);
            j--;
        }
    
        return 0;
    }
    Quote Originally Posted by Izzy98
    How does order in condition part affect output?
    The comma operator is in effect. In other words, this condition:
    Code:
    i <= 5, j >= 0
    is equivalent to this condition:
    Code:
    j >= 0
    Likewise this condition:
    Code:
    j >= 0, i <= 5
    is equivalent to this condition:
    Code:
    i <= 5
    because the left hand side operand of the comma expression is evaluated, then the right hand side operand of the comma expression is evaluated, and the result of the expression is the result of that right hand side operand.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-28-2015, 08:59 PM
  2. 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
  3. Replies: 23
    Last Post: 04-05-2011, 03:40 PM
  4. Having trouble understanding 'for' loop parameters
    By spottedzebra in forum C Programming
    Replies: 11
    Last Post: 06-23-2010, 08:23 PM
  5. Question understanding casted loop
    By elwad in forum C Programming
    Replies: 14
    Last Post: 05-23-2009, 04:13 PM

Tags for this Thread