Can you help me with this line of code please??

A

almurph

Hi,


Can you help me with this please? i am trying to translate some C
into C#. Is the following first line cod code (in C) equivalent to he
second (in C#.NET)? I can't write it literally as C# does not like the
first segment:


if((a < b) == (c == 1))
{
do something;
}


equivalent to (the C# equivalent):


if(c == 1)
{

if(a < b)
{
do something;
}
else
{
if(! a< b)
{
do something;
}
}
}


Would appreciate any comments/suggestions/reviews that you may be
able to offer.

Thanking you,
Colm
 
E

Eric Sosman

Can you help me with this please? i am trying to translate some C
into C#. Is the following first line cod code (in C) equivalent to he
second (in C#.NET)? I can't write it literally as C# does not like the
first segment:

if((a < b) == (c == 1))
{
do something;
}

equivalent to (the C# equivalent):
[...]

Sorry; I don't know any C# and can't say what might
be equivalent. What I *can* do, though, is describe what
the C fragment does; maybe then you can figure out how
to express the same thing in C#.

Okay: The C fragment makes two tests, and then combines
the results of those tests into a final answer. It asks
whether `a' is less than `b', and gets either true (1) or
false (0). It also asks whether `c' is equal to 1 and again
gets either true or false. Finally, it asks whether the
two true/false tests got the same result. If they were the
same (both true or both false), `do something' is executed.
If they were different (true and false, or false and true),
`do something' is skipped.

Some equivalent re-writings in C (the second assumes
that evaluating a,b,c produces no side-effects):

if (!( (a < b) ^ (c == 1) )) {
// do something
}

if ( (a < b && c == 1) || (a >= b && c != 1) ) {
// do something
}

Now, it's up to you to come up with the C# that expresses
the same thing.
 
B

Ben Bacarisse

Can you help me with this please? i am trying to translate some C
into C#. Is the following first line cod code (in C) equivalent to he
second (in C#.NET)? I can't write it literally as C# does not like the
first segment:

if((a < b) == (c == 1))

This is just a good in C# as in C so there is no (obvious) need to
re-write anything.

You need to post in a C# group. There you will find people who can
understand the C and tell you the proper C# way to do things (I
can't).
 
J

James Kuyper

Hi,


Can you help me with this please? i am trying to translate some C
into C#. Is the following first line cod code (in C) equivalent to he
second (in C#.NET)? I can't write it literally as C# does not like the
first segment:

What you're really looking for is a forum where you can find people who
know both C and C# well enough to answer your questions. It might have
seemed plausible that someone like that might be found in a newsgroup
devoted to C. However, in all of the answers that people have made to
your questions about translating C code to C#, I don't remember a single
response that suggested that the responder knew anything about C#. Might
you not do better by going to a C# forum, and hoping someone there knows
C? You certainly can't do much worse.
if((a < b) == (c == 1))
{
do something;
}

Why not? Do you know what the C# rule is that is violated by that code?
The wording of the error messages might give you a clue - what do they
say? This is a prime example of a place where some knowledge of C# would
be helpful in answering your question.
equivalent to (the C# equivalent):


if(c == 1)
{

if(a < b)
{
do something;
}
else
{
if(! a< b)
{
do something;
}
}
}

Assuming that the above C# code has the same meaning as it would have in
C, it is not equivalent to the earlier C code. Unless either 'a' or 'b'
is volatile, the third if() is pointless. You can't get to the third
if() unless the condition for the second if() is false, which means that
the condition for the third if() is guaranteed to be true whenever it's
actually evaluated. Therefore, the above code is equivalent to:

if(c == 1)
{

if(a < b)
{
do something;
}
else
{
do something;
}
}

Or, simplifying even further:

if(c==1)
{
do something;
}


A correct C replacement for your original C code would be:

if(a < b)
{
if(c==1)
do something;
}
else
{
if(c!=1)
do something;
}

Does C# find that acceptable?
 
A

Antoninus Twink

Unless either 'a' or 'b' is volatile, the third if() is pointless. You
can't get to the third if() unless the condition for the second if()
is false, which means that the condition for the third if() is
guaranteed to be true whenever it's actually evaluated.

Wrong. For example, if a is 1 and b is 0, then neither the first nor the
second "do something" will be executed.

Perhaps you got confused about precedence: ! a<b is (!a)<b, not !(a<b).
 
J

James Kuyper

James said:
(e-mail address removed) wrote: ....

Assuming that the above C# code has the same meaning as it would have in
C, it is not equivalent to the earlier C code. Unless either 'a' or 'b'
is volatile, the third if() is pointless. You can't get to the third
if() unless the condition for the second if() is false, which means that
the condition for the third if() is guaranteed to be true whenever it's
actually evaluated.

My apologies. As has already been pointed out by someone else, I made an
elementary mistake in interpreting the above code. I read it as !(a<b),
even though the parentheses were quite clearly missing. Interpreted
correctly, there's even less connection between the above code and the C
code that it's supposed to be equivalent to than I thought. I suppose my
mind filled in the parentheses in an attempt to make it seem more
reasonable than it really was (though it was still wrong).

The main point I was making remains unchanged:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top