473,698 Members | 2,171 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Logical And

sam
Please look at the code below
#include <stdio.h>

int expr(char str[], int i){
printf("%s \n",str);
return i;
}

int main()
{
if(expr("1st",1 ) || expr("2nd",0) && expr("3rd",1));
return 0;
}

output
-------
1st

As && has an higher precedence over ||
then it should call expr("2nd",0) or expr("3rd",0)
first then why it calls (expr("1st",1) first

Regards
Shiju
Nov 14 '05
34 2286
Old Wolf wrote:
> if(expr("1st",1 ) || expr("2nd",0) && expr("3rd",1));

Summary: Operator precedence governs the meaning of an
expression with multiple operators, but does not control
the order in which the operands are evaluated.


Finally a sensible answer in this thread. A corollary is: If you
want to use complex logical expressions, parenthise them so there
is NO DOUBT, NO DOUBT WHATSOEVER, what they mean. You will save a
lot of headaches, both your own and others.


(((((How) (would) (that) (help))?) (The (expressions (
((A || B) && C)
(and)
(A || (B && C))
) (have) ((the) (same) ((order) (of) (evaluation))). )
((There) (was) (never) ((any) (doubt) (about) (((the) (meaning))
(of) ((the) (expression)))) )).


Not so.

A B Eval ((A || B) && C) Eval (A || (B && C))
== == =============== ==== =============== =====
0 0 A B done=0 A B done=0
0 1 A B C done=C A B C done=C
1 0 A C done=C A B done=0
1 1 A C done=C A done=1

If I have made no mistakes. At any rate, the expression values
and side effects are different.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 14 '05 #31
CBFalconer <cb********@yah oo.com> wrote:
Old Wolf wrote: <snip>
(((((How) (would) (that) (help))?) (The (expressions (
((A || B) && C)
(and)
(A || (B && C))
) (have) ((the) (same) ((order) (of) (evaluation))). )
((There) (was) (never) ((any) (doubt) (about) (((the) (meaning))
(of) ((the) (expression)))) )).


Not so.

A B Eval ((A || B) && C) Eval (A || (B && C))
== == =============== ==== =============== =====
0 0 A B done=0 A B done=0
0 1 A B C done=C A B C done=C
1 0 A C done=C A B done=0

^^^^^^^^^^ 1 1 A C done=C A done=1

If I have made no mistakes.
Maybe it's me who is mistaken, but shouldn't the marked (^^^)
line read: A done=1 (just like the one below it)?
At any rate, the expression values
and side effects are different.


However, your claim is of course correct.

Regards
--
Irrwahn Grausewitz (ir*******@free net.de)
welcome to clc : http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.eskimo.com/~scs/C-faq/top.html
acllc-c++ faq : http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html

Nov 14 '05 #32
In <40**********@m indspring.com> pete <pf*****@mindsp ring.com> writes:
Dan Pop wrote:

In <40************ ***********@new sreader02.highw ay.telekom.at> Robert Bachmann <Ro************ *@rbdev.net> writes:
>Keith Thompson wrote:
>> If I saw code that used "if (a & b)", my first assumption would be
>> that it was a typo for "if (a && b)", and my second would be that the
>> author really intended to test the bitwise "and" of a and b.
>
>When writing something like "if (a & b)" it is a good idea to
>write "if (a & b) /*bitwise*/" instead.
>On the other hand it looks a little bit funny to me, just like
> some beginner who writes "int i; /*an integer variable*/" ;-)


The real solution is to write it as:

if ((a & b) != 0) ...

No silly comments needed to remove any doubt WRT your real intentions.
It is seldom a good idea to skip the explicit comparison.


I believe that you're implying that it's a good idea
to skip the explicit comparison of the result of a logical operation.

You wouldn't write
if ((a && b) != 0)

right ?


Right. When dealing with a logical expression, the explicit comparison
of the result is pointless, because the intent is obvious. Ditto for
numeric variables that are used as flags (conceptually booleans), where
the intent is equally obvious. E.g. I would write:

int i, access_ok = 0;
...
if (i != 0) /* do this */ ;
if (access_ok) /* do that */ ;

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #33
Dan Pop wrote:
When dealing with a logical expression, the explicit comparison
of the result is pointless, because the intent is obvious. Ditto for
numeric variables that are used as flags (conceptually booleans),
where the intent is equally obvious. E.g. I would write:

int i, access_ok = 0;
...
if (i != 0) /* do this */ ;
if (access_ok) /* do that */ ;


Thank you.

--
pete
Nov 14 '05 #34
Old Wolf <ol*****@inspir e.net.nz> spoke thus:
(((((How) (would) (that) (help))?) (The (expressions (
((A || B) && C)
(and)
(A || (B && C))
) (have) ((the) (same) ((order) (of) (evaluation))). )
((There) (was) (never) ((any) (doubt) (about) (((the) (meaning))
(of) ((the) (expression)))) ))


Was that intended to resemble (horrible) Lisp code? ;D

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #35

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
44106
by: Hari Om | last post by:
Here are the details of my error log files: I execute the command and get following message at console: ---------------------------------------------------------------------- ../sqlldr scott/tiger@common control=/full_path/test.ctl log=/full_path/adhoc/test.log SQL*Loader: Release 9.2.0.1.0 - Production on Tue Sep 2 10:49:27 2003 Copyright (c) 1982, 2002, Oracle Corporation. All rights reserved.
7
2894
by: Charles Crume | last post by:
Hello all; I have used dBASE, and other computer languages/databases, for years. They all have a logical field type. However, the version of MySQL used by the ISP hosting my site does not support a "logical" field type. It does support ENUM and I have set some up in a couple of tables that accept the values 'T' and 'F'. Sometimes they work like a logical field: if ($myrow) echo 'New';
80
35101
by: Christopher Benson-Manica | last post by:
Of course one can get the effect with appropriate use of existing operators, but a ^^ operator would make for nice symmetry (as well as useful to me in something I'm working on). Am I the only one who would find it useful? -- Christopher Benson-Manica | I *should* know what I'm talking about - if I ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
3
1572
by: serge | last post by:
How do I determine which method I should use if I want to optimize the performance of a database. I took Northwind's database to run my example. My query is I want to retrieve the Employees' First and Last Names that sold between $100,000 and $200,000. First let me create a function that takes the EmployeeID
4
3552
by: serge | last post by:
I am running a query in SQL 2000 SP4, Windows 2000 Server that is not being shared with any other users or any sql connections users. The db involves a lot of tables, JOINs, LEFT JOINs, UNIONS etc... Ok it's not a pretty code and my job is to make it better. But for now one thing I would like to understand with your help is why the same SP on the same server and everything the same without me changing anything at all in terms of SQL...
14
36851
by: blueboy | last post by:
Hi, I am planning to automate a nighty restore of a DB on another server can someone point me in the right direction with the SQL script to modify the logical file names to the correct path and not the ones carried over with the DB?? i.e the database is to be renamed on the new server any help much appreciated
2
5541
by: dbtwo | last post by:
Until today I always thought as long as you see a lot of logical reads as compared to physical reads, then you're good. But it looks like it isn't so. But doesn't logical read mean it's being read from memory and no I/O involved? So why is Logical Reads = CPU Consumption ? I ran into an exact scenario last week when our applciation were running something, and each time an application started, the CPU would go from 99% idle to 48% idle. I...
1
1948
by: ags5406 | last post by:
Hi -- I posted this in a Fortran group but thought I'd post here as well. Any help is appreciated. I have a IVF10 DLL that is the calculation engine for a frontend application written in VB.NET 2005. I've noticed that when I pass a Boolean/Logical from VB.NET to IVF10 as part of an argument list, when I try to evaluate it it only
11
4686
by: Dominic Vella | last post by:
I am using MS-Access2000. I can't seem to set the default values for Logical type fields. I start with Dim dbsTmp As Object ' I think it's DAO.Database Set dbsTmp = DBEngine.OpenDatabase(CurrentProject.path & "\data_be.mdb") and then use the following to run my SQL ststement dbsTmp.Execute ----------------------------------------------------- Something like this SQL statement works: "ALTER TABLE tblStudent ADD COLUMN student_number...
7
3555
by: raylopez99 | last post by:
I have a logical drawing space much bigger than the viewport (the screen) and I'd like to center the viewport (the screen) to be at the center of the logical drawing space. After following the excellent transforms specified on Bob Powell's site, I still wonder if there's an easier way of centering it than the following procedure? Here is what I do now (it's awkward but it works): 1) I follow the transforms specified on Bob Powell's...
0
8674
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8603
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9157
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9023
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4366
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4615
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2327
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1999
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.