Thread: Simple OR question

  1. #1
    Registered User
    Join Date
    Apr 2016
    Posts
    4

    Simple OR question

    Hi

    I was wondering if this is appropriate use of the OR operand. I know there are lots of different types of or, but i'm just looking for the program to print if one or the other of the statements is true.

    Thanks

    Code:
    #include <stdio.h>
    
    
    int main(int argc, const char * argv[])
    {
    // insert code here...
    
    int a; //example numbers!
        int b;
        int c;
        int d;
        a=1;
        b=2;
        c=3;
        d=4;
    
    
        if ( ( a < b ) OR ( c<d ) )
        {
            printf("Yes");
        }
        return 0;
    }
    
    


    Also, i'm getting a build error expecting a ) on the if line. I can add a ) even though i don't see why it would expect one but it still asks for ).

    Thanks

  2. #2
    Guest
    Guest
    The OR operator is written like this:
    Code:
    if(a < b || c < d)
    {
        ....
    }
    AND is &&.

    There are a few operators you can write like text, but nobody does that.

  3. #3
    Registered User winuser's Avatar
    Join Date
    Apr 2016
    Location
    POLAND
    Posts
    5
    Quote Originally Posted by superfish151 View Post
    Hi

    I was wondering if this is appropriate use of the OR operand. I know there are lots of different types of or, but i'm just looking for the program to print if one or the other of the statements is true.

    Thanks

    Code:
    #include <stdio.h>
    
    
    int main(int argc, const char * argv[])
    {
    // insert code here...
    
    int a; //example numbers!
        int b;
        int c;
        int d;
        a=1;
        b=2;
        c=3;
        d=4;
    
    
        if ( ( a < b ) OR ( c<d ) )
        {
            printf("Yes");
        }
        return 0;
    }
    


    Also, i'm getting a build error expecting a ) on the if line. I can add a ) even though i don't see why it would expect one but it still asks for ).

    Thanks
    This is OK, furthermore if you like, you can use whatever operator name you want (almost) :


    Code:
    #define OPERATOR_OR ||
    #define O_R ||
    
    if (x OPERATOR_OR y)
    {
        // do something
    }
    
    if (x O_R y)
    {
       // do something
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by winuser
    This is OK
    No, it isn't. superfish151's own post already noted that there is a compile error, and rightfully so since OR is not a keyword and there is no identifier with that name in the code presented.

    Quote Originally Posted by winuser
    furthermore if you like, you can use whatever operator name you want (almost)
    That is true, but it is a terrible suggestion: it does not improve code readability since readers will be familiar with the operators but unfamiliar with the new names that you introduce, e.g., is OPERATOR_OR || or is it | (i.e., bitwise or)?

    If you really must use alternative names, then you should use what Guest alluded to, i.e., #include <iso646.h> and use the standard or macro.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by winuser View Post
    This is OK, furthermore if you like, you can use whatever operator name you want (almost) :
    @winuser,
    Please don't post the random ramblings of your inexperience. It's a waste of everybody's time.

    @superfish151,
    Some international keyboards don't have the characters used in the logical and bitwise operators, having replaced them with more generally useful characters for their language. So since 1995 C provides the iso646.h header with macros for alternate spellings for those operators. They are built into C++ as keywords (although apparently MS C++ compilers require the header). In general, though, they shouldn't be used unless you're actually working with such a keyboard.

    https://en.wikipedia.org/wiki/C_alternative_tokens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question
    By Kevinphp7 in forum C Programming
    Replies: 5
    Last Post: 10-15-2014, 07:39 AM
  2. simple question
    By dreadredemption in forum C++ Programming
    Replies: 4
    Last Post: 03-06-2013, 06:17 PM
  3. Simple question ?
    By Dilmerv in forum C++ Programming
    Replies: 2
    Last Post: 04-17-2006, 02:10 AM
  4. simple simple design question
    By Chaplin27 in forum C++ Programming
    Replies: 6
    Last Post: 05-31-2005, 11:33 PM
  5. simple I/O question
    By Mortissus in forum C++ Programming
    Replies: 7
    Last Post: 02-14-2005, 10:14 AM

Tags for this Thread