Hello World!
The first program anyone ever writes.
Pretty much any tutorial or lesson on programming will have you "print" something to the screen as your first program. We tend to use "Hello World!" as that first string, because it's fun.
Output is the only way the program can give feedback to you, the user. So let's start by creating a simple program that outputs "Hello World!".
Javascript
Python
C++
Java
Javascript does not have an output "window" perse, but it has (at least) two different ways you can print output.
Method 1: The console A console is a text area, a box, or a 'place' where a program can give text output, usually meant to be hidden from the user but sometimes it is the only input/output window (DOS or BASH prompts). Javascript can give output by "logging" to the console window:
HelloWorld.js
1
// Print to the console
2
console.log("Hello World!");
Copied!
Method 2: Writing to the current document Javascript is primarily meant to provide interactivity with a web page (HTML). So, if you are running your Javascript as the back-end to a document, you can write to that document directly:
HelloWorldDoc.js
1
// Write to the main document (this adds to whatever is already there)
2
document.write("Hello World!");
3
​
4
// OR you can overwrite the entire document
5
document.documentElement.innerHTML = "Hello World!";
Copied!
Python has one of the simplest methods for giving text output - the "print" command (method, function, whatever...):
HelloWorld.py
1
# Print output
2
print("Hello World!")
Copied!
I love C++ but it is a complicated beast (for good reasons). In order to print output, you need to ensure you are utilizing the code that controls "Standard Output" otherwise known as stdout.
HelloWorld.cpp
1
// Header file for IO (we are "including it" in our code)
2
#include<iostream>
3
​
4
// Utilizing the "standard" namespace, so we don't have to say "std" every time.
5
using namespace std;
6
7
// C++ programs need a main function where the code begins
8
int main() {
9
// Let's finally print to std out
10
cout << "Hello World";
11
​
12
// The main function has to return something
13
return 0;
14
}
Copied!
Java is also a great language to learn, but is also quite complicated (for good reason). Let's look at our first program in Java:
HelloWorld.java
1
// The class name has to match the filename
2
public class HelloWorld {
3
// Similar to C++, Java needs a main function to start the program
4
public static void main(String[] args) {
5
// Print "Hello, World"
6
System.out.println("Hello, World");
7
}
8
}
Copied!
Last modified 2yr ago
Copy link