help please (newbie with C)

This is a discussion on help please (newbie with C) within the C Programming forums, part of the General Programming Boards category; I need help with understanding relational operators like...assigning a variable to a relational operator. In my C book ("Complete Beginners ...

  1. #1
    Yaj
    Yaj is offline
    Registered User
    Join Date
    Jun 2002
    Posts
    23

    help please (newbie with C)

    I need help with understanding relational operators like...assigning a variable to a relational operator. In my C book ("Complete Beginners Guide to C") it said if the value is false then it is 0 and if true then the relational operator is 1. But what is the point of this? Why wouldn't i just assign a 1 or 0 to the variable?

    In an exmple explaining a the for operator is shows this

    for (sub=0; sub<=51; sub++)
    val= (val==14) ? 1 : val; /*If val is 14, reset to 1 */

    So it resets to one after getting to 14? I'm trying to figure this out but the only source of information i'm getting is from a comment. I've tried looking in tutorials but they don't explain it..is there a tutorial that explains this? i looked really hard but couldn't find one.

    So what is the point of assigning relational operators to variables if it's just going to give me a 1 or a 0? why don't i assign it a 1 or a zero?

    Could someone give me a quick source code that would give me a use for this function?
    thankyou

    oh and sorry if i was suppose to make a topic declaring me being here i didn't see it in the rules.
    o.o

  2. #2
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    I'm very unclear with your what you exactly are looking for. I assume you are very unclear with the "Ternary Operator"
    The code presented above is probably only to explain the way the "Ternary Operators" work. To make things clearer, the same result could be achieved from the code below also:

    Code:
    for (sub = 0; sub <= 51; sub ++) {
           if(val == 14) {
                    val = 1;
           }
    }
    Hope this has helped you. But if you still are skeptical about this, here's link:
    http://www.cs.cf.ac.uk/Dave/C/node5....00000000000000

  3. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    Relational operators are operators like '==' and '&&'. You can't assign a variable to a relational operator, but you use relational operators and variables in an expression which evaluates to true or false. And the result of this evaluation can be assigned to a variable.

    Example:

    result = a && b;

    If a is true and b is true, then result gets the value true. If a is false or b is false or a is false and b is false, then result is false.

    Note that it true is not always 1 and false is not always 0!!

    >Why wouldn't i just assign a 1 or 0 to the variable?

    Relational operators are used to make decisions. They are for example used in if-constructions:

    Code:
    if (result == true)
    {
        /* do this */
    }
    else
    {
        /* do this */
    }
    >val= (val==14) ? 1 : val; /*If val is 14, reset to 1 */

    This is a ternary operator.

    result = (a == b) ? 1 : 0;

    First the value of (a == b) is calculated, in other words, it is checked if a and b are equal, this can be true or false. If a and b are equal, then result gets the value 1, else result gets the value 0.

    You can just assign a 1 or 0 to the variable, but then you would still need the if-construction. Just as shaik786 explained.

  4. #4
    Unregistered
    Guest
    You could say that if something was true then x, and if something was false then y. The conditional operator combines them and is a little more compact.

  5. #5
    Unregistered
    Guest
    I think what the example is trying to tell you is that when using a relational operator in a loop, eventually the value in the relational statement becomes false and the loop is terminated and the next statement in the program is executed. It sounds like you might be confusing the true and false of the example with a value that has to be assigned to a variable. This is not the case.

    For Example:

    for( i = 1; i <= 10; i++ )
    printf( "%d", i );

    This control structure would print the numbers 1-10.
    Eventually i <= 10 becomes false when i becomes 11, and the program will fall out of the loop and proceed to the next statement in the program.

    In computers, usually 0 represents false and 1 represents true.
    This has nothing to do with the value that is assigned to the variable.

    I hope this was of help to you.

    Basia

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getting to grips with allegro and ms vc++ (newbie)
    By jimjamjahaa in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2005, 06:49 PM
  2. Newbie in problem with looping
    By nrain in forum C Programming
    Replies: 6
    Last Post: 11-05-2005, 11:53 AM
  3. Newbie Programmer
    By Extropian in forum C++ Programming
    Replies: 3
    Last Post: 05-18-2004, 01:17 PM
  4. Some help for a newbie?
    By Ilmater in forum C++ Programming
    Replies: 23
    Last Post: 04-19-2004, 07:44 PM
  5. Newbie Game Develpoers Unite!
    By Telenosis in forum Game Programming
    Replies: 10
    Last Post: 06-22-2002, 02:02 PM

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