if (x && y || z)

This is a discussion on if (x && y || z) within the C++ Programming forums, part of the General Programming Boards category; if I have something like 'm'==command[0] && 'u'==command[1] || 's'==command[2] how does it work? Can I use both && and ...

  1. #1
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167

    if (x && y || z)

    if I have something like

    'm'==command[0] && 'u'==command[1] || 's'==command[2]

    how does it work? Can I use both && and || together and if so what does it mean? Does it mean (this AND that) OR that or this (AND that OR that)
    AIM: MarderIII

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Sure you can use it. You just have to pay attention to presidence, or do the smart thing and use parenthesis to state exactly what you mean.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280
    if (com[0] == 'm' || (com[1] == 'n' && com[2] == 'o'))

    If com[0] equals 'm' or if com[1] equals 'n' and com[2] equals 'o,' then do what ever.

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    I think he might mean would it be equivilent to this:

    ( ('m'==command[0] && 'u'==command[1]) || ('m'==command[0] && 's'==command[2]) )

    In which case, no.

  5. #5
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    just remember to use parenthesis when you're using both '&&' and '||'
    Code:
    ( XX && YY || ZZ )
    
    // TRUE IF:
    // XX AND YY
    // OR:
    // ZZ
    
    ( ( XX && YY ) || ZZ )
    
    // TRUE IF:
    // XX AND YY
    // OR:
    // ZZ
    
    ( XX && ( YY || ZZ))
    
    // TRUE IF:
    // XX
    // AND:
    // YY OR ZZ
    a great example of how parentesis make a big difference...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  6. #6
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167
    instead of saying

    Code:
    if (!strcmpi("execute command", command))
    is there a way to check and see if command includes "execute command" without checking to see that command[0]=e and command [1]=x and so on...or perhaps to check that command[0-14]='execute command' ?
    AIM: MarderIII

  7. #7
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    Use the strstr function. It should be in your reference.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  8. #8
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167
    I don't know what a reference is, aside from a dictionary or encyclopedia.
    AIM: MarderIII

  9. #9
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280
    A nice reference is http://www.cppreference.com/


    The specific page for strstr() is here, http://www.cppreference.com/com/stds...ls.html#strstr

  10. #10
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167
    I have no idea what this means
    Code:
     #include <string.h>
      char *strstr( const char *str1, const char *str2 );
    
    The function strstr() returns a pointer to the first occurrence of str2 in str1, or NULL if no match is found.
    AIM: MarderIII

  11. #11
    jasondoucette.com JasonD's Avatar
    Join Date
    Mar 2003
    Posts
    278
    You have to know what pointers are to understand that. The function simply finds the first occurrence of a substring within a larger string. Instead of returning an index to its find, it returns a pointer to it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  2. process killer on a loop
    By Anddos in forum Windows Programming
    Replies: 8
    Last Post: 01-11-2006, 12:50 AM
  3. Replies: 4
    Last Post: 11-23-2003, 06:15 AM
  4. Massive Function Problem
    By Marc Sharp in forum C Programming
    Replies: 10
    Last Post: 11-19-2003, 07:49 PM
  5. Tic Tac Toe Help
    By aresashura in forum C++ Programming
    Replies: 1
    Last Post: 11-21-2001, 11:52 AM

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