Code::Blocks Forums

User forums => Help => Topic started by: rose1975 on July 04, 2017, 08:39:26 am

Title: Why the results are different after running? just changing "=="to"="?
Post by: rose1975 on July 04, 2017, 08:39:26 am
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.
Title: Re: Why the results are different after running? just changing "=="to"="?
Post by: Miguel Gimenez on July 04, 2017, 01:28:28 pm
You should know the difference between comparison and assignment operators. Anyway that's off topic here.
Title: Re: Why the results are different after running? just changing "=="to"="?
Post by: stahta01 on July 04, 2017, 02:52:00 pm
@rose1975:

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