Thread: for loop without a body

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

    for loop without a body

    I need to get the factorial of the number n inputted by the user and I have to use the for loop without a body to get the factorial. But in that case the scope of the variable k is the for loop, and when I try to print the k after the loop, it prints 1 (i. e. the value as k was initialized). What can I do?

    Code:
    #include <stdio.h>
    int main(void) {
    	int n, k = 1;
    	scanf("%d", &n);
    	for (int i = 1, k; i <= n; k *= i, i++);
    	printf("%d", k);
    	return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,403
    Remove the declaration of k from the loop.

    Anyway, this is a bad idea: the updating of k logically belongs in the loop body, so taking it out just to have more concise code is pointless. Sometimes, you will find that there really is no reason to have anything in the loop body, in which case go ahead and make it empty.
    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
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Just remove the k

    E.g.

    Code:
    for (int i = 1; i <= n; k *= i, i++)
        ;
    That will work, but even better would be to avoid using the comma operator for no (good) reason

    Code:
    for (int i = 1; i <= n; i++)
        k *= i;
    Edit: Notice in the first example I gave I moved the semicolon to a line of its own and indented it? This is not for the compiler (which doesn't care). This is for readability for both you and other human beings. It makes it clear that "yes, I meant this loop to have an empty body" and didn't accidentally put the semicolon there. Some compilers (clang at least, I haven't even checked gcc but I suspect it now behaves the same) will warn if the semi-colon isn't on the next line and at a larger indentation level than the actual for(...)
    Last edited by Hodor; 11-28-2015 at 01:29 AM.

  4. #4
    Registered User
    Join Date
    Nov 2015
    Posts
    82
    Thanks Hodor and laserlight, removed the k and it worked fine! And I know that I could write the k *= i; part inside the body of for but I needed to achieve the result with an empty-body-for.

  5. #5
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by lmanukyan View Post
    Thanks Hodor and laserlight, removed the k and it worked fine! And I know that I could write the k *= i; part inside the body of for but I needed to achieve the result with an empty-body-for.
    Well, you could still do it without using the comma operator. E.g.

    Code:
        for (int i = n + 1; --i > 1; k *= i)
            ;
    Although I will concede that this is just as obfuscated (perhaps more so, depending on your point of view) and pointless as using the comma operator. But if it's for a class exercise or something then I guess ya just have to go with the flow.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Does any body use Z ?
    By jabka in forum C Programming
    Replies: 5
    Last Post: 10-19-2007, 02:23 PM
  2. Any body know....
    By swgh in forum Game Programming
    Replies: 1
    Last Post: 01-25-2006, 05:41 AM
  3. Where is the body
    By siavoshkc in forum C++ Programming
    Replies: 11
    Last Post: 01-19-2006, 04:52 AM
  4. can some body help
    By mona in forum Game Programming
    Replies: 5
    Last Post: 11-12-2002, 04:58 PM

Tags for this Thread