Programming Basics
The same basic building blocks of programming, shown side by side in Java, Python and C.
Last updated: September 1, 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 = 5Cint 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 + bCint 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 commentPython# single line commentC// single line commentHow a loop with a condition runs
This is how a typical for or while loop with a check runs, step by step.