🖥️
Intro to Computer Science (ICS3U/C)
  • An Introduction to Computer Science
  • Videos & Slides
  • Unit 1: In the Beginning
    • The History of Computers
    • Binary & Logic
      • Bits and Bytes (Binary)
      • Transistors (Changing Bits)
      • Logic Gates
        • Poster
        • Logic.ly
    • The Parts of a Computer
  • Unit 2: Intro to Code
    • How Do We Code?
      • Coding Conventions (Rules)
      • Commenting Code
    • What is HTML?
      • Hello World! (in HTML)
      • HTML Slideshow
    • Hello World!
    • Input / Output
      • The Console
      • Prompt, Alert, Confirm
    • Variables & Data
      • Strings (Text)
      • Numbers (Values)
        • Converting & Rounding
        • The Math Object
          • Random Numbers
      • Booleans
        • Truthiness
      • Arrays
  • Unit 3: Control Flow
    • Conditionals (if this, do that)
      • If...Else
        • Logical Operators
      • Switch / Case
      • Ternary Operators
    • Loops (Repeating Code)
      • For...Loop
      • While & Do/While Loops
    • Debugging
  • Unit 4: Functions
    • Functional Programming
    • User Defined Functions
      • Hoisting and Scope
    • Calling a JS Function
  • TL;DR
    • Programming Basics
    • Slideshows & Demos
    • Javascript Syntax Poster
  • Advanced Topics
    • Recursion
    • Structures & Algorithms
    • Mmm... Pi
  • External Links
    • Typing Club!
    • repl.it
    • Khan Academy
    • Geek Reading
    • ECOO CS Contest
Powered by GitBook
On this page
  1. Unit 2: Intro to Code

Hello World!

The first program anyone ever writes.

PreviousHello World! (in HTML)NextInput / Output

Last updated 6 years ago

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 does not have an output "window" perse, but it has (at least) two different ways you can print output.

Method 1: 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
// Print to the console
console.log("Hello World!");

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
// Write to the main document (this adds to whatever is already there)
document.write("Hello World!");

// OR you can overwrite the entire document
document.documentElement.innerHTML = "Hello World!";

Python has one of the simplest methods for giving text output - the "print" command (method, function, whatever...):

HelloWorld.py
# Print output
print("Hello World!")

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
// Header file for IO (we are "including it" in our code)
#include<iostream>  

// Utilizing the "standard" namespace, so we don't have to say "std" every time.
using namespace std; 
  
// C++ programs need a main function where the code begins
int main() { 
    // Let's finally print to std out
    cout << "Hello World"; 

    // The main function has to return something
    return 0; 
} 

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
// The class name has to match the filename
public class HelloWorld {
    // Similar to C++, Java needs a main function to start the program
    public static void main(String[] args) {
        // Print "Hello, World" 
        System.out.println("Hello, World");
    }
}
The console