LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-25-2011, 12:09 PM   #1
alikamu
LQ Newbie
 
Registered: Jul 2011
Posts: 6

Rep: Reputation: Disabled
Post A Little explanation on logical OR.


Hi friends,

i have a code :

int x,y,z; x=1;y=z=0;x=x||y&&z;
printf("%d",x,y,z);

here value of x will depend on the result of x OR y AND z.
so the x OR y will be 1, therefore it will not check Logical AND condition.

If i am wrong please do let me know (thanks)

but in second example:
(c=='\0' || c=='\n' || c=='\t')

here will it follow as before and only left OR will be executed or the complete expression will be executed.

Thanks for reading and answering.

Last edited by alikamu; 07-25-2011 at 01:36 PM. Reason: Sorry, as pointed out by Heraton.
 
Old 07-25-2011, 01:15 PM   #2
Heraton
Member
 
Registered: Apr 2011
Location: Germany
Distribution: Mint 10, openSuSE
Posts: 58

Rep: Reputation: 3
Smile Using C i guess...

Hi!

Would have been nice to let us know, which language you are referring to. I will simply assume C. A second hint i would like to give is to use code tags. This little bastards help reading your code.

I am not sure, but i guess you mixed up the comparison operator "==" and the assignment operator "=". Your second example will change the value of c to '\0', '\n' or '\t', depending in which order you write your "logical expression". Each of the three expressions first changes the value of c, and after that, it will take the new value and will interpret that as a logical value, which will almost always be true.

If you did use the "=" for a reason, i don't have a clue about what your question is exactly...

Edit:
There is another little flaw in your post about operator precedence, if we are talking about C:
Logical AND "&&" has a precedence of 5 while logical OR "||" has only 4. That for your upper expression should be evaluated as:

X OR ( Y AND Z )

This example demonstrates the behavior:
Code:
#include <stdio.h>

int main(int c, char * argv[])
{
	int x = 1;
	int y = 1;
	int z = 0;

	if (x||y&&z)
		printf("true\n");
	else
		printf("false\n");
}
If your assumption was right, x and y would evaluate to true, but true and false would result in a false...

Anyway, tests on my machine result in true...

Regards, Heraton

Last edited by Heraton; 07-25-2011 at 01:31 PM. Reason: See edit...
 
Old 07-25-2011, 01:16 PM   #3
devnull10
Member
 
Registered: Jan 2010
Location: Lancashire
Distribution: Slackware Stable
Posts: 572

Rep: Reputation: 120Reputation: 120
Different languages have different implementations (which are you using??) however most will execute just the left hand side in a logical or expression if that part of the expression evaluates to true. An example in c:

Code:
     1  #include<stdio.h>
       
     2  int main() {
     3    int x=1;
     4    int y=0;
     5    x==y||printf("Executed\n");
     6  }
Code:
 tmp $ ./a.out 
Executed
Code:
     1  #include<stdio.h>
       
     2  int main() {
     3    int x=0;
     4    int y=0;
     5    x==y||printf("Executed\n");
     6  }
Code:
 tmp $ ./a.out 
 tmp $
We often used to do things like this in PHP:

Code:
do_something(etc) or die("Couldn't do something");
 
Old 07-25-2011, 02:41 PM   #4
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by alikamu
Last edited by alikamu; Today at 14:36. Reason: Sorry, as pointed out by Heraton.
Don't edit your first post in response to replies. It makes the thread very difficult to understand.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] C - Why signals? Example? Explanation? golmschenk Programming 6 04-08-2010 04:18 PM
Extending logical volume LogVol01 Insufficient allocatable logical exte swap space umeshsharma Linux - Newbie 4 06-22-2009 12:26 PM
Log explanation tekmann33 Linux - Newbie 2 07-25-2008 02:26 PM
Better SUDO explanation maginotjr Slackware 9 07-04-2005 01:59 PM
I Could Use An Explanation winger Linux - General 3 04-13-2002 10:32 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:27 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration