if else statement problem two

This is a discussion on if else statement problem two within the C Programming forums, part of the General Programming Boards category; 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 ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,341
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    5,871
    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 02:11 AM.
    Right 98% of the time, and don't care about the other 3%.

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, 08: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, 07:13 AM
  4. If Else statement problem
    By doofusboy in forum C Programming
    Replies: 2
    Last Post: 11-09-2005, 06: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

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