Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
9 views

Module 2 Day 1 - Sample Program and Layout of A Program

The document provides an introduction to programming in C++, including: 1. It reviews flowcharts and their basic symbols used to represent program logic and flow. 2. It identifies the basic structure of a C++ program, including the main function, input/output statements, and comments. 3. It presents a sample "Hello World" program in C++ with explanations of each part, including the #include directive, using namespace statement, main function, cout statement, and return statement.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Module 2 Day 1 - Sample Program and Layout of A Program

The document provides an introduction to programming in C++, including: 1. It reviews flowcharts and their basic symbols used to represent program logic and flow. 2. It identifies the basic structure of a C++ program, including the main function, input/output statements, and comments. 3. It presents a sample "Hello World" program in C++ with explanations of each part, including the #include directive, using namespace statement, main function, cout statement, and return statement.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Module Code 2.

0
COMPUTER Introduction to a
Programming Language

SCIENCE 2
Lesson Code 2.1
PREPARED BY: Sample Program and
MAUREEN D. AGRAZAMENDEZ Layout of a Program
OBJECTIVES

• Review Flowcharts
• Identify and define the basic structure of a program;
• Create a simple program following the basic structure of a program;
• write simple computer programs in C++.
• write simple input and output statements.
REVIEW: FLOWCHARTS
FLOWCHART
• Flowchart is a graphical representation of an algorithm.
• Programmers often use it as a program-planning tool
to solve a problem.
• It makes use of symbols which are connected among
them to indicate the flow of information and processing
• The process of drawing a flowchart for an algorithm is
known as “flowcharting”.
BASIC SYMBOLS USED IN
FLOWCHART DESIGNS
• Terminal:
• The oval symbol indicates Start, Stop and
Halt in a program’s logic flow.
• A pause/halt is generally used in a program
logic under some error conditions.
• Terminal is the first and last symbols in the
flowchart.
BASIC SYMBOLS USED IN
FLOWCHART DESIGNS
• Input/Output:
• A parallelogram denotes any function of
input/output type.
• Program instructions that take input from
input devices and display output on output
devices are indicated with parallelogram in
a flowchart.
BASIC SYMBOLS USED IN
FLOWCHART DESIGNS
• Processing:
• A box represents arithmetic instructions.
• All arithmetic processes such as adding,
subtracting, multiplication and division are
indicated by action or process symbol.
BASIC SYMBOLS USED IN
FLOWCHART DESIGNS
• Decision
• Diamond symbol represents a decision
point.
• Decision based operations such as yes/no
question or true/false are indicated by
diamond in flowchart.
BASIC SYMBOLS USED IN
FLOWCHART DESIGNS
• Connectors:
• Whenever flowchart becomes complex or it
spreads over more than one page, it is useful to
use connectors to avoid any confusions.
• It is represented by a circle.
• Flow lines:
• Flow lines indicate the exact sequence in which
instructions are executed.
• Arrows represent the direction of flow of
control and relationship among different
symbols of flowchart.
RULES FOR CREATING
FLOWCHARTS
• A flowchart is a graphical representation of an algorithm.it should follow some rules while
creating a flowchart

Rule 1: Flowchart opening statement must be ‘start’ keyword.

Rule 2: Flowchart ending statement must be ‘end’ keyword.

Rule 3: All symbols in the flowchart must be connected with an arrow line.

Rule 4: The decision symbol in the flowchart cannot be associated with the arrow line.
C++
WELCOME TO C++
• C++—a powerful computer programming language that’s appropriate for technically
oriented people with little or no programming experience, and for experienced programmers
to use in building substantial information systems.
• You’re already familiar with the powerful tasks' computers perform. Using this PL, you’ll
write instructions commanding computers to perform those kinds of tasks.
WHAT IS C++?
• C++ evolved from C, which was developed by Dennis Ritchie at Bell Laboratories.
• C is available for most computers and is hardware independent.
• C++, an extension of C, was developed by Bjarne Stroustrup in the early 1980s at Bell
Laboratories.
• C++ provides a number of features that “spruce up” the C language, but more importantly, it
provides capabilities for object-oriented programming.
FIRST PROGRAM IN C++:
PRINTING A LINE OF TEXT
Create a flowchart that displays
“Hello World!” to the screen
SAMPLE PROGRAM IN C++
Comments
#include <iostream> // allows program to output data to the screen
using namespace std;

