Thread: if else statement problem two

  1. #1
    Registered User loserone+_+'s Avatar
    Join Date
    Dec 2012
    Location
    Indonesia
    Posts
    112

    Exclamation if else statement problem two

    look at this code below
    Code:
    printf(“range =”);scanf(“%i”,&range);
    printf(“target =”);scanf(“%i”,&target);
    if( ( (range==target)>0) && ( (range==target)<10) )
    when i compile this code, it isn’t the expected output i want
    example, range=12 and target=12, it will go to else not if true
    simple way says, the range==target must between >0 and <10
    0 < range==target < 10
    any ideas?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    range == target && range >= 0 && range <= 10
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Salem has suggested the condition you actually need to test.

    To understand your problem it is also useful to understand what the condition you tested actually means.

    First remember that in C, a non-zero integer value corresponds to true, and zero to false.

    Code:
    if( ( (range==target)>0) && ( (range==target)<10) )
    First this evaluates the expression "range == target". If range and target are equal, that yields a value of 1, so ((range == target) > 0) is equivalent to (1 > 0), which has a value of 1 (true). Hence it is necessary to evaluate the second part of the expression (after the &&). Evaluating ((range == target) < 10) is equivalent to (1 < 10), which yields 1.

    Hence the test is equivalent to
    Code:
       if (1 && 1) ...
    1 && 1 is always true.

    The expression you chose is certainly not equivalent to your intended test "the values are equal, both greater to zero, and both less than 10".
    Last edited by grumpy; 02-14-2013 at 03:11 AM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with if statement
    By adohertyd in forum C Programming
    Replies: 4
    Last Post: 12-20-2011, 09:34 PM
  2. if statement problem.....
    By JM1082 in forum C++ Programming
    Replies: 16
    Last Post: 09-09-2011, 11:23 AM
  3. problem with ; at end of if statement
    By Bujo0 in forum C++ Programming
    Replies: 3
    Last Post: 03-12-2011, 08:13 AM
  4. If Else statement problem
    By doofusboy in forum C Programming
    Replies: 2
    Last Post: 11-09-2005, 07:18 AM
  5. having a problem with a FOR statement
    By jonesy in forum C Programming
    Replies: 3
    Last Post: 10-01-2001, 01:24 PM