Hello World

Understanding our program

In the last section, we left off with this code.

// ConsoleApplication.cpp: This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>

int main()
{
    std::cout << "Hello World!\n";
}

This section will break down everything in this file before we add our own code.

If you didn't get this file or a similar one when you started a C++ program, you can copy this code and ensure you can run it. Hello World! should appear in a console window when you run the program. If it does not, something has likely gone wrong, and you must fix it before making your own programs.

Comments

Everything following double forward slashes // are comments. The compiler ignores comments, so they won't affect how our code runs.

You can use comments to document your code or leave notes for yourself or other developers.

Comments are usually highlighted in green.

Multiline Comments

/*This is another type of comment
It can span over many lines.*/

Multi-line comments start with /* and end with */. They let you comment multiple lines or stop the comment before the end of the line.

These are often used for longer notes or "commenting out" code when you have a piece of code you want to keep for reference but don't want to run.

#include <iostream>

#include tells the compiler that we want to use code from another file. Here we are using code that exists inside the file iostream.

This file is a part of the standard C++ library, and has code that handles the input and output to the console window.

int main()

As mentioned earlier, code executes from top to bottom, and generally from left to right.

The code execution starts inside the curly brackets after int main. The location code execution starts is called the entry point. 😉

The int main() syntax will make more sense as the course progresses.

The important thing to understand now is that code execution starts at the opening curly bracket ({) and ends at the closing curly bracket (}).

std::cout

std::cout << "Hello World!\n";

This line of code outputs the text Hello world to the console window. We separate the text we want as data from the text we want as code by wrapping quotation marks around it.

The cout object is the console output stream. This is the text that the console is showing.

The double lesser than sign << insert the data on the right (Hello world!) into the console output (cout).

cout is inside the std namespace. You can think of namespaces as folders. The double colons :: are used to access the namespace.

std is short for standard and includes tools that are part of the standard C++ library. You can think of it as a toolbox with pre-made tools that make programming in C++ easier.

"Hello world\n" - Escape Sequences

You might have noticed that \n isn't printed, despite being inside the quotation marks.

Escape sequences like \n represent special characters that are hard to include in a piece of text. Escape sequences start with a backward slash \.

  • \n: is the newline character. It represents a linebreak. If we type something after \n it prints on the next line.

  • \t: turns into a tab.

  • \": A quotation mark would normally end the text. \" includes a quotation mark without ending the text.

  • \\: A double backward slash includes \ in our text without using escape sequences.

To print Hello world!\n, we have to type std::cout << "Hello world!\\n";.

std::endl;

You can also use std::endl to "end line" by putting it at the end of your input stream.

std::cout << "This is an example of a line" << std::endl;
std::cout << "This text will appear on a new line";

Basic Math

The console output can also output numbers. Numbers don't need quotation marks.

#include <iostream>

int main()
{
    std::cout << 13579;
}
13579

An operator is a symbol that tells the computer to perform a specific mathematical or logical function. For example, the + symbol does addition.

Like in maths, multiplication(*) and division(/) operators have priority over the addition(+) and subtraction(-) operators.

#include <iostream>

int main()
{
    std::cout << 5+4*(3+3)/2;
}
17

Note:

Wrapping the equations in quotation marks makes the program see it as text. This prints the equation instead of performing the calculation.

#include <iostream>

int main()
{
    std::cout << "5+4*(3+3)/2";
}
5+4*(3+3)/2

Square Rooting & The Power of Search Engines

Don't limit yourself to the knowledge from this course when developing. A large part of development is finding information online. There will often be small gaps in your knowledge that don't demand a deep dive to be filled in. A quick online search can bring you an answer that lets you continue your development.

As an example, try getting the square root of a number.

Try searching for "square root of a number in C++"


To get the square root of a number, you can use the sqrt function.

Functions are reusable pieces of code that take an input and return an output. You can think of them as math functions (f(x)=...) We will cover functions in more depth in chapter 6.

In the same way std::cout requires the <iostream> header, the sqrt function requires the <cmath> header.

#include <cmath>
#include <iostream>

int main() 
{
	std::cout << "The square root of 5 is " << sqrt(5);
}

Now is a good time to head over to the practice area to familiarise yourself with it.

Select the chapter you're at, or use the "completed lessons" filter to find tasks to explore the knowledge you just gained.

It's important to use the knowledge you gain to solidify your learning. For each chapter, you're going to unlock a new understanding of a topic. New challenges and projects will unlock as you progress.

Questions

Questions

Questions

Questions

Questions

Questions

Questions

Questions