// function main begins program execution


int main()
{
cout << “Hello World!"; // display message
return 0; // indicate that program ended successfully
} // end function main
COMMENTS
• Single-line Comments begin with //, indicating that the remainder of each line is a comment.
• You insert comments to document your programs and to help other people read and
understand them.
• Comments do not cause the computer to perform any action when the program is run—
they’re ignored by the C++ compiler and do not cause any machine-language object code to
be generated.
• The comment Text-printing program describes the purpose of the program.
• A comment beginning with // is called a single-line comment because it terminates at the
end of the current line.
• [Note: You also may use C’s style in which a comment—possibly containing many lines—
begins with /* and ends with */.]
SAMPLE PROGRAM IN C++

Preprocessing Directive
A library is a collection
#include <iostream> // allows program to output of predefined
data to theroutines
screenor
using namespace std; programs that a program
can use. These routines
// function main begins program execution
or programs perform
int main()
{ specific tasks in a
cout << “Hello World!"; // display messageprogram without writing
the codes that do it.
return 0; // indicate that program ended successfully
} // end function main
SAMPLE PROGRAM IN C++

using namespace std;


#include <iostream> // allows program to output The
datausing
tonamespace
the screen
using namespace std; statement just means that in
the scope it is present, make all
// function main begins program execution the things under the std
int main() namespace available without
{ having to prefix std:: before
cout << “Hello World!"; // display messageeach of them
return 0; // indicate that program ended successfully
} // end function main
SAMPLE PROGRAM IN C++

using namespace std;


#include <iostream> // allows program to output data to the screen
using namespace std;

// function main begins program execution


int main()
{
std:: cout << “Hello World!"; // display message
return 0; // indicate that program ended successfully
} // end function main
SAMPLE PROGRAM IN C++

The main function


This line indicates the start of the
#include <iostream> // allows program to output program.
data to Herethe
we arescreen
declaring a
using namespace std; function named main, which is an
identifier. The parentheses ()
// function main begins program execution immediately following main is an
int main() operator that tells the compiler
{ that it is a function. Functions
cout << “Hello World!"; // display messagewill be discussed later in this
course. For now, always
return 0; // indicate that program ended successfully
remember that if you want a
} // end function main program to be executed, it must
have the main function.
SAMPLE PROGRAM IN C++

The curly braces


#include <iostream> // allows program to output The
data
curly to the
braces screen
indicate the
using namespace std; beginning and ending of a
function. Statements that are
// function main begins program execution needed for the program to work
int main() are inserted between the curly
braces.
{
cout << “Hello World!"; // display message
return 0; // indicate that program ended successfully
} // end function main
SAMPLE PROGRAM IN C++

statements
#include <iostream> // allows program to output These
datalines
toof the screen
codes are called a
using namespace std; statements. Statements are
fragments of a C++ program that
// function main begins program execution are executed in sequence.
int main() Statements always end with a
semicolon ( ; ). Each function
{
may contain as may statement
cout << “Hello World!"; // display messagedepending on how the program
return 0; // indicate that program ended successfully
will run.
} // end function main
Common Programming Error
SAMPLE PROGRAM IN C++
The Stream Insertion Operator
The << operator is referred to as the
#include <iostream> // allows program to output stream
datainsertion
to the screen
operator. When this
using namespace std; program executes,
the value to the operator’s right, the
// function main begins program execution right operand, is inserted in the output
int main() stream.
Notice that the operator points in the
{
direction of where the data goes. The
cout << “Hello World!"; // display messageright operand’s
return 0; // indicate that program ended successfully
characters normally print exactly as
} // end function main they appear between the double quotes
SUMMARY
• In summary, the general structure of a C+
+ program will always contain a
preprocessor directive, the main function
and program statements. The general
structure of a very simple program could
be shown as:
• You may also include comments in your
program to make it more readable and
could help other programmers understand
your program. You may use single line or
multi-line comments.

You might also like