| New Reply |
[C] variable system in programming |
Share Thread |
| Aug11-12, 11:20 AM | #1 |
|
|
[C] variable system in programming
I just started programming and i dont understand how variables work. First the book defines a as 3 and b as 5 then defines a =a+b. For me who hasnt done programming ever that implies that b=0 but i think second a is different from the first one.
Help regarding how variables work in computer |
| Aug11-12, 11:42 AM | #2 |
|
Recognitions:
|
In the computer language example you show, "=" means to assign a value to a variable, as opposed to meaning the variable is equal to that value. This would be a language like C, and each of those statement can be explained as:
A = 3 means to store the value 3 into A B = 5 means to store the value 5 into A A = A + B means to add the values stored in A and B, then store the sum into A which will be 8. Some languages like APL use an extended character set and use left arrow for assignment: A ← 3 B ← 5 A ← A + B Others languages like Pascal use := A := 3 B := 5 A := A + B |
| Aug11-12, 11:32 PM | #3 |
|
|
a=3 b=5 a=a+b a now = 8 b still = 5 it is the same as saying a=3+5 |
| Aug11-12, 11:50 PM | #4 |
|
|
[C] variable system in programming
By the way there are two main types of variables, numeric and string. Its important not to get them mixed up.
Strings work like this. a="hello " b="world" a=a+b a now = "hello world" Always define your variable types before you use them otherwise a=5 b=3 c=a+b c might end up being 53 Variables are usually assigned at the start of a program and can be used to tell the computer how they are to be treated. As a string Dim A as string Dim B as string A=9 B=6 A=A+B A now = 96 As a number Dim A as integer Dim B as integer A=9 B=6 A=A+B A now = 15 |
| Aug12-12, 01:13 AM | #5 |
|
Mentor
|
Just as a general comment... it helps to say which programming language you are using.
|
| Aug12-12, 02:08 AM | #6 |
|
|
Ok thank you. I am learning C.
|
| Aug12-12, 04:06 AM | #7 |
|
|
The best way to think about these is that the left hand variable is where the stuff gets stored and the right hand side (of the equals sign) is what is taken in. If a variable appears in both sides then the variable may change after the line has been executed. |
| Aug12-12, 08:22 AM | #8 |
Recognitions:
|
There are two types of programming language. The most common type (including C) could be described as "imperative", i.e. most of the source code contains commands telling the computer to do something. The "=" sign is just as shorthand for "work out the value of what is on the right hand side, and then assign it to the variable on the left hand side". As you said, this isn't what "=" means in mathematics, but there is a limited set of characters available on a standard computer keyboard.
There are a few languages that are "declarative", where the source code is more like a "set of equations" that the computer "solves" to produce a result (or several results, if the solution isn't unique). These languages are usually less general-purpose, because the method for "solving the equations" has to be built into the implementation, so you can't just throw anything at it and hope it will figure out how to solve it. PROLOG is an example of that type of language. Some early versons of the BASIC programming language carried the idea of an "imperative" language to its logical conclusion, and every statement in the language stared with a "verb". Arguably it's a bit more obvious what LET A = A + B means compared with just A = A + B, but since the computer doesn't need the "LET" to figure out what the statement means, it didn't survive into any "modern" programming language that I know of. The inventors of COBOL language (now pretty much obsolete) chose not to use symbols at all, and went for long winded alternatives like ADD B TO A ADD B AND C GIVING A instead of a = a + b and a = b + c. That might not seem too bad, but typing words like MULTIPLY and DIVIDE in full instead of * and / wasn't much fun. |
| Aug12-12, 12:19 PM | #9 |
|
Recognitions:
|
|
| New Reply |
Similar discussions for: [C] variable system in programming
|
||||
| Thread | Forum | Replies | ||
| ODE System with Variable Coefficients | Differential Equations | 3 | ||
| Linear Programming - Restating a System as a Canonical Primal | Calculus & Beyond Homework | 1 | ||
| operating system and programming language | Programming & Comp Sci | 15 | ||
| variable mass system | Advanced Physics Homework | 3 | ||
| 3 Variable Linear Programming Question | Calculus & Beyond Homework | 1 | ||