0% found this document useful (0 votes)
8 views3 pages

102programming Lectures Notes Handout1

This document provides an introduction to writing a simple C++ program, specifically a 'Hello World!' program, and emphasizes the importance of code layout for readability. It explains the basic structure of C++ programs, including the use of the iostream library and the standard namespace, as well as the function main() and its components. Additionally, it outlines the steps for compiling and executing the program using a development environment.

Uploaded by

Winnie Tam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

102programming Lectures Notes Handout1

This document provides an introduction to writing a simple C++ program, specifically a 'Hello World!' program, and emphasizes the importance of code layout for readability. It explains the basic structure of C++ programs, including the use of the iostream library and the standard namespace, as well as the function main() and its components. Additionally, it outlines the steps for compiling and executing the program using a development environment.

Uploaded by

Winnie Tam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

CS 102 Programming I, Spring 2016

Lectures Notes: Handout 1


Dr. Ghriga
Writing a Simple Program

The first program in a new programming language is traditionally a program that displays
greetings such as: “Hello World!” Following this tradition, here is our first program in C++.

// File hello.cc

#include <iostream>
using namespace std;

int main()
{
cout << “Hello World!” << endl;
return 0;
}

Using our Code::Blocks environment (or Xcode), you should create a new project, give it a name
and then create a file hello.cc and enter the program as it appears in the above listing.

Adopting a Good Layout Style

C++ is case-sensitive. You cannot type MAIN, Main, or Return. On the other hand, C++ has a
free form layout. You can write your main program in any of the forms below:

int main() { cout << “Hello World!” << endl; return 0; }

Or write in this format,

int main()
{
cout
<<
“Hello World”
<<
endl
;
return 0;
}
Take a moment and contrast the readability of the above 3 layouts. The first program listing of
file hello.cc is by far the most readable by a human. Laying out your program in a readable
fashion is an important component of your development efforts in writing programs. You should
follow the first program listing. This effort will be recognized and rewarded.

Understanding the Structure of Simple C++ Programs

The file hello.cc shows the basic structure of a simple C++ program. The first line,

#include <iostream>

tells the compiler to read the file iostream, which contains the definition for stream input/output
package. You must include this file in your programs as they usually are expected to get inputs
or display outputs.

The next line,

using namespace std;

tells the compiler that all names used in the program belong to the “standard namespace.”
Namespaces are used to avoid name conflicts in large programs. In this course, separate name
spaces are not necessary: you will be always using the standard namespace, and you can simply
add this directive below your #include directives.

Another important part of the program is the line int main(), which defines a function called
main() which returns an integer. A function is a collection of programming instructions that
carry out a task. Every C++ program must have a main function. The instructions or statements
you put in main inside the curly braces will be executed one by one. Note that each statement
must end with a semi-colon.

This first statement uses cout to display text and uses the << symbols (called insertion operator)
to indicate what to output. The sequence of characters is enclosed in quotation marks so that the
compiler knows you literally meant to output “Hello World!” The endl manipulator forces the
end of line return and flushes the output to the screen. The return value 0 means successful
completion of the execution of the program.
After the tour of this traditional first C++ program, we are now ready to introduce the overall
structure we use for our simple C++ programs:

#include <iostream>
using namespace std;

int main()
{
statements
return 0;
}

Compiling and Executing Your Program

To compile your program, select Build from the menu and then click on Compile current file
from the submenu items. Select Build again from the submenu items to generate the executable
code for your program. Select Run to execute your program after generating the executable code.
You may also opt to choose Build and Run from the submenu items to generate your executable
code and execute the program with one click! All students are expected to be acquainted with
the development environment by the end of week one.

You might also like