Hello World Program
Write and understand your first program
Our First Program --- Hello World Program
In this section, we will try to write our first program.
Here is a simple program in the C++ programming language that displays the phrase Hello World on the screen:
Let's break down this code.
Including Libraries
The first line #include <iostream> tells the computer to include the iostream library, which contains functions for input and output operations. This is necessary to use the cout command for text output.
The using namespace std Statement
The line using namespace std; means that we are using the std namespace. The std namespace contains standard objects and functions, including cout and endl.
The main Function
The main part of the program is located in the main() function. The main() function is the entry point of the program. The code inside this function will be executed when the program is run.
Text Output
The line cout << "Hello World" << endl; is used to output text to the screen. cout stands for "console output". We use the << operator to pass text to cout. Then we output the text "Hello World". The phrase << endl; adds a newline character, which makes the output more readable.
Semicolons
Notice the semicolon (;) at the end of each line. It marks the end of a statement and tells the compiler that one command has finished and we can move to the next one. Without semicolons, the code will be incorrect and will not compile.
Returning a Value
The main() function ends with the command return 0;. Here we return the value 0, which indicates successful program completion. If the program terminates with a non-zero value (return 1; and so on), this may indicate the presence of errors.
This program is a simple example of an introduction to programming. It outputs "Hello World" to the screen and explains basic concepts such as including libraries, using namespaces, the main() function, text output, and returning values.