Programming Basics

The same basic building blocks of programming, shown side by side in Java, Python and C.

Last updated: September 23, 2026

Declaring a variable

A variable stores a value under a name, so you can access it again later in the code.

Javaint number = 5;
Pythonnumber = 5
Cint number = 5;

Printing text

Displays text or the result of a calculation on the screen, handy for testing and following what the program is doing.

JavaSystem.out.println("Hello");
Pythonprint("Hello")
Cprintf("Hello");

If condition

Runs a block of code only if a certain condition is true, otherwise it's skipped.

Javaif (number > 0) { ... }
Pythonif number > 0:
Cif (number > 0) { ... }

For loop

Repeats a block of code a fixed number of times, for example exactly ten times.

Javafor (int i = 0; i < 10; i++) { ... }
Pythonfor i in range(10):
Cfor (int i = 0; i < 10; i++) { ... }

While loop

Repeats a block of code for as long as a condition stays true, the number of runs isn't fixed in advance.

Javawhile (number > 0) { ... }
Pythonwhile number > 0:
Cwhile (number > 0) { ... }

Defining a function

Groups several instructions under their own name, which you can then reuse later with a single call.

Javaint add(int a, int b) { return a + b; }
Pythondef add(a, b): return a + b
Cint add(int a, int b) { return a + b; }

List or array

Stores several values one after another under a shared name, instead of creating a separate variable for each value.

Javaint[] numbers = {1, 2, 3};
Pythonnumbers = [1, 2, 3]
Cint numbers[] = {1, 2, 3};

Comment

Text in the code that the program ignores, useful for explanations to yourself or others.

Java// single line comment
Python# single line comment
C// single line comment

Data types and type conversion

Every value has a data type, for example a whole number or a piece of text. If two types don't match, you need to convert one first.

intA whole number with no decimal places, for example 5
floatA number with decimal places, for example 5.0
strText, also called a string, for example "5"
Python: int("5")Converts the text "5" into the number 5

Operators

Symbols you use to combine values or compare them with each other.

+ − * /Addition, subtraction, multiplication, division
%Remainder of a division, for example 7 % 2 gives 1
== and !=Checks whether two values are equal or not equal
> and <Checks whether a value is greater or smaller than another

Working with text

Text can be treated as a chain of individual characters, each character has a position, starting at 0.

text[0]The first character of a text, called indexing
text[0:3]The first three characters, called slicing
text.lower()Converts the text to lowercase
text.replace("a","b")Replaces every "a" in the text with a "b"
len(text)Returns the number of characters in the text

ASCII, characters and escape sequences

Internally, every character is just a number. ASCII defines which number belongs to which character.

ASCIIAssigns fixed number values to letters, digits and symbols, capital A for example stands for 65
Python: chr(65)Converts the number 65 into the character "A"
Python: ord("A")Converts the character "A" back into the number 65
\n and \tEscape sequences for a line break and a tab inside a text

Errors while programming

An error message is no reason to panic, it almost always names the line and the type of problem. The most dangerous error shows no message at all though.

SyntaxErrorThe code breaks the grammar of the language, often a missing bracket or colon
NameErrorA name is used that doesn't exist yet, often a typo in a variable name
IndentationErrorThe indentation is wrong, in Python indentation decides what belongs to which code block
Semantic errorThe code runs without any error, but produces a wrong result, for example because + was used instead of −, no error message, so it's the hardest to find
TipCheck the line number named in the error message first, that's almost always where the problem is
Worth a look

How a loop with a condition runs

This is how a typical for or while loop with a check runs, step by step.

Start
Check condition, e.g. i < 10
True: run the statement, go back to the check
False: end the loop
For reference

Deeper by language

A few extra quirks per language, beyond the plain comparison.

Python
Python uses indentation to decide which code belongs to which block, there are no curly braces. Variables get their type assigned automatically and can even change it during the program. Extra functionality comes through so called packages, installable with a tool called pip. Python isn't compiled ahead of time like C, it's interpreted line by line while running, which makes it a bit slower but easier to try things out with.
Java
In Java, all code has to sit inside a class, a single standalone command isn't enough. A variable's type is fixed when it's written and stays fixed afterward, called static typing. Java gets compiled ahead of time into an intermediate code that runs on what's called the Java Virtual Machine, which is why the same Java code runs on Windows, Mac and Linux without changes.
C
C sits very close to the hardware, without many of the safety nets other languages give beginners. Memory sometimes has to be requested and freed by hand, forget that and you get what's called a memory leak. In exchange, compiled C code runs especially fast, which is why C is still used for operating systems and, as mentioned elsewhere on this page, for many microcontrollers.
For reference

ASCII table

All printable ASCII characters with their number code. You don't need to memorize this table, it's just here for reference whenever you need a code.

CharacterCodeCharacterCodeCharacterCodeCharacterCode
Space32!33"34#35
$36%37&38'39
(40)41*42+43
,44-45.46/47
048149250351
452553654755
856957:58;59
<60=61>62?63
@64A65B66C67
D68E69F70G71
H72I73J74K75
L76M77N78O79
P80Q81R82S83
T84U85V86W87
X88Y89Z90[91
\92]93^94_95
`96a97b98c99
d100e101f102g103
h104i105j106k107
l108m109n110o111
p112q113r114s115
t116u117v118w119
x120y121z122{123
|124}125~126