question on ==

Started by
5 comments, last by Grimey 23 years, 1 month ago
ok i want to know exactly what this is....yes im a newbie as its easy to tell heres an example else if(age==100) whats the "==" in the above thing for is there a particluar reason why there is two equal signs to say that its equal? - Grimey
Advertisement
It tests for equality.
When you use one = sign, it is an assignment statement.
val = 5; // the number 5 is put in val
(val == 10); // see if val contains the value 10

Martee
Magnum Games.NET
All your code are belong to us.
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
okay look at the following code:

---
int age = 0;
if (age == 0)
do something here

if (age = 10)
do something else
---

the difference is that the "==" is a test for equality, whilst the "=" is a assign operator. So age == 0 returns true or false. Whilst age = 0 says let age be 0.
The "==" operator is an equality operator. This is NOT the same as the "=" operator which is the assignment operator.


As an example:

age = 100;
This assigns the value of 100 to the variable age.

(age == 100)
This compares the value that is in age to 100. If it is the same then the expression is evaluated to true.


So you could say:
bool isEquals = ( age == 100 )

This assigns isEquals to either true if age is equal to 100 or false if not.





-------
Andrew
damn, you beat me by 2 seconds
Haha

Martee
Magnum Games.NET
All your code are belong to us.
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
thanks for the help

This topic is closed to new replies.

Advertisement