Basics · 02
Variables and values
Give a value a name so you can reuse and change it.
A variable is a name tag on a value.
The example below stores a name and an age, prints them, then adds 1 to the age and prints it again.
name = "Lara"age = 10print(name + " " + str(age))age = age + 1print(age)const name = "Lara";let age = 10;console.log(name + " " + age);age = age + 1;console.log(age);const name: string = "Lara";let age: number = 10;console.log(name + " " + age);age = age + 1;console.log(age);public class Main { public static void main(String[] args) { String name = "Lara"; int age = 10; System.out.println(name + " " + age); age = age + 1; System.out.println(age); }}using System; class Program{ static void Main() { string name = "Lara"; int age = 10; Console.WriteLine(name + " " + age); age = age + 1; Console.WriteLine(age); }}#include <iostream>#include <string> int main() { std::string name = "Lara"; int age = 10; std::cout << name << " " << age << '\n'; age = age + 1; std::cout << age << '\n';}#include <stdio.h> int main(void) { const char *name = "Lara"; int age = 10; printf("%s %d\n", name, age); age = age + 1; printf("%d\n", age); return 0;}name = "Lara";age = 10;Console.WriteLine(name + " " + age);age = age + 1;Console.WriteLine(age);Lara 10 11
Key points
=does not mean "is equal to".
It means "put the value on the right into the name on the left", which is whyage = age + 1makes sense.- Some languages make you write a type (integer, text, …) when you create a variable.
In others the value knows its own type, so you don't. - A value that should never change is a constant.
Assigning to a constant again is an error. - Integers have a size limit.
A 32-bitintgoes up to2,147,483,647(about 2.1 billion); for bigger numbers use a 64-bit integer (up to about 9.2×10^18).
How each language writes it
| Python | age = 10 — no type.There is no constant syntax, only the convention of an uppercase name like MAX_SCORE.Integers have no size limit. Swap with a, b = b, a |
|---|---|
| JavaScript | let age = 10, constants with const (reassigning one throws TypeError: Assignment to constant variable. at run time).Numbers are exact only up to 2^53 (about 9×10^15). Swap with [a, b] = [b, a]; put a value in text with ${age} |
| TypeScript | let age: number = 10 — leave the type out and it is inferred from the value.Reassigning a const is a compile error.Otherwise the same as JavaScript |
| Java | int age = 10;, text is String, and var lets the compiler infer.Constants use final, big numbers long.Swap with a temporary |
| C# | int age = 10;, text is string, var also works.Constants use const, big numbers long.Swap with (a, b) = (b, a); put a value in text with $"{age}" |
| C++ | int age = 10;, text is std::string, and auto lets the compiler infer.Constants use const, big numbers long long.Swap with std::swap(a, b) |
| C | int age = 10;, text is const char * (or a char array).Constants use const, big numbers long long.Swap with a temporary. Put text and numbers into slots: printf("%s %d\n", name, age) |
| Craykon | age = 10; — no type, and no constants.Numbers are exact up to 2^53. Swap with a temporary. me is a keyword, so it can't be a variable name |
Common mistakes
Adding to a variable with no starting valueC · C++
int total;
total += 5;
printf("%d\n", total);→ It starts from whatever garbage was in memory, so the sum is wrong
Give it a starting value: int total = 0;.
Java and C# reject such code at compile time.
Swapping with a = b; b = a;
a = 1; b = 2;
a = b;
b = a;→ Both a and b are 2: a's original value was lost on the first line
Use a temporary: t = a; a = b; b = t;
Using a variable outside the block that created itCraykon
if (true) {
y = 1;
}
Console.WriteLine(y);→ An error saying the name y hasn't been made yet
Create it before the block with y = 0;.
(Python, by contrast, still sees it outside.)
How languages differ
Python can't join text and a number with
Convert with
Python can't join text and a number with
+ ("Lara " + 10 → TypeError: can only concatenate str (not "int") to str).Convert with
str(age) or use f"{name} {age}".