Variables in Programming and Algebra


Collapse Content

Now that you know what a variable is, we will clarify a common area of confusion between algebra and programming.

In algebra, you can have a statement like this:

x = 2 + 3

This is a "truth-statement" that the variable x is equal to 2+3 or 5.

Similarly, in programming, you can write int x = 2 + 3 and the variable x will end up as the integer 5. However, there's a big difference between the two statements, as will now be seen.

What if you faced the following two statement together?
x = 2 + 3
x = 11

In algebra, you would immediately recognize that this is impossible. x cannot be both 2+3 and 11 since they're not equal. However, in programming, = is not a statement of truth, it's an action that assigns a value to a variable.
So you can assign 2+3 to x and then afterwards assign 11 to x, and x will now be set to 11 instead of 5.

Equality Check
If you wanted to check if something was equal in programming, you use ==. For example:

int x = 2 + 3; //assignment
x == 11; //this would be false

Review this:

  • = assignment
  • == equality check

Challenge

After the following code runs, what number will sum equal?

int x = 2;
int y = 5;
x = 100;
int sum = x+y;

Please sign in or sign up to submit answers.

Alternatively, you can try out Learneroo before signing up.

Comments

  • Easy I'm liking this website.

  • Enter Integer or Decimal Answer: I expect int sum == answer

  • I love this site. Now every other book is looking like a reference sheet.

  • You say "In the following code, what number will sum equal?"
    It would be better to say "In the following code, what number will sum become?"
    Also, you should give an extra example of multi-line assignment of variables before testing student understanding

    cont...
  • This is amazing thank you so much

  • Thanks to this website, I dont feel like a dumb as.. anymore!

  • so good, I like it so much!

  • Good site so far :)

  • It's easy. Forget the int x = 2 and go to x = 100 and add the y to the x.

  • I have increased my programming knowledge by 500% in the last 10 minutes.

  • In the code why it is written
    int x = 2;
    int y = 5;
    x = 100; .Can anyone tell me why data type int is not written befor x=100;

  • As mentioned on the previous page, you need to declare a variable's type the first time, but afterwards you can use it without re-mentioning what type it is.

  • Yes, but i want to learn programming. And isnt this a bit to easy to really learn programming?

  • You can start off easy. There's lot's of challenging material later if you want it!

  • ok i want that challenging stuff :-)

  • When you're ready, check out Algorithms

  • we will, but we are not ready yet :-)

  • this is just like js and c

  • This was my hardest challenge yet...How does 100 + 5 = 105 if x=2???? Computers are DU|\/|@$$3$ !!!! If X(2)=100 And X(1)=2 and Y(5) then wouldn't x+x+y =107? the only place xxy ever made sense was in Klinefelter syndrome! :p

  • Got confused here since x = 100; wasn't given any data type.

  • Great Website! Thanks.

  • This website makes a lot of sense, because a lot of people are not employed or partially employed and can't afford the high monthly costs of other training programs. Also, the questions are simple

    cont...
  • Am so great-full for the amazing challenges on this website

All Node Comments
Contact Us
Sign in or email us at [email protected]