is it possible to double a command without a loop?

This is a discussion on is it possible to double a command without a loop? within the C Programming forums, part of the General Programming Boards category; if I have an int with a value and I want to make a command repeat itself that values times? ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    9

    is it possible to double a command without a loop?

    if I have an int with a value and I want to make a command repeat itself that values times?

    and is there a command regarding even and uneven numbers?

    and does anybody know if it's possible to redefine an expression?
    e.g
    if at the beginning of my code i defined abc as +
    can I redefine it to - at another point in my code?
    Last edited by NA84; 11-04-2010 at 04:13 PM.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    Quote Originally Posted by NA84 View Post
    if I have an int with a value and I want to make a command repeat itself that values times?
    goto if you must tho' it is best avoided.
    Quote Originally Posted by NA84 View Post
    and is there a command regarding even and uneven numbers?
    Perhaps you mean the modulo operator.
    Quote Originally Posted by NA84 View Post
    and does anybody know if it's possible to redefine an expression?
    e.g
    if at the beginning of my code i defined abc as +
    can I redefine it to - at another point in my code?
    First define, then undefine, then re-define.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    107
    Quote Originally Posted by itCbitC View Post
    goto if you must tho' it is best avoided.
    Maybe for that particular purpose, but there are good uses of goto.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by NA84 View Post
    if I have an int with a value and I want to make a command repeat itself that values times?
    Believe me, you're far better to use a loop than anything else.
    Goto will work, but it's generally frowned upon and a real pain to debug.

    and is there a command regarding even and uneven numbers?
    If you are using integer values, test bit 0... 1 is odd, 0 is even.
    Code:
    // true if odd
    bool IsOdd(int Value)
      { return Value & 1; }
    and does anybody know if it's possible to redefine an expression?
    e.g
    if at the beginning of my code i defined abc as +
    can I redefine it to - at another point in my code?
    If you are just looking to change the sign of the variable you can use x = 0-x; to flip between a positive number and a negative one.

    If you mean to flip an equation from adding to subtracting... that's self modifying code which, even amongst bad ideas, is a really bad idea. It would make more sense to use some conditional term to decide whether to add or subtract.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by NA84 View Post
    if I have an int with a value and I want to make a command repeat itself that values times?
    A recursive function? Something like this:

    Code:
    #include <stdio.h>
    
    void hello( int i )
    {
        if ( i != 0 )
        {
            hello( i - 1 );
        }
    
        puts( "Hello\n" );
    }
    
    int main()
    {
        hello( 5 );
        return 0;
    }
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    9
    thank you all.
    one more question...
    if i have a counter and I need to printf one thing when the counter is a prime number and a different thing if it's not a prime no'. Do you have a good idea for that??

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by NA84 View Post
    thank you all.
    one more question...
    if i have a counter and I need to printf one thing when the counter is a prime number and a different thing if it's not a prime no'. Do you have a good idea for that??
    Obviously you need some algorythm to decide if a number is prime or not.

    I'm thinking you need to try writing this program yourself, then if you run into trouble you can post your code here and we'll take a look at it for you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Travel Expenses-Weird Result
    By taj777 in forum C++ Programming
    Replies: 4
    Last Post: 04-06-2010, 03:23 PM
  2. Functions, have errors...NEED HELP FAST
    By alkamenes in forum C++ Programming
    Replies: 6
    Last Post: 11-02-2009, 03:00 PM
  3. Help me please.
    By yann in forum C Programming
    Replies: 15
    Last Post: 09-29-2009, 10:04 PM
  4. C++ to C Conversion
    By dicon in forum C Programming
    Replies: 7
    Last Post: 06-11-2007, 09:38 PM
  5. functions and passing data
    By redmondtab in forum C Programming
    Replies: 41
    Last Post: 09-21-2006, 01:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21