0% found this document useful (0 votes)
2 views

CSC201 topic(basic_structure)

The document provides an overview of the basic structure of programming languages, specifically focusing on C++ and the 'Hello World' program. It explains the significance of comments, directives, functions, and statements within a C++ program, along with a line-by-line breakdown of the 'Hello World' code. Additionally, it emphasizes the importance of proper indentation for readability, while noting that C++ does not enforce strict rules on formatting.

Uploaded by

francis orafu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

CSC201 topic(basic_structure)

The document provides an overview of the basic structure of programming languages, specifically focusing on C++ and the 'Hello World' program. It explains the significance of comments, directives, functions, and statements within a C++ program, along with a line-by-line breakdown of the 'Hello World' code. Additionally, it emphasizes the importance of proper indentation for readability, while noting that C++ does not enforce strict rules on formatting.

Uploaded by

francis orafu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

CSC 201

Basic Structure of
Programming Languages

Download course materials here:


Are you preparing for the university or for the universe? http://www.iykelnhub.org/people_profile/ikechukwu-onyenwe
CSC 201

C++ Netbeans Installation requirements and quick


start tutorial

https://netbeans.org/kb/docs/cnd/quickstart.html

Download course materials here:


Are you preparing for the university or for the universe? http://www.iykelnhub.org/people_profile/ikechukwu-onyenwe
CSC 201

Structure of a program


The best way to learn a programming
language is by writing programs.

Hello World program to illustrate all
the fundamental components C++
which simply prints "Hello World" to
your computer screen.

Download course materials here:


Are you preparing for the university or for the universe? http://www.iykelnhub.org/people_profile/ikechukwu-onyenwe
CSC 201

Structure of a program

Source: http://www.cplusplus.com/doc/tutorial/program_structure/

C++ code for this program. The result when the program is
executed by a computer.

Line numbers to make discussing programs


and researching errors easier. They are not
part of the program.

Download course materials here:


Are you preparing for the university or for the universe? http://www.iykelnhub.org/people_profile/ikechukwu-onyenwe
CSC 201

Let's examine Hello World code line by line:


Structure of a program

Line 1: // my first program in C++

// indicate that the rest of the characters of line 1


after // is a comment inserted by the programmer
but which has no effect on the behavior of the
program.

Programmers use them to include short


explanations or observations concerning the code
or program. In this case, it is a brief introductory
description of the program.
Download course materials here:
Are you preparing for the university or for the universe? http://www.iykelnhub.org/people_profile/ikechukwu-onyenwe
CSC 201

Let's examine Hello World code line by line:


Structure of a program

Line 1: // my first program in C++ cont’d

Comments provide an important tool to document


directly within the source code what the program
does and how it operates.

C++ supports two ways of commenting code:


// line comment: disregards everything from // to
end of that same line.
/* block comment */: disregards everything within /
* and */. Multiple lines allowed.
Download course materials here:
Are you preparing for the university or for the universe? http://www.iykelnhub.org/people_profile/ikechukwu-onyenwe
CSC 201

Let's examine Hello World code line by line:


Structure of a program

Line 2: #include <iostream>


Lines beginning with a hash sign (#) are directives
read known as the preprocessor. They are special
lines interpreted before the compilation of the
program itself begins.

Here, the directive #include <iostream>, instructs


the preprocessor to include a header called iostream,
that allows standard input and output operations. Eg.
writing the output (Hello World) to the screen.

Download course materials here:


Are you preparing for the university or for the universe? http://www.iykelnhub.org/people_profile/ikechukwu-onyenwe
CSC 201

Let's examine Hello World code line by line:


Structure of a program

Line 3: A blank line.

Blank lines have no effect on a program. They simply


improve readability of the code.

Download course materials here:


Are you preparing for the university or for the universe? http://www.iykelnhub.org/people_profile/ikechukwu-onyenwe
CSC 201

Let's examine Hello World code line by line:


Structure of a program

Line 4: int main ()

This line initiates the declaration of a function. A


function is a group of code statements which are
given a name: here, the name is "main" to the group
of code statements that follow.

We will discuss Functions later, but essentially, their


definition is introduced with a succession of a type
(int), a name (main) and a pair of parentheses (()),
optionally including parameters.
Download course materials here:
Are you preparing for the university or for the universe? http://www.iykelnhub.org/people_profile/ikechukwu-onyenwe
CSC 201

Let's examine Hello World code line by line:


Structure of a program

Line 4: int main ()

The function named main is a special function in all


C++ programs; it is the function called when the
program is run. The execution of all C++ programs
begins with the main function, regardless of where
the function is actually located within the code.

Download course materials here:


Are you preparing for the university or for the universe? http://www.iykelnhub.org/people_profile/ikechukwu-onyenwe
CSC 201

Let's examine Hello World code line by line:


Structure of a program
Lines 5 and 7: { and }

The open brace ({) at line 5 indicates the beginning


of main's function definition, and the closing brace
(}) at line 7, indicates its end.

Everything between these braces is the function's


body that defines what happens when main is called.

All functions use braces to indicate the beginning and


end of their definitions.
Download course materials here:
Are you preparing for the university or for the universe? http://www.iykelnhub.org/people_profile/ikechukwu-onyenwe
CSC 201

Let's examine Hello World code line by line:


Structure of a program
Line 6: std::cout << "Hello World!";

This line is a C++ statement.

A statement is an expression that can actually


produce some effect. It is the meat of a program,
specifying its actual behavior.

Statements are executed in the same order that they


appear within a function's body.

Download course materials here:


Are you preparing for the university or for the universe? http://www.iykelnhub.org/people_profile/ikechukwu-onyenwe
CSC 201

Let's examine Hello World code line by line:


Structure of a program
Line 6: std::cout << "Hello World!";

This statement has three parts:


1. std::cout, which identifies the standard character
output device (usually, this is the computer
screen).
2. the insertion operator (<<), which indicates that
what follows is inserted into std::cout.
3. a sentence within quotes ("Hello world!"), is the
content inserted into the standard output.

Download course materials here:


Are you preparing for the university or for the universe? http://www.iykelnhub.org/people_profile/ikechukwu-onyenwe
CSC 201

Let's examine Hello World code line by line:


Structure of a program
Line 6: std::cout << "Hello World!";

Notice that the statement ends with a semicolon (;).


This character marks the end of the statement, just
as the period ends a sentence in English.

Note: All C++ statements must end with a semicolon


character. One of the most common syntax errors in
C++ is forgetting to end a statement with a
semicolon.

Download course materials here:


Are you preparing for the university or for the universe? http://www.iykelnhub.org/people_profile/ikechukwu-onyenwe
CSC 201

Let's examine Hello World code line by line:


Structure of a program
Further information:
Not all the lines of this program perform actions when the code
is executed.

a line containing a comment (beginning with //).

a line with a directive for the preprocessor (beginning with #).

a line that defines a function (in this case, the main function).

a line with a statements ending with a semicolon (the insertion


into cout), which was within the block delimited by the braces
( { } ) of the main function.
Download course materials here:
Are you preparing for the university or for the universe? http://www.iykelnhub.org/people_profile/ikechukwu-onyenwe
CSC 201

Let's examine Hello World code line by line:


Structure of a program
The program has been structured in different lines and
properly indented, in order to make it easier to understand for
the humans reading it.

But C++ does not have strict rules on indentation or on how to


split instructions in different lines.

For example, instead of

all in a single line, and this would have had exactly the same
meaning as the preceding code.

Download course materials here:


Are you preparing for the university or for the universe? http://www.iykelnhub.org/people_profile/ikechukwu-onyenwe
CSC 201

End of Today’s Lecture.


Questions!

Download course materials here:


Are you preparing for the university or for the universe? http://www.iykelnhub.org/people_profile/ikechukwu-onyenwe

You might also like