Hi Guys , i have 2 questions ... i appreciate u if u answer ...
1. In "If&Else" when we use "==" istead of "= " The compiler behave the same ... do u know why ??
2. and what is the preference of ++ ? (instead we plus 1)
SenioRija
Tnx !
Hi Guys , i have 2 questions ... i appreciate u if u answer ...
1. In "If&Else" when we use "==" istead of "= " The compiler behave the same ... do u know why ??
2. and what is the preference of ++ ? (instead we plus 1)
SenioRija
Tnx !
Last edited by Seniorija; 10-27-2012 at 07:47 AM.
1. One equal sign is for assignment.Two for comparison.
example
2.What do you mean by preference?Code:#include <iostream> using namespace std; int main() { int a; a = 5; // assignment if(a == 5) { cout<<"a is five"<<endl; } if( a = 10 ) { cout<<"An assignment was just performed!!!Not a comparison!"<<endl; } return 0; }
It following are equivalent
You should write what makes you feel more comfortable The ++ and -- give a shorter codeCode:a++; a = a + 1; a+= 1;
1) It doesn't, you think it does. What really happens when you use "=" instead of "==" is that an assignment is performed instead of a comparison.
2) It's shorter, meaning more compact and as a result less errors may happen, and of course someone may want to use the side effects of it ( pre-increment vs post-increment )
Devoted my life to programming...
Tnx ...
1.I know that 1 equal sign is for assignment and 2 equal sgin is for comparison ,
but my question is about if&else ... i mean when i use == istead of = the compiler behave the same ... !! do u know why compiler behave like this and run the program without any Errors ?!!!
2. I get ur answer ... Tnx so much !
The compiler does not behave the same.It does what you tell him to do!
This not a syntax error,so that the compiler should say something ,but in most cases a logical errors.The compiler thinks that you know what are doing.But it is very usual that people just forget the 2nd equal sign.The compiler can't say something because if you write this
then what is inside the condition is not false,thus it will enter the body of the if.Code:if( n=5) { printf("this is going to be printed\n"); }
It is the programmer's responsibility to use the appropriate operator
There is a difference between the resulting code behaving the same, and the compiler itself behaving the same. The former is false as the two simply do not generate code that does the same thing, and the later depends upon what compiler and what its warning flags/levels are.
If the warning levels do not permit it to tell you about the possible mistake, then it will just blindly compile the code, because that's what you've entrusted it to do.
For number 2, you should probably refine your question. There are a bunch of different things you could mean. You could be talking about operator precedence, for example. But under the assumption you are asking why one would use different methods of incrementing over others, well there are several parts to that.
One is that the following statements on their own all do the same thing:
But when used inside other expresions, the above are not all the same.Code:++x; x++; x += 1; x = x + 1;
In general, you should prefer to use the first of the above ways of incrementing a variable.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
Just a short demonstration. As you run this code, you will see it prints "n is 5", which is clearly not true.Code:int n = 10; if (n == 5) std::cout << "n is 5!"; if (n = 5) std::cout << "n is 5!";
It happens because the last if assigns 5 to n, then evaluates "n" which is greater than 0, and therefore true.
Again, they are not the same thing!