Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. I could not understand loop testcondition understanding| rest program runs fine

I could not understand loop testcondition understanding| rest program runs fine

Scheduled Pinned Locked Moved C / C++ / MFC
json
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Member_14849246
    wrote on last edited by
    #1

    #include
    void main()
    {char name[40];
    printf("Enter the string:");
    gets(name);
    char ch = ' ';
    int i,count=0;
    for(i=0;name[i];i++)
    {if(name[i]==' ')
    count++;
    }
    printf("Number of words is %d",(count)+1);
    }

    Greg UtasG L 2 Replies Last reply
    0
    • M Member_14849246

      #include
      void main()
      {char name[40];
      printf("Enter the string:");
      gets(name);
      char ch = ' ';
      int i,count=0;
      for(i=0;name[i];i++)
      {if(name[i]==' ')
      count++;
      }
      printf("Number of words is %d",(count)+1);
      }

      Greg UtasG Offline
      Greg UtasG Offline
      Greg Utas
      wrote on last edited by
      #2

      This is something I hate, but it's idiomatic. A C string ends with the character NUL, whose ASCII value is 0, which is equivalent to false. So the loop ends when the character NUL (usually written as '\0') is encountered. The check is equivalent to

      name[i] != '\0'

      but some people hate typing so much that they write this kind of thing.

      Robust Services Core | Software Techniques for Lemmings | Articles

      <p><a href="https://github.com/GregUtas/robust-services-core/blob/master/README.md">Robust Services Core</a>
      <em>The fox knows many things, but the hedgehog knows one big thing.</em></p>

      1 Reply Last reply
      0
      • M Member_14849246

        #include
        void main()
        {char name[40];
        printf("Enter the string:");
        gets(name);
        char ch = ' ';
        int i,count=0;
        for(i=0;name[i];i++)
        {if(name[i]==' ')
        count++;
        }
        printf("Number of words is %d",(count)+1);
        }

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        The for statement breaks down as three individual expressions:

        for(set; while; do) :
        //
        // set : perform this or these expressions first: multiple expressions must be separated by commas
        // while : repeat the loop while this expression equates to true
        // do : perform this or these expressions at the end of each loop

        Note that any of these expressions (or indeed all of them) may be blank. In your code the expression in the while part is name[i], which means while name[i] is true, or rather while name[i] is not equal to zero. Strings in C are (or should be) terminated with a zero character (NULL), so the expression will be true as long as the character in question is not the terminating NULL.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups