Thread: Help me understand the logic of the code?

  1. #1
    Registered User
    Join Date
    Jun 2017
    Posts
    11

    Wink Help me understand the logic of the code?

    If anyone can, Please help me out understand the logic of the code and most likely how it works. I was able to come up with it somehow but without knowing proper back processing, It is kinda weird to me. Just posting the part where it is confusing. And it is about counting the digits in entered number(I gave count value of 0 before hand already).

    Code:
     
    for(;n<0;)
    {
    count=count+1;
    n=n/10;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Suppose the code was written like this instead:
    Code:
    while (n < 0)
    {
        count = count + 1;
        n = n / 10;
    }
    would you understand it?

    By the way, I think you have a typo: n < 0 probably should have been n > 0
    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

  3. #3
    Registered User
    Join Date
    Jun 2017
    Posts
    11
    Quote Originally Posted by laserlight View Post
    Suppose the code was written like this instead:
    Code:
    while (n < 0)
    {
        count = count + 1;
        n = n / 10;
    }
    would you understand it?

    By the way, I think you have a typo: n < 0 probably should have been n > 0
    I meant the process that goes in the background to understand the language better.
    Also, You had put up a valid typo. Now I just checked, I just made mistake when typing the code in hurry. A silly mistake.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Orcus
    I meant the process that goes in the background to understand the language better.
    In this case it is just a matter of fully understanding a for loop. You could look up tutorials etc that break down the components of a for loop for you, and thereby understand what this particular for loop means.
    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

  5. #5
    Registered User
    Join Date
    Jun 2017
    Posts
    11
    Quote Originally Posted by laserlight View Post
    In this case it is just a matter of fully understanding a for loop. You could look up tutorials etc that break down the components of a for loop for you, and thereby understand what this particular for loop means.
    Ah, I understand what you mean. I will do that. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 07-16-2016, 04:14 AM
  2. What gets printed? Trying to understand the logic.
    By Joie Moie in forum C Programming
    Replies: 1
    Last Post: 05-02-2013, 07:30 PM
  3. Coding why wrong/logic cant understand
    By raihan004 in forum C Programming
    Replies: 6
    Last Post: 10-02-2012, 04:16 AM
  4. Recursive function, trying to understand the logic.
    By csharp100 in forum C Programming
    Replies: 7
    Last Post: 11-16-2010, 11:38 AM
  5. Replies: 21
    Last Post: 10-14-2006, 04:38 AM

Tags for this Thread