Basics · 01
Printing to the screen
Your first line of code: print text and the result of a calculation.
The first step in programming is output: writing something on the screen (standard output).
The example below prints one line of text and one calculated result.
print("Hello, World!")print(3 + 4)console.log("Hello, World!");console.log(3 + 4);console.log("Hello, World!");console.log(3 + 4);public class Main { public static void main(String[] args) { System.out.println("Hello, World!"); System.out.println(3 + 4); }}using System; class Program{ static void Main() { Console.WriteLine("Hello, World!"); Console.WriteLine(3 + 4); }}#include <iostream> int main() { std::cout << "Hello, World!" << '\n'; std::cout << 3 + 4 << '\n';}#include <stdio.h> int main(void) { printf("Hello, World!\n"); printf("%d\n", 3 + 4); return 0;}Console.WriteLine("Hello, World!");Console.WriteLine(3 + 4);Hello, World! 7
Key points
- Text goes in quotes.
Put a calculation in quotes, like"3 + 4", and it is printed as plain characters, not calculated. - A calculation written without quotes prints its result.
3 + 4prints7. - Most print functions end the line for you.
There is usually a separate one that keeps writing on the same line. - Codelper's judge compares your output with the answer character by character.
One extra space or missing newline makes it wrong. - Some languages run straight from the first line of the file; others start in a fixed
mainfunction.
How each language writes it
| Python | print(...) — stay on the line with print(x, end="").Runs from the first line of the file |
|---|---|
| JavaScript | console.log(...) — stay on the line with process.stdout.write(...).Runs from the first line of the file |
| TypeScript | Same as JavaScript |
| Java | System.out.println(...) — stay on the line with System.out.print(...).All code lives in a main function inside a class |
| C# | Console.WriteLine(...) — stay on the line with Console.Write(...).As in the example, it starts in a Main function inside a class |
| C++ | std::cout << ... << '\n' — never ends the line by itself, so write '\n' yourself.Starts at main |
| C | printf("...\n") — write the \n yourself.puts(...) adds the newline for you.Starts at main |
| Craykon | Console.WriteLine(...) — stay on the line with Console.Write(...).Runs from the first line of the file |
Common mistakes
Joining numbers onto text with +Java · JavaScript · TypeScript · C# · Craykon
console.log("Sum: " + 3 + 4);→ Sum: 34 — left to right, 3 is glued onto the text, then 4 onto that
Group the math first: "Sum: " + (3 + 4).
In Python, joining text and a number with + is a TypeError.
Forgetting \nC · C++
printf("Hello, World!");
printf("%d", 3 + 4);→ Hello, World!7 — the two lines run together, so the answer is wrong
End every line with \n: printf("%d\n", 3 + 4);
How languages differ
C's
print, console.log, println, WriteLine and Craykon's Console.WriteLine end the line for you.C's
printf and C++'s cout do not, so write \n yourself.