0.6 — Your first program
Read one box at a time. No rush.
Hello, world!#
Type this in, then run it:
C++▶ Run
#include <iostream> int main(){ std::cout << "Hello, world!\n"; return 0;}Output
Hello, world!Congratulations — you are a programmer! 🎉
Line by line#
#include <iostream>= load the tool for printing and reading.int main()= the start. Every program runsmainfirst.{ }= the curly braces hold the steps ofmain.std::cout << "Hello, world!\n";= print the text.\nmeans "new line".return 0;= tell the computer "all done, no problems".
The semicolon#
Most lines end with a semicolon ;. It is like a full stop at the end of a sentence. It tells C++ "this step is finished".
C++ — wrong ✗
std::cout << "Oops\n"return 0;Forget one, and the compiler will complain.
Key idea
Every step (called a statement) ends with ;.
Try changing it#
Change the text in quotes and run it again:
C++▶ Run
#include <iostream> int main(){ std::cout << "My name is Sam.\n"; std::cout << "I am learning C++!\n"; return 0;}Output
My name is Sam.I am learning C++!Tip
Breaking things on purpose is a great way to learn. Remove a ; or a } and read what the compiler says.
Tip: press ← / → to move between lessons, / to search.