Thread: Compound Statement

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    13

    Compound Statement

    (what will be the output someone please help me out thanks)


    if (abs(x) < xmin) x = (x > 0) ? xmin : -xmin;

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    If you indent it, the truth will be revealed!
    Code:
    if (abs(x) < xmin)
       x = (x > 0) ? xmin : -xmin;
    The first line of code is simple.
    The second line of code says.
    If x>0, then assing xmin to x.
    If condition x>0 is false (thus x <= 0 is true ) , then assign -xmin to x.

    In other (code) words
    Code:
    if (abs(x) < xmin)
    {
         if(x > 0)
         {
                x = xmin;
         }
         else 
         {
                x = -xmin;
          }
    }
    Hope this helps
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    13
    hey thanks dear friend for explaining the code in detail but when i run it using values it does not give anything

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Why don't you post the code you tried so that we can see it too?
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    13
    #include<stdio.h>


    main()



    float xmin,x;
    x=5.6;


    if (abs(x) < xmin)

    if(x > 0)

    x = xmin;

    else

    x = -xmin;

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    13
    i did not include the braces as it does not accept here with braces assume i put the braces

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Ok first wrap your code in code tags next time It is easy. You write
    [key] /* Your code here */ [/key] Replace key with code in order this to work

    In your code you do not give a value to xmin, so your flow of code will not go into the if structure you have.

    Also if you want to see something put some printf's

    Finally you usually write
    Code:
    int main(void)
    {
           ....
           return 0;
    }
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compound Interest?
    By jbone0881 in forum C Programming
    Replies: 6
    Last Post: 02-25-2011, 11:07 PM
  2. need help with compound interest
    By JoelearningC in forum C Programming
    Replies: 4
    Last Post: 03-09-2008, 10:32 AM
  3. compound interest, need help
    By shaitan_superma in forum C Programming
    Replies: 8
    Last Post: 09-01-2007, 11:50 PM
  4. compound interest
    By bliznags in forum C Programming
    Replies: 11
    Last Post: 03-20-2005, 01:46 AM
  5. Overloading compound operators?
    By Nyda in forum C# Programming
    Replies: 2
    Last Post: 04-28-2004, 05:42 AM