Thread: decrementing for loop help needed

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    3

    Lightbulb decrementing for loop help needed

    hiya

    Got a problem with a for loop
    for example
    Say I want it to start at 10, test for at less than 5 and decrement each time before printing, this has been driving me mad.

    this is what i thought it would be

    for(x=10; x<5; x--)

    printf("%d", x); etc

    when i do this for x is greater than 5 it works fine i.e x>5 and prints 109876 but for x<5 prints nothing any ideas.

    ad

  2. #2
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    start at 10, test for at less than 5
    10 is not less than 5, so the loop condition fails immediately

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    3
    this is a question on a practise exam paper

    this is exactly what it says.....

    write a for statement which assigns x the value of 10, test if less than 5, and decrement each time the for loop is executed.????

    do you not think it can be done then???

    ad

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Yes it can be done.
    Yes, you've just written it successfully.
    Yes, you've just found out that it runs zero times.

    An odd question for sure, but your code matches the description.

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    3
    thanks it is really strange tho not sure if the q is a misprint

  6. #6
    Registered User
    Join Date
    Feb 2005
    Posts
    19
    What is the test for.

    Clarify that, and try nesting an "if" loop to achieve the desired result.

    Good luck.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. free needed or not?
    By quantt in forum Linux Programming
    Replies: 3
    Last Post: 06-25-2009, 09:32 AM
  2. C Programmers needed for Direct Hire positions
    By canefan in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 09-24-2008, 11:55 AM
  3. C++ help needed
    By Enkindu in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 08-31-2004, 11:24 PM