Thread: Difference between the outputs.

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    14

    Question Difference between the outputs.

    I am new to C programming. Please , tell me , why there is difference between the outputs of the two programs below.
    Code:
     #include<stdio.h>
    int main()
    {
      int i=0,j=0;
    
    
    
    
      for(;i<=20;i++)
         {
             for(;j<=10;j++)
                {
                         printf("%d\t%d\n",i,j);
                }
    
    
         }
         return 0;
      }
    Code:
     #include<stdio.h>
    int main()
    {
      int i,j;
    
    
    
    
      for(i=1;i<=20;i++)
         {
             for(j=1;j<=10;j++)
                {
                         printf("%d\t%d\n",i,j);
                }
    
    
         }
         return 0;
      }
    Last edited by psaikia; 12-05-2011 at 02:00 PM.

  2. #2
    Registered User
    Join Date
    Nov 2011
    Posts
    52
    Because i and j is initialized with different values.

    Does this looks different?

    Code 1:
    i = 0
    j = 0

    Code 2:
    i = 1
    j = 1

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    14
    That is ok

    But the outer loop is not working in the first one . ,, In the second one the outer loop and inner loop is working.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    88
    Quote Originally Posted by psaikia View Post
    That is ok

    But the outer loop is not working in the first one . ,, In the second one the outer loop and inner loop is working.
    In the first example, What is the initial value of j when i > 0 ?

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    14
    Quote Originally Posted by sparkomemphis View Post
    In the first example, What is the initial value of j when i > 0 ?
    Did not understand , what are you trying to mean,plz elaborate.

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    52
    This one got me too @@, The inner loop on the first code will only work once. j needs to be reinitialized to 1 or 0 on the inner loop so it will work properly.

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    14
    Quote Originally Posted by mnd22 View Post
    This one got me too @@, The inner loop on the first code will only work once. j needs to be reinitialized to 1 or 0 on the inner loop so it will work properly.
    Yeah you are correct ,In the 1st code if we reinitialize the j in second loop , it works.

    Can you explain me why it is like this ? thank you.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by psaikia View Post
    Yeah you are correct ,In the 1st code if we reinitialize the j in second loop , it works.

    Can you explain me why it is like this ? thank you.
    What is the value of j after the first pass through the inner loop?

    Since the first example (stupidly) removes the initializer from the for loops and places them outside both loops, the value of j will stay wherever the inner loop left it the first time through... Second time, j = 11... the loop does not run.

    I've seen this broken format of for loops quite frequently over the past few weeks. Your code demonstrates perfectly why it's broken!

  9. #9
    Registered User
    Join Date
    Nov 2011
    Posts
    52
    Your condition checks if j is <= 10. After the first loop j is 11 so without setting it to 0 again the result will always be false.

    It's a pretty simple code you should try running it through your mind or if you have a good IDE try stepping through the code in debug mode.

  10. #10
    Registered User
    Join Date
    Nov 2011
    Posts
    14
    Yeah , now I understand , Thank you ​commontater and mnd22

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Different outputs on different OS
    By darksifer in forum C Programming
    Replies: 13
    Last Post: 10-19-2010, 01:45 PM
  2. why i getting different outputs?
    By nkrao123@gmail. in forum C Programming
    Replies: 2
    Last Post: 12-08-2009, 08:33 AM
  3. rounding off outputs
    By lastresort in forum C++ Programming
    Replies: 5
    Last Post: 02-21-2005, 07:52 PM
  4. strings and outputs
    By Mule in forum C++ Programming
    Replies: 3
    Last Post: 03-02-2003, 05:39 PM
  5. Outputs
    By kas2002 in forum C++ Programming
    Replies: 3
    Last Post: 10-29-2002, 07:12 PM