Lesson 1 - Your first program
Friday, 03 March 2017 11:53 AM
General rule:
Always use << with cout.
Include <iostream>:
Special instruction that we always put at the top of the code to tell the compiler to include the
iostream header file. Header files basically define a number of additional instructions that we can
use in our programs to make them simpler and neater. A particularly useful one is iostream. The io in
iostream stands for Input/Output and this header file allows us to use the cout instruction to output
messages to the screen.
namespace std;
Some C++ compilers allow you to leave this out.
#include <iostream>
int main( )
{
std::cout << "Hello world";
return 0;
}
cout is defined in the iostream header file. All the names defined in it belong to a namespace called
std. If you don't use using namespace std; in your program, then you should prefix every name that
belongs to the std namespace with std::.
int main( )
This is the header for the main function of the program.
Semicolons:
Semicolon is called a statement terminator. Each statement must be terminated by a semicolon.
cout <<:
Stands for console output (display). Pronounce cout as “see-out”
Comment statement:
Is a text that we add to a program that has no effect on the running of the program but that is
generally used to explain what the programming (or part of the program) does.
//This is a comment statement
UNISA Page 1
, Endl:
Endl stands for end line. When used with cout it makes output continue on the next line.
C++ Structure:
//DescriptiveComment
#include <StandardHeaderFile>
using namespace std;
int main( )
{
StatementSequence;
}
UNISA Page 2
Friday, 03 March 2017 11:53 AM
General rule:
Always use << with cout.
Include <iostream>:
Special instruction that we always put at the top of the code to tell the compiler to include the
iostream header file. Header files basically define a number of additional instructions that we can
use in our programs to make them simpler and neater. A particularly useful one is iostream. The io in
iostream stands for Input/Output and this header file allows us to use the cout instruction to output
messages to the screen.
namespace std;
Some C++ compilers allow you to leave this out.
#include <iostream>
int main( )
{
std::cout << "Hello world";
return 0;
}
cout is defined in the iostream header file. All the names defined in it belong to a namespace called
std. If you don't use using namespace std; in your program, then you should prefix every name that
belongs to the std namespace with std::.
int main( )
This is the header for the main function of the program.
Semicolons:
Semicolon is called a statement terminator. Each statement must be terminated by a semicolon.
cout <<:
Stands for console output (display). Pronounce cout as “see-out”
Comment statement:
Is a text that we add to a program that has no effect on the running of the program but that is
generally used to explain what the programming (or part of the program) does.
//This is a comment statement
UNISA Page 1
, Endl:
Endl stands for end line. When used with cout it makes output continue on the next line.
C++ Structure:
//DescriptiveComment
#include <StandardHeaderFile>
using namespace std;
int main( )
{
StatementSequence;
}
UNISA Page 2