how for loop works ...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ponnuvel9
    New Member
    • Oct 2007
    • 1

    #1

    how for loop works ...

    for(i=0;0;i++)
    {
    printf("Its me");
    }

    This code prints the string "Its me" only once.
    How it works?
    Because, the condition should fail, even for the first time rite?
  • cornfed
    New Member
    • Oct 2007
    • 3

    #2
    The middle value, 0 in ur example, should have a equation (Like i=0) In the first i=0 u just specify the fact that i starts counting at 0.

    Originally posted by ponnuvel9
    for(i=0;0;i++)
    {
    printf("Its me");
    }

    This code prints the string "Its me" only once.
    How it works?
    Because, the condition should fail, even for the first time rite?

    Comment

    • amitpatel66
      Recognized Expert Top Contributor
      • Mar 2007
      • 2358

      #3
      Originally posted by ponnuvel9
      for(i=0;0;i++)
      {
      printf("Its me");
      }

      This code prints the string "Its me" only once.
      How it works?
      Because, the condition should fail, even for the first time rite?
      Yes because you have said 0 in the for loop.
      try below to print the message 9 times:

      [code=c]
      for(int i=0;i<10;i++)
      {
      printf("Its me");
      }
      [/code]

      Can you see the change in my code and your code??

      Comment

      • amitpatel66
        Recognized Expert Top Contributor
        • Mar 2007
        • 2358

        #4
        Originally posted by ponnuvel9
        for(i=0;0;i++)
        {
        printf("Its me");
        }

        This code prints the string "Its me" only once.
        How it works?
        Because, the condition should fail, even for the first time rite?
        The for loop prints the value once becuase the second value 0 is checked after the value of i is incremented first time.
        when i becomes 1, it checks for condition which returns 0 (ie false) and exits the loop

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          This code:

          Originally posted by weaknessforcats
          int i;
          for(i=0;0;i++)
          {
          printf("Its me");
          }
          prints nothing. (Using Visual Studio.NET 2005).

          The int i is set to 0 and then tested. 0 is false. Loop terminated.

          Who says its displays once?? Did you use a do loop?? Is this a test ??

          Comment

          Working...