User forums > Help

Why the results are different after running? just changing "=="to"="?

(1/1)

rose1975:
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.

Miguel Gimenez:
You should know the difference between comparison and assignment operators. Anyway that's off topic here.

stahta01:
@rose1975:

Read the rules! http://forums.codeblocks.org/index.php/topic,9996.0.html

Navigation

[0] Message Index

Go to full version