Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Question about C language structure

Status
Not open for further replies.

nadiro

Member level 1
Member level 1
Joined
Jul 23, 2012
Messages
34
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,288
Activity points
1,601
HI
:?: I want to know, what is the difference between this two structures:
1) if(N=!0)
2) if(N=!0);
I think when we write any instruction after form 1, it means they depends on the condition. But in form 2 there isn't any relation between the condition and instructions. WHAT IS YOUR IDEA?:idea:
What will happen if we write like form 2 ...?
 

Hi

You can't use form 2, you will get an error..
 

HI
:?: I want to know, what is the difference between this two structures:
1) if(N=!0)
2) if(N=!0);
I think when we write any instruction after form 1, it means they depends on the condition. But in form 2 there isn't any relation between the condition and instructions. WHAT IS YOUR IDEA?:idea:
What will happen if we write like form 2 ...?

Form 1 should be followed by some statements that are to be executed if the condition is true.
Code:
if(N=!0){
//do something
//do something else
//and something else
//.....
}

Form2 doesn't do anything. It's like writing
Code:
if(N=!0){

}
 

Exactly as Tahmid says,


Code C - [expand]
1
2
3
4
if ( a == b ) 
{
     // empty
}


is the same thing as

Code C - [expand]
1
if ( a == b );



now about the condition inside the if(N=!0) ---> ( it means if( N = (!0) ) )

Actually that condition will always be true because you missed the boolean operator !=

because !0 is NOT FALSE and is equal to TRUE. So N will get the value of ONE. One is a boolean true so what you have is if( true )
 
Last edited:
in statement 1
if the the condition of if statement is true then the if block will be executed otherwise else part will be executed
in statement2
the if block has null body because after semicolon of if block tells the if block has no elements
 
I would put it like that: they're both useless. (or useful if you're looking for anything that might make the CPU waste time)
The first if will always execute the block code because N = !0 means N=1 which means true. You can avoid that by just writing the code straight-trough.
The second if will always be executed too but will have no block of code to execute, so it'll actually do nothing else than wasting time on the CPU. You can avoid it by simply not putting it in first place.
 

You can dig a big hole for yourself if you aren't careful about the difference between = and ==. Similarly the difference between =! and != and ==!

Keith
 
  • Like
Reactions: FvM

    FvM

    Points: 2
    Helpful Answer Positive Rating
You can dig a big hole for yourself if you aren't careful about the difference between = and ==. Similarly the difference between =! and != and ==!

Keith


He is asking to being better, you should share your knowledge to help him.
If you don't want to answer, then it's fine. But don't give such answer which can depreciate anyone.
 

He is asking to being better, you should share your knowledge to help him.
If you don't want to answer, then it's fine. But don't give such answer which can depreciate anyone.

There is no reason to have such an attitude! My comment was intended to be helpful. You haven't actually contributed to the thread in a very helpful way.

Personally I don't like the look of either statement but it is not clear what the purpose of the original post is and whether the purpose is really to assign N with a value or whether it is a mistake and a comparison is intended - hence my comment.

Keith
 

I don't have any bad attitude or not trying to prove that I am very helpful to the site.
In fact you've help much more to this site.

I've visit this thread, have read answers and then I read of yours " You can dig a big hole for yourself " With whatever intention, of help you've wrote that, I've no problem or I should don't have a problem also.

But after a year in this site, I like to help/ like to get help from this site so much.
& when I read such answer which you've gave; I don't feel its good.
So being a part of site I suggest you that through my comment.

It was not an attempt to tell you that it is your mistake & not made any kind of comparison also.

& I am not saying personally to you, that's the strictly professional view behind it. "It is the same statement which I'd got once when I was a beginner and I'd got disappointed through that at that time.& when I read similar kind of statement as your answer then I've ask you not to depreciate anyone"
 

Well, I believed my original post would be helpful as I don't know the intended purpose of the statement and it could be that someone has mistakenly written =! when they mean != or ==!. I have know way of knowing.

Other than that I don't see the point in me repeating what has already been said such as:

I would put it like that: they're both useless.
which is a statement I would agree with, but then it assumes the statements haven't been written incorrectly in the first place.

There seems to be a lot of confusion in this thread with some assumptions about the purpose of the statement but the original poster has never come back to clarify it or comment on the suggestions so far.

Keith.
 

Well, I believed my original post would be helpful as I don't know the intended purpose of the statement and it could be that someone has mistakenly written =! when they mean != or ==!. I have know way of knowing.

Other than that I don't see the point in me repeating what has already been said such as:

which is a statement I would agree with, but then it assumes the statements haven't been written incorrectly in the first place.

There seems to be a lot of confusion in this thread with some assumptions about the purpose of the statement but the original poster has never come back to clarify it or comment on the suggestions so far.

Keith.

HI
Oh! I am so sorry to come back late. Excuse me please (There were some problems)
as you said,I wrote "=!" mistakenly, in fact I mean "==!" .
But I have a problem! :???:
What is the difference between != or !== or =! ? Do they have their own meaning by itself or some of they don't have any meaning? Which one is true? and Which one should be used in C structure?
Thank you;-)

- - - Updated - - -

Hi

You can't use form 2, you will get an error..
In codevision compiler both these 2 forms are possible & it doesn't have error.
Test it to understand,my friend:wink:
But the problem is,are they useful too or not?? And in which condition we use each one?
 

HI
Oh! I am so sorry to come back late. Excuse me please (There were some problems)
as you said,I wrote "=!" mistakenly, in fact I mean "==!" .
But I have a problem! :???:
What is the difference between != or !== or =! ? Do they have their own meaning by itself or some of they don't have any meaning? Which one is true? and Which one should be used in C structure?
Thank you;-)

= is assignment operator, and it means that the right hand is assigned to the left hand..

== is a boolean or logic operator it compares the right hand and the left hand and returns true if values are equal or false if they are diferent.

!== does not exist in C and should give a syntax error..

! is a logical NOT..
so =! means that the left hand will be assigned to a logical not of the right hand...

You should give your self some time to explore C operators https://en.wikipedia.org/wiki/Operators_in_C_and_C++
and their precedence
 

Things like == and != are "relational" operators - for comparisons https://en.wikipedia.org/wiki/Relational_operator

= on its own is an "assignment" operator - so x = y + z does what it would normally look like algebraically - like normal maths.

! is causing some confusion I think. If you write != it is a relational operator meaning "not equal to". However it is also a logical operator when it is on its own https://en.wikipedia.org/wiki/Operators_in_C_and_C++

So, when you wrote N=!0 you have not really made a comparison, you have made N equal to NOT zero. No comparison has happened.

I never really understood the purpose of the original statements, hence my warning that you can really get confused with these if you aren't careful. The main thing to do is check any relational statements very carefully, particularly if they don't seem to be working - it is so easy to make a mistake. I think that is why some languages use := instead of = - by making = on its own invalid, you prevent people accidentally misusing it - it simply isn't allowed.

Keith.

- - - Updated - - -

... and mgate beat me to it. I am a slow typist.
 

Thank you for all of your ideas:)
In fact my purpose of this post is to understand the difference between the 2 forms of if that I said.
As the members refer to the answer, I got the differences.
Now the rest of my question is:
According to members' idea, if the statment "N==!0" happens(the condition be true), in form2 nothing will execute by the program and it is like if(N==!0) { }.
But how about the situation in which the condition of if doesn't be true?
for example we write if(N==!0); but when the program arrives at this line of program, there isn't any N==!0 situation. Then what will execute??
I hope,I can explain my meaning.;-)
 

If the condition isn't true then program execution moves to the next statement.

Keith
 

Just analize the situation and you'll get all the answers.

if(N==!0) means: IF value contained in "N" is equal to "not zero", and it can be translated to IF "N" is "true". In its turn it means that to be able to run any code contained in the if code block N must be true. If it's not true and an "else" statement is defined, the else code block will be executed. If there's no else statement simply the program will jump over the whole if code block.
By the way:
(...) but then it assumes the statements haven't been written incorrectly in the first place.
Keith's right. Writing an if statement and suddenly ending it with a statement end char (which in C/C++ and most languages is ';' ) is not allowed at all. The compiler would for sure stop compiling and give you an error. If you really want to make a null if statement then you have to use the empty brackets trick:
Code:
if (N==!0) { /*see, it's empty */ }
You may instead omit the brackets and only the next instruction after the if (or else) statement will get executed when the if condition is true (or false).
Code:
if (N == !0)
	RunWhenIfHasTRUECondition();	// will be executed only when the if condition is TRUE
else
	RunWhenIfHasFALSECondition();	// will be executed only when the if condition is FALSE

// the next code will be executed anyway because it's not part of the IF statement code
MyVar=12345;
OtherFunction();
 
Last edited:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top