Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 27

Thread: true/false question

  1. #11
    Join Date
    Nov 2005
    Location
    Sendai, Japan
    Beans
    11,296
    Distro
    Kubuntu

    Re: true/false question

    Quote Originally Posted by MG&TL View Post
    Standard C doesn't have a boolean variable.
    I am not sure what you mean by "a boolean variable", but standard C certainly does have a bool type since C99. Also note sure what "this" refers to in

    Quote Originally Posted by MG&TL View Post
    ...this works with any data type, not just int, AFAIK.
    EDIT: Okay...

    Quote Originally Posted by MG&TL View Post
    You can, but for your purposes, you probably don't need to.

    Just set it to 1 (or more) or 0, depending on how you take input.
    What is this? There is a type bool, its purpose is to represent boolean values. Of course you don't "need" to use it, and you don't "need" to use an int, either. You could use a float, or a struct, or whatever. But a bool is the right thing to use. Not an int or anything else.
    Last edited by Bachstelze; November 20th, 2012 at 12:07 PM.
    「明後日の夕方には帰ってるからね。」


  2. #12
    Join Date
    Nov 2005
    Location
    Sendai, Japan
    Beans
    11,296
    Distro
    Kubuntu

    Re: true/false question

    What I said above notwithstanding, just because a variable can only take two values does not necessarily mean it should be made a bool. A bool can not take any two values, it can only take the values true and false. This is a good use of bool:

    Code:
    bool found = false;
    for (int i=0; i<n; i++) {
        if (t[i] == SOME_VALUE) {
            found = true;
        }
    }
    This is a bad one:

    Code:
    bool turn = true;
    for (;;) {
        /* Play */
        turn = !turn;
    }
    it should be:

    Code:
    #define WHITE 1
    #define BLACK 0
    
    int turn = WHITE;
    for (;;) {
        /* Play */
        turn = (turn == WHITE) ? BLACK : WHITE;
    }
    A good rule of thumb is: add a question mark after the name of your variable. If you can answer the resulting question with yes or no, make it a bool. Otherwise, don't.
    Last edited by Bachstelze; November 20th, 2012 at 12:29 PM.
    「明後日の夕方には帰ってるからね。」


  3. #13
    Join Date
    Nov 2012
    Beans
    12

    Re: true/false question

    So i do need to use a bool type variable?that's my problem. Do i give an initial value of true or 1? and how do i check which value it holds? with frog=1 condition?

  4. #14
    Join Date
    Nov 2012
    Beans
    12

    Re: true/false question

    I meant frog ==1 of course

  5. #15
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,288
    Distro
    Kubuntu

    Re: true/false question

    Quote Originally Posted by Vaphell View Post
    var = !var will work, try it.
    Yep.

    Another way (assuming var is initialized to either 0 or 1)...
    Code:
    var ^= 1;
    And with C99 style:
    Code:
    #include <stdbool.h>
    ...
    bool var = false;
    ...
    var ^= true;
    Last edited by dwhitney67; November 20th, 2012 at 12:27 PM.

  6. #16
    Join Date
    Nov 2012
    Beans
    12

    Re: true/false question

    We haven't learnt that one so im not sure what it means

  7. #17
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,288
    Distro
    Kubuntu

    Re: true/false question

    Quote Originally Posted by littlerunaway View Post
    I meant frog ==1 of course
    You can do that. A simple if/else is all that is required. Depending on circumstances, it is sometimes helpful to use a terciary-style of statement.

    Examples:
    Code:
    #include <stdbool.h
    
    int main(int argc, char** argv)
    {
        bool args_given = (argc > 1);
    
        if (args_given)
        {
            // parse command-line options
        }
        else
        {
            // no command-line options given
        }
        ...
        return 0;
    }
    Code:
    ...
    printf("The value is %s\n", (value ? "true" : "false"));
    ...

  8. #18
    Join Date
    Nov 2005
    Location
    Sendai, Japan
    Beans
    11,296
    Distro
    Kubuntu

    Re: true/false question

    Quote Originally Posted by littlerunaway View Post
    So i do need to use a bool type variable?that's my problem. Do i give an initial value of true or 1? and how do i check which value it holds? with frog=1 condition?
    What does "frog" even mean? Whether you should make it a bool or not depends on this. In any case, as such it most likely fails the bool test of my post #12.
    「明後日の夕方には帰ってるからね。」


  9. #19
    Join Date
    Nov 2012
    Beans
    12

    Re: true/false question

    Frog is a variable that should get true or false. how the programs behaves depends on what frog hold. frog also should change between 1&0 under the same conditions

  10. #20
    Join Date
    Nov 2005
    Location
    Sendai, Japan
    Beans
    11,296
    Distro
    Kubuntu

    Re: true/false question

    That's not what I meant. Why is your variable named "frog"?
    「明後日の夕方には帰ってるからね。」


Page 2 of 3 FirstFirst 123 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •