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

IT 102 - Computer Programming 1: Prepared By: Mr. Johnniephere E. Agravante CITE Instructor

This document provides an overview of lessons from weeks 5-8 of an IT 102 computer programming course in C++. It discusses the basic structure of a C++ program, including comments, variables, identifiers, and the main function. It provides an example "Hello World" program and explains each part, such as #include directives, using namespaces, output statements, and return codes. It also covers comments, variables, identifiers, and some common integrated development environments for writing C++ code.

Uploaded by

deniel dacillo
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

IT 102 - Computer Programming 1: Prepared By: Mr. Johnniephere E. Agravante CITE Instructor

This document provides an overview of lessons from weeks 5-8 of an IT 102 computer programming course in C++. It discusses the basic structure of a C++ program, including comments, variables, identifiers, and the main function. It provides an example "Hello World" program and explains each part, such as #include directives, using namespaces, output statements, and return codes. It also covers comments, variables, identifiers, and some common integrated development environments for writing C++ code.

Uploaded by

deniel dacillo
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

IT 102 – Computer

Programming 1
Prepared by: Mr. Johnniephere E. Agravante
CITE Instructor
WEEK 5 – WEEK 8 LESSONS

Base elements of C++ Program

Structure of a C++ program


Variables and Identifiers
DESIRED LEARNING OUTCOMES:

To be able to introduce and identify the basic structure


of a C++ program.
To be able to identify and discuss the base elements of
C++ program.
To be able to define variable and identifier.

To be able to differentiate identifier and variable.


STRUCTURE OF A C++ PROGRAM

// my first program in C++


#include <iostream> OUTPUT:
using namespace std;
int main () Hello World!
{
cout << "Hello World!";
return 0;
}
// my first program in C++

This is a comment line. All lines beginning with


two slash signs (//) are considered comments and
do not have any effect on the behavior of the
program. The programmer can use them to include
short explanations or observations within the
source code itself.
#include <iostream>

Lines beginning with a hash sign (#) are directives for the
preprocessor. They are not regular code lines with expressions
but indications for the compiler's preprocessor. In this case
the directive #include <iostream> tells the preprocessor to
include the iostream standard file. This specific file
(iostream) includes the declarations of the basic standard
input-output library in C++, and it is included because its
functionality is going to be used in the program.
using namespace std;

All the elements of the standard C++ library are


declared within what is called a namespace, the
namespace with the name std. So in order to access
its functionality we declare with this expression that
we will be using these entities. This line is very
frequent in C++ programs that use the standard
library, and in fact it will be included in most of the
source codes.
int main ( )

This line corresponds to the beginning of the definition of


the main function. The main function is the point by where
all C++ programs start their execution, independently of its
location within the source code. It does not matter whether
there are other functions with other names defined before
or after it - the instructions contained within this function's
definition will always be the first ones to be executed in
any C++ program. For that same reason, it is essential that
all C++ programs have a main function.
cout << "Hello World!";

This line is a C++ statement. A statement is a


simple or compound expression that can
actually produce some effect. In fact, this
statement performs the only action that
generates a visible effect in our first program.
return 0;

The return statement causes the main function


to finish. Return may be followed by a return code
(in our example is followed by the return code 0). A
return code of 0 for the main function is generally
interpreted as the program worked as expected
without any errors during its execution. This is the
most usual way to end a C++ console program.
EXAMPLE OF A C++ PROGRAM:

int main ()
{
cout << " Hello int main () { cout << "Hello World!"; return 0; }
World!";
return 0;
}
EXAMPLE C++ PROGRAM:

// my second program in C++


#include <iostream>
using namespace std;
int main ()
Output Hello World! I'm a C++ program
{
cout << "Hello World! ";
cout << "I'm a C++ program";
return 0;
}
EXAMPLE C++ PROGRAM:

int main () { cout << " Hello World! "; cout << " I'm a C++ program "; return 0; }
OR

int main ()
{
cout << "Hello World!";
cout << "I'm a C++ program";
return 0;
}
COMMENTS

Comments are parts of the source code


disregarded by the compiler. They simply do
nothing. Their purpose is only to allow the
programmer to insert notes or descriptions
embedded within the source code.
C++ supports two ways to insert
comments:

// line comment
/* block comment */
C++ PROGRAM:

/* my second program in C++ with more comments */


#include <iostream>
using namespace std;
int main ()
{
cout << "Hello World! "; // prints Hello World!
cout << "I'm a C++ program"; // prints I'm a C++ program
return 0;
}

Output Hello World! I'm a C++ program


WHAT IS VARIABLE?

-is a portion of memory to store a determined value.

a = 5;
b = 2;
a = a + 1;
result = a - b;
WHAT IS AN IDENTIFIER?

- is the name to denote labels, types, variables,


constants or functions, in a C++ program.
- A valid identifier is a sequence of one or more
letters, digits or underscore characters (_).
Neither spaces nor punctuation marks or symbols
can be part of an identifier. Only letters, digits
and single underscore characters are valid.
Standard Reserved Keywords are:

asm, auto, bool, break, case, catch, char, class, const,


const_cast, continue, default, delete, do, double,
dynamic_cast, else, enum, explicit, export, extern, false, float,
for, friend, goto, if, inline, int, long, mutable, namespace, new,
operator, private, protected, public, register, reinterpret_cast,
return, short, signed, sizeof, static, static_cast, struct, switch,
template, this, throw, true, try, typedef, typeid, typename,
union, unsigned, using, virtual, void, volatile, wchar_t, while
C++ IDE & SOURCE EDITOR -
SOFTWARE
Dev C++

Code:: Blocks

Visual Studio Code

Eclipse
CodeLite

NetBeans

CppDroid – Android Mobile Device


IT 102 – COMPUTER PROGRAMMING 1

Thank you and God


bless Everyone!
Stay Safe!

You might also like