Author Topic: Why the results are different after running? just changing "=="to"="?  (Read 293 times)

Offline rose1975

  • Single posting newcomer
  • *
  • Posts: 2
The first code:
#include <iostream>
using namespace std;

int main () {
      int a=80;
   if( a == 10 ) {
         cout << "Value of a is 10 " << endl;
   } else if( a == 20 ) {
     cout << "Value of a is 20" << endl;
   } else if( a == 30 ) {
     cout << "Value of a is 30" << endl;
   }else {
     cout << "Value of a is not matching" << endl;
   }
     cout << "Exact value of a is : " << a << endl;

   return 0;
}
result: Value of a is not matching
Exact value of a is: 80

The 2nd code:(just changed a == 20 to a=20)
#include <iostream>
using namespace std;

int main () {
      int a=80;
   if( a == 10 ) {
         cout << "Value of a is 10 " << endl;
   } else if( a = 20 ) {
     cout << "Value of a is 20" << endl;
   } else if( a == 30 ) {
     cout << "Value of a is 30" << endl;
   }else {
     cout << "Value of a is not matching" << endl;
   }
     cout << "Exact value of a is : " << a << endl;

   return 0;
}
The result is:Value of a is 20
Exact value of a is: 20.

so, why appeared so difference in the results just changing == to =?

any help will be thankful.

Offline Miguel Gimenez

  • Multiple posting newcomer
  • *
  • Posts: 54
You should know the difference between comparison and assignment operators. Anyway that's off topic here.

Offline stahta01

  • Lives here!
  • ****
  • Posts: 5971
    • My Best Post
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 32 bit.
On Debian Testing, compiling CB Trunk against wxWidgets 3.0.2.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